Repository navigation
move entities with arrow keys #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vegita2
wants to merge
3
commits into
master
Choose a base branch
from
361-move-entities
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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; | ||
|
|
||
|
|
@@ -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[] = []; | ||
|
|
@@ -69,7 +77,7 @@ export class EntityManager extends BaseObject { | |
|
|
||
| constructor( | ||
| scene: Phaser.Scene, | ||
| active = true | ||
| active = true, | ||
| ) { | ||
| super(scene, 'entityManager', active); | ||
| } | ||
|
|
@@ -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[]) => { | ||
|
|
@@ -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(); | ||
|
|
@@ -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); | ||
|
|
||
|
|
@@ -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({ | ||
|
|
@@ -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 | ||
|
|
@@ -267,7 +275,7 @@ export class EntityManager extends BaseObject { | |
| return; | ||
| } | ||
| this.deleteSelectedEntities(); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| this.addKeybinding({ | ||
|
|
@@ -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 *= 5; | ||
| step.y *= 5; | ||
| } | ||
| this.selectedEntities.forEach(entity => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just my personal preference, but in places like these I much prefer a regular for loop. |
||
| 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', | ||
| }); | ||
| }, | ||
| }); | ||
|
|
||
|
|
||
|
|
@@ -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()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -365,7 +410,7 @@ export class EntityManager extends BaseObject { | |
|
|
||
| Globals.stateHistoryService.saveState({ | ||
| name: 'Entity added', | ||
| icon: 'add' | ||
| icon: 'add', | ||
| }, true); | ||
|
|
||
| return e; | ||
|
|
@@ -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(); | ||
|
|
@@ -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 }); | ||
|
|
||
| } | ||
|
|
||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -475,7 +520,7 @@ export class EntityManager extends BaseObject { | |
| if (saveHistory) { | ||
| Globals.stateHistoryService.saveState({ | ||
| name: 'Entity deleted', | ||
| icon: 'delete' | ||
| icon: 'delete', | ||
| }, true); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend reducing the shift factor to 4 so that is more useful without grid mode