| 1 |
import { Roll } from "jga-games-opend6-dices"; |
| 2 |
|
| 3 |
import { Keys } from "jga-games-commands"; |
| 4 |
import { DisplayInterface } from "jga-games-display"; |
| 5 |
import { ComponentName, DrawSystem, Engine, Entity, MovementSystem, Player } from "jga-games-engine"; |
| Warning |
Row 6, Column 10: "'CellularMapGenerator' is defined but never used."
@typescript-eslint/no-unused-vars
|
| 6 |
import { CellularMapGenerator, Directions, GameMap, PositionInterface } from "jga-games-maps"; |
| 7 |
import { InputDataInterface, Screen } from "jga-games-screens"; |
| Warning |
Row 8, Column 10: "'TileFactory' is defined but never used."
@typescript-eslint/no-unused-vars
|
| 8 |
import { TileFactory } from "jga-games-tiles"; |
| 9 |
|
| 10 |
export class PlayScreen extends Screen { |
| 11 |
private map: GameMap; |
| 12 |
private player: Player; |
| 13 |
private entities: Map<string, Entity>; |
| 14 |
|
| 15 |
public constructor(engine: Engine, map: GameMap) { |
| 16 |
super("Play", engine); |
| 17 |
|
| 18 |
this.player = engine.getPlayer(); |
| 19 |
this.entities = engine.getEntities(); |
| 20 |
|
| 21 |
this.map = map; |
| 22 |
} |
| 23 |
|
| 24 |
public getCurrentX(): number { |
| 25 |
return this.player.getComponent(ComponentName.POSITION).getPosition().x; |
| 26 |
} |
| 27 |
|
| 28 |
public getCurrentY(): number { |
| 29 |
return this.player.getComponent(ComponentName.POSITION).getPosition().y; |
| 30 |
} |
| 31 |
|
| 32 |
public enter(): void { |
| 33 |
super.enter(); |
| 34 |
|
| 35 |
this.player.getComponent(ComponentName.POSITION).setPosition(this.map.getRandomFloorPosition()); |
| 36 |
} |
| 37 |
|
| 38 |
public exit(): void { |
| 39 |
// |
| 40 |
} |
| 41 |
|
| 42 |
public render(display: DisplayInterface): void { |
| 43 |
const topLeftValues = this.getTopLeftValues(display.getWidth(), display.getHeight()); |
| 44 |
const topLeftX = topLeftValues[0]; |
| 45 |
const topLeftY = topLeftValues[1]; |
| 46 |
|
| 47 |
for (let x = topLeftX; x < topLeftX + display.getWidth(); x++) { |
| 48 |
for (let y = topLeftY; y < topLeftY + display.getHeight(); y++) { |
| 49 |
// Fetch the glyph for the tile and render it to the screen |
| 50 |
const glyph = this.map.getTile({ x, y }).getGlyph(); |
| 51 |
display.draw({ x: x - topLeftX, y: y - topLeftY }, glyph); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
this.engine.process([ |
| 56 |
new DrawSystem(this.engine.getEntities(), display, { |
| 57 |
x: topLeftX, |
| 58 |
y: topLeftY, |
| 59 |
}), |
| 60 |
]); |
| 61 |
} |
| 62 |
|
| 63 |
public handleInput(inputType: string, inputData: InputDataInterface): string | null { |
| 64 |
let result: string | null = null; |
| 65 |
|
| 66 |
if (inputType === "keydown") { |
| 67 |
switch (inputData.code) { |
| 68 |
case Keys.ESCAPE: { |
| 69 |
// If escape is pressed, go to lose screen |
| 70 |
result = "lose"; |
| 71 |
break; |
| 72 |
} |
| 73 |
case Keys.RETURN: { |
| 74 |
// If enter is pressed, go to the win screen |
| 75 |
result = "win"; |
| 76 |
break; |
| 77 |
} |
| 78 |
case Keys.UP: { |
| 79 |
this.move(Directions.UP); |
| 80 |
this.notify(`Move ${Directions.UP}`); |
| 81 |
break; |
| 82 |
} |
| 83 |
case Keys.DOWN: { |
| 84 |
this.move(Directions.DOWN); |
| 85 |
this.notify(`Move ${Directions.DOWN}`); |
| 86 |
break; |
| 87 |
} |
| 88 |
case Keys.LEFT: { |
| 89 |
this.move(Directions.LEFT); |
| 90 |
this.notify(`Move ${Directions.LEFT}`); |
| 91 |
break; |
| 92 |
} |
| 93 |
case Keys.RIGHT: { |
| 94 |
this.move(Directions.RIGHT); |
| 95 |
this.notify(`Move ${Directions.RIGHT}`); |
| 96 |
break; |
| 97 |
} |
| 98 |
case Keys.ROLL: { |
| 99 |
const roll = new Roll(); |
| 100 |
this.notify(`Roll => ${roll.getResult()}`); |
| 101 |
break; |
| 102 |
} |
| 103 |
default: |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return result; |
| 108 |
} |
| 109 |
|
| 110 |
public move(direction: string): void { |
| 111 |
switch (direction) { |
| 112 |
case Directions.DOWN: { |
| 113 |
this.moveTo(0, 1); |
| 114 |
break; |
| 115 |
} |
| 116 |
case Directions.LEFT: { |
| 117 |
this.moveTo(-1, 0); |
| 118 |
break; |
| 119 |
} |
| 120 |
case Directions.RIGHT: { |
| 121 |
this.moveTo(1, 0); |
| 122 |
break; |
| 123 |
} |
| 124 |
case Directions.UP: { |
| 125 |
this.moveTo(0, -1); |
| 126 |
break; |
| 127 |
} |
| 128 |
default: |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public getTopLeftValues(displayWidth: number, displayHeight: number): number[] { |
| 133 |
// Make sure the x-axis doesn't go to the left of the left bound |
| 134 |
let topLeftX = Math.max(0, this.player.getComponent(ComponentName.POSITION).getPosition().x - displayWidth / 2); |
| 135 |
// Make sure we still have enough space to fit an entire game screen |
| 136 |
topLeftX = Math.min(topLeftX, this.map.getWidth() - displayWidth); |
| 137 |
|
| 138 |
// Make sure the y-axis doesn't above the top bound |
| 139 |
let topLeftY = Math.max( |
| 140 |
0, |
| 141 |
this.player.getComponent(ComponentName.POSITION).getPosition().y - displayHeight / 2, |
| 142 |
); |
| 143 |
// Make sure we still have enough space to fit an entire game screen |
| 144 |
topLeftY = Math.min(topLeftY, this.map.getHeight() - displayHeight); |
| 145 |
|
| 146 |
return [topLeftX, topLeftY]; |
| 147 |
} |
| 148 |
|
| 149 |
protected moveTo(dX: number, dY: number): void { |
| 150 |
const newPosition: PositionInterface = { x: 0, y: 0 }; |
| 151 |
|
| 152 |
newPosition.x = Math.max( |
| 153 |
0, |
| 154 |
Math.min(this.map.getWidth() - 1, this.player.getComponent(ComponentName.POSITION).getPosition().x + dX), |
| 155 |
); |
| 156 |
// Positive dY means movement down |
| 157 |
// negative means movement up |
| 158 |
// 0 means none |
| 159 |
newPosition.y = Math.max( |
| 160 |
0, |
| 161 |
Math.min(this.map.getHeight() - 1, this.player.getComponent(ComponentName.POSITION).getPosition().y + dY), |
| 162 |
); |
| 163 |
|
| 164 |
this.engine.process([new MovementSystem(this.engine.getEntities(), newPosition, this.map)]); |
| 165 |
} |
| 166 |
} |
| 167 |
|