Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]
### Added
- Selected entities can be moved with the arrow keys (Shift for 5x)

### Fixed
- Event steps now format array properties (e.g. enemies of START_PVP_BATTLE) instead of [object Object]
- Fixed an issue where keyboard shortcuts conflicted with input elements [#364](https://github.com/CCDirectLink/crosscode-map-editor/issues/364)
Expand Down
87 changes: 66 additions & 21 deletions webapp/src/app/services/phaser/entities/entity-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import { Vec2 } from '../vec2';
import { CCEntity } from './cc-entity';
import { SelectionBox } from './selection-box';

const arrowDirections: Record<string, Point> = {
ArrowLeft: { x: -1, y: 0 },
ArrowRight: { x: 1, y: 0 },
ArrowUp: { x: 0, y: -1 },
ArrowDown: { x: 0, y: 1 },
};

export class EntityManager extends BaseObject {

// TODO: If ? is really required, add a description why.
Expand All @@ -22,6 +29,7 @@ export class EntityManager extends BaseObject {
private visibilityKey!: Phaser.Input.Keyboard.Key;
private leftPointerDown = false;
private rightPointerDown = false;
private arrowKeyMoved = false;

private skipEdit = false;

Expand Down Expand Up @@ -58,7 +66,7 @@ export class EntityManager extends BaseObject {
prevEntity: undefined,
entity: undefined,
timer: 0,
pos: {x: 0, y: 0}
pos: { x: 0, y: 0 },
};

private selectedEntities: CCEntity[] = [];
Expand All @@ -69,7 +77,7 @@ export class EntityManager extends BaseObject {

constructor(
scene: Phaser.Scene,
active = true
active = true,
) {
super(scene, 'entityManager', active);
}
Expand Down Expand Up @@ -177,7 +185,7 @@ export class EntityManager extends BaseObject {
this.selectionBox.onInputDown(pointer);
}
},
emitter: this.scene.input
emitter: this.scene.input,
});

const pointerUpFun = (pointer: Phaser.Input.Pointer, gameObject: Phaser.GameObjects.GameObject[]) => {
Expand All @@ -199,7 +207,7 @@ export class EntityManager extends BaseObject {
this.gameObjectDown = false;
Globals.stateHistoryService.saveState({
name: 'Entity moved',
icon: 'open_with'
icon: 'open_with',
});
} else {
const entities = this.selectionBox.onInputUp();
Expand All @@ -217,7 +225,7 @@ export class EntityManager extends BaseObject {
entity = gameObject[0].getData('entity');
}
if (entity) {
const p = {x: pointer.worldX, y: pointer.worldY};
const p = { x: pointer.worldX, y: pointer.worldY };
if (this.leftClickOpts.timer < 200 && Vec2.distance2(p, this.leftClickOpts.pos) < 10) {
this.selectEntity(entity, this.multiSelectKey.isDown);

Expand All @@ -235,13 +243,13 @@ export class EntityManager extends BaseObject {
this.addKeybinding({
event: 'pointerup',
fun: pointerUpFun,
emitter: this.scene.input
emitter: this.scene.input,
});

this.addKeybinding({
event: 'pointerupoutside',
fun: pointerUpFun,
emitter: this.scene.input
emitter: this.scene.input,
});

this.addKeybinding({
Expand All @@ -253,9 +261,9 @@ export class EntityManager extends BaseObject {
}
Globals.gridSettings.update(settings => ({
...settings,
enableGrid: !settings.enableGrid
enableGrid: !settings.enableGrid,
}));
}
},
});

// TODO: still triggers when npc editor is open
Expand All @@ -267,7 +275,7 @@ export class EntityManager extends BaseObject {
return;
}
this.deleteSelectedEntities();
}
},
});

this.addKeybinding({
Expand All @@ -278,7 +286,44 @@ export class EntityManager extends BaseObject {
return;
}
this.hideEntities();
}
},
});

this.addKeybinding({
event: 'keydown',
emitter: this.scene.input.keyboard!,
fun: (event: KeyboardEvent) => {
const dir = arrowDirections[event.key];
if (!dir || Helper.isInputFocused() || this.selectedEntities.length === 0) {
return;
}
const grid = Globals.gridSettings();
const step = grid.enableGrid ? { ...grid.size } : { x: 1, y: 1 };
if (this.multiSelectKey.isDown) {
step.x *= 4;
step.y *= 4;
}
for (const entity of this.selectedEntities) {
entity.addPosition(dir.x * step.x, dir.y * step.y);
entity.updateZIndex();
}
this.arrowKeyMoved = true;
},
});

this.addKeybinding({
event: 'keyup',
emitter: this.scene.input.keyboard!,
fun: (event: KeyboardEvent) => {
if (!this.arrowKeyMoved || !arrowDirections[event.key]) {
return;
}
this.arrowKeyMoved = false;
Globals.stateHistoryService.saveState({
name: 'Entity moved',
icon: 'open_with',
});
},
});


Expand Down Expand Up @@ -314,15 +359,15 @@ export class EntityManager extends BaseObject {
for (const entity of map.entities) {
promises.push(this.generateEntity(entity));
}

const newEntities = await Promise.all(promises);

if (entities === this._entities) {
//Add at the start of the array, in case a user somehow managed to add a new entity while the map is loading
entities.unshift(...newEntities);
entities.unshift(...newEntities);
} else {
// Someone called `this.initialize` before we finished loading map. Destroy everything.
newEntities.forEach(e => e.destroy());
newEntities.forEach(e => e.destroy());
}
}

Expand Down Expand Up @@ -365,7 +410,7 @@ export class EntityManager extends BaseObject {

Globals.stateHistoryService.saveState({
name: 'Entity added',
icon: 'add'
icon: 'add',
}, true);

return e;
Expand All @@ -375,10 +420,10 @@ export class EntityManager extends BaseObject {
const entityClass = Globals.entityRegistry.getEntity(entity.type);
console.assert(!!this.map, 'I dont think map is ever undefined, but if it ever happens check the TODO on private map?: CCMap;');
const map = this.map!;

// Preload entities since we need it for loadJsonMergedSync in the entity's constructor
await Globals.jsonLoader.loadJsonMerged('entities.json');

const ccEntity = new entityClass(this.scene, map, entity.x, entity.y, entity.type);
if (!entity.settings.mapId) {
entity.settings.mapId = map.getUniqueMapid();
Expand All @@ -395,7 +440,7 @@ export class EntityManager extends BaseObject {
return;
}
await navigator.clipboard.writeText(JSON.stringify(entities));
Globals.snackbar.open('copied entity to clipboard', undefined, {duration: 1000});
Globals.snackbar.open('copied entity to clipboard', undefined, { duration: 1000 });

}

Expand All @@ -418,7 +463,7 @@ export class EntityManager extends BaseObject {
} catch (e) {
Globals.snackbar.open('could not parse entities from clipboard', undefined, {
duration: 2000,
panelClass: 'snackbar-error'
panelClass: 'snackbar-error',
});
return;
}
Expand Down Expand Up @@ -475,7 +520,7 @@ export class EntityManager extends BaseObject {
if (saveHistory) {
Globals.stateHistoryService.saveState({
name: 'Entity deleted',
icon: 'delete'
icon: 'delete',
}, true);
}
}
Expand Down
Loading