---
# System prepended metadata

title: March 28 2021

---

# March 28 2021

## Overview

- The model will detect collisions between game objects
- GameObjects will have listeners placed on them that the view can update based off of
- KeyInput events will be passed to the controller and the controller will retrieve a string that maps to that event's keycode. 
For example, if the user presses "UP", the controller will retrieve what string (function) name that maps to "UP". This command name will map to a callback function that is put in the controller from the model. The controller will call this callback.
- Each model will have a controller that will be retrieved from the view. Probably a `getController()` method.
- GameObjects should be able to register their own key event actions. For example:

```json
interface GameObject {
    type: "character",
    ...
    input: [
        {
            key: "A",
            method: "fireBullet"
        }
    ]
}
```

So then when creating this gameobject it will register
the function onto the viewcontroller that will call its
"fireBullet" method everytime the 'A' is pressed.

This makes me think that actions will be their own classes but that's something we can talk about later!

Let's Try to have our API defined by wednesday! (not a hard deadline). Be thinking about model view controller interaction since we still don't have that finalized.

\>\> **Meeting 8pm Monday!** <<

## IMPORTANT RESOURCES

Sort collidble ranges before checking colissions

https://medium.com/ingeniouslysimple/entities-components-and-systems-89c31464240d

https://www.toptal.com/game/video-game-physics-part-ii-collision-detection-for-solid-objects

"Bag" (multimap). A map key that maps to a list of a type, in the guava library:

https://www.techiedelight.com/google-guava-multimap-class-java/

"Ranges". Easy way to see intersections:
https://www.baeldung.com/guava-rangeset


### Character

Design Pattern: Observer pattern.

Model has a standard unit size of 1 for position.
The view has a block size parameter that translates 



Calculate collisions from within the model.

As player moves, camera moves along with the player.
View determines how the camera will move.

Camera is view responsibility

```java=
public class Model {

}
```

```json=
{

type: "character"
height: 20,
width: 40,
x: 0,
}

```


```java=
public abstract class GameObject {
    - x = 0
    - y = 0
    x = 125
    y = 125
    
    
    - height = 250 units
    - width = 250
    - xVelcity
    - yVelocity
    setOnMove();
    setOnImageUpdate();
    setOnCollision();
    compareX;
    compareY;
}
```

## Model

- Detects collisions, do stuff based off of that
- Collidble game objects are added to special list during creation.

Within GameObject you can move the height width x and y stuff to its own
class.

## Controller

Receives input events from the view, translates those
(somehow) into the appropriate API calls on the model.

## View

Captures input events and passes straight to controller.

```java=
public class View {



public start() {
    root.onKeyPress( e -> {
        controller.handlePress(e);        
    })
}

}

public class Controller {
    private static Map<String, List<Function>> callbacks;
    private static Map<KeyCode, String> keymaps;
    
    public void register(String name, Function func) {
        callbacks.get(name).add(func);
    }

    handlePress(KeyEvent e) {
        String name = keymaps.get(e.keyCode());
        List<Function> c = callbacks.get(name);
        for (var cc : c) {
            cc.execute(1);
        }
    }
    
    handleRelease(KeyEvent e) {
        
    }
}

```

custom functions also for each object
```json
{
    inputs: [
        {
            "A": "fireBullet"
        }
    ]
}
```

``` java
public class Controller {
    Map activeKeys;

    handleActivateKey(KeyEvent keycode) {
        activeKeys.set(keycode.name(), true);
    }
    
    handleDeactivateKey(KeyEvent keycode) {
        activeKeys.set(keycode.name(), false);
    }
    
    updateMode() {
        model.update(activeKeys); // or give it upon construction
    }
}

public class model() {
    update(map activeKeys) {
    
        if (activeKeys.get("UP")) {
            methods.handleUp();
        }
        
        if (activeKeys.get("DOWN")) {
            movePlayerUP();
        }
    }
    
    movePlayetUP() {
        
    }
}
```

