Skip to content

Commit

Permalink
add keyboard bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
daveey committed Mar 11, 2024
1 parent f62875e commit 76c15ff
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 27 deletions.
2 changes: 1 addition & 1 deletion js/griddlyjs-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions js/griddlyjs-app/src/renderer/Sprite2DRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class Sprite2DRenderer extends RendererBase {

sprite.setDisplaySize(
this.renderConfig.TileSize,
this.renderConfig.TileSize
this.renderConfig.TileSize
);
}

Expand All @@ -141,7 +141,12 @@ class Sprite2DRenderer extends RendererBase {
if (!sprite) {
return;
}
const objectTemplate = this.objectTemplates[objectTemplateName];
var objectTemplate = this.objectTemplates[objectTemplateName];
if (!objectTemplate) {
// pick the first one
objectTemplate = this.objectTemplates[Object.keys(this.objectTemplates)[0]];
}


sprite.setPosition(this.getCenteredX(x), this.getCenteredY(y));
sprite.setTexture(this.getTilingImage(objectTemplate, x, y));
Expand Down Expand Up @@ -228,6 +233,10 @@ class Sprite2DRenderer extends RendererBase {
};

getTilingImage = (objectTemplate, x, y) => {
if (objectTemplate === undefined) {
console.log("Undefined object template");
};

if (objectTemplate.tilingMode === "WALL_16") {
const objectLeft = this.tileLocations.get(
this.getObjectLocationKey(x - 1, y)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,57 @@ class HumanPlayerScene extends Phaser.Scene {

this.keyMap = new Map();

const mapKeyToAction = (key, actionName, actionTypeId, actionId, mapping) => {
const mappedKey = this.input.keyboard.addKey(key, false);
mappedKey.on("down", this.processUserKeydown);
mappedKey.on("up", this.processUserKeyup);

this.keyMap.set(key, {
actionName,
actionTypeId,
actionId,
description: mapping.description,
});
};

const presetActionKeys = {
"move": [ Phaser.Input.Keyboard.KeyCodes.E, Phaser.Input.Keyboard.KeyCodes.R ],
"rotate": [ Phaser.Input.Keyboard.KeyCodes.S, Phaser.Input.Keyboard.KeyCodes.A,
Phaser.Input.Keyboard.KeyCodes.D, Phaser.Input.Keyboard.KeyCodes.W ],
"pickup": [ Phaser.Input.Keyboard.KeyCodes.Y ],
"use": [ Phaser.Input.Keyboard.KeyCodes.U ],
"shield": [ Phaser.Input.Keyboard.KeyCodes.O ],
"prestige": [ Phaser.Input.Keyboard.KeyCodes.PLUS ],
"attack": [ Phaser.Input.Keyboard.KeyCodes.NUMPAD_ONE,
Phaser.Input.Keyboard.KeyCodes.NUMPAD_TWO,
Phaser.Input.Keyboard.KeyCodes.NUMPAD_THREE,
Phaser.Input.Keyboard.KeyCodes.NUMPAD_FOUR,
Phaser.Input.Keyboard.KeyCodes.NUMPAD_FIVE,
Phaser.Input.Keyboard.KeyCodes.NUMPAD_SIX,
Phaser.Input.Keyboard.KeyCodes.NUMPAD_SEVEN,
Phaser.Input.Keyboard.KeyCodes.NUMPAD_EIGHT,
Phaser.Input.Keyboard.KeyCodes.NUMPAD_NINE,
]
};

actionNames.forEach((actionName, actionTypeId) => {
const actionMapping = actionInputMappings[actionName];
if (!actionMapping.internal) {
const inputMappings = Object.entries(actionMapping.inputMappings);
console.log(inputMappings);

// If the action is one of the preset actions, use the preset keys
if (actionName in presetActionKeys) {
const keys = presetActionKeys[actionName];
inputMappings.forEach((inputMapping) => {
const actionId = Number(inputMapping[0]);
const key = keys[actionId - 1];
const mapping = inputMapping[1];
mapKeyToAction(key, actionName, actionTypeId, actionId, mapping);
});
return;
}

const actionDirections = new Set();
inputMappings.forEach((inputMapping) => {
// check that all the vectorToDest are different
Expand All @@ -485,36 +530,16 @@ class HumanPlayerScene extends Phaser.Scene {
key = movementKeys[this.toMovementKey(mapping.orientationVector)];
}

const mappedKey = this.input.keyboard.addKey(key, false);
mappedKey.on("down", this.processUserKeydown);
mappedKey.on("up", this.processUserKeyup);

this.keyMap.set(key, {
actionName,
actionTypeId,
actionId,
description: mapping.description,
});
mapKeyToAction(key, actionName, actionTypeId, actionId, mapping);
});
} else {
// We have an action Key

inputMappings.forEach((inputMapping) => {
const key = actionKeyOrder.pop();

const actionId = Number(inputMapping[0]);
const mapping = inputMapping[1];

const mappedKey = this.input.keyboard.addKey(key, false);
mappedKey.on("down", this.processUserKeydown);
mappedKey.on("up", this.processUserKeyup);

this.keyMap.set(key, {
actionName,
actionTypeId,
actionId,
description: mapping.description,
});
mapKeyToAction(key, actionName, actionTypeId, actionId, mapping);
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions resources/games/RTS/Stratega/heal-or-die.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Version: "0.1"
Environment:
Name: Heal Or Die
Description: |
Game environment ported from https://github.com/GAIGResearch/Stratega.
You have units that heal and units that perform close combat.
Game environment ported from https://github.com/GAIGResearch/Stratega.
You have units that heal and units that perform close combat.
Additionally, on every turn, the health of your units decreases. Win the game by killing your opponents pieces first.
Observers:
Sprite2D:
Expand Down

0 comments on commit 76c15ff

Please sign in to comment.