Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
172 additions
and
137 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Tiles from "../assets/Graphics"; | ||
import Mrpas from "mrpas"; | ||
|
||
const radius = 7; | ||
|
||
export default class FOV { | ||
private layer: Phaser.Tilemaps.DynamicTilemapLayer; | ||
private mrpas: any; | ||
private lastX: number; | ||
private lastY: number; | ||
private width: number; | ||
private height: number; | ||
|
||
constructor( | ||
width: number, | ||
height: number, | ||
walls: Array<Array<boolean>>, | ||
map: Phaser.Tilemaps.Tilemap | ||
) { | ||
const utilTiles = map.addTilesetImage("util"); | ||
|
||
this.layer = map | ||
.createBlankDynamicLayer("Dark", utilTiles, 0, 0) | ||
.fill(Tiles.util.indices.black); | ||
|
||
this.mrpas = new Mrpas(width, height, (x: number, y: number) => { | ||
return walls[y] && !walls[y][x]; | ||
}); | ||
|
||
this.lastX = -1; | ||
this.lastY = -1; | ||
|
||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
update(tileX: number, tileY: number) { | ||
if (this.lastX == tileX && this.lastY == tileY) { | ||
return; | ||
} | ||
this.lastX = tileX; | ||
this.lastY = tileY; | ||
|
||
this.layer.forEachTile( | ||
(t: Phaser.Tilemaps.Tile) => { | ||
t.alpha = t.alpha < 1 ? 0.7 : 1; | ||
}, | ||
this, | ||
0, | ||
0, | ||
this.width, | ||
this.height | ||
); | ||
|
||
this.mrpas.compute( | ||
tileX, | ||
tileY, | ||
radius, | ||
(x: number, y: number) => { | ||
return this.layer.getTileAt(x, y).alpha < 1; | ||
}, | ||
(x: number, y: number) => { | ||
this.layer.getTileAt(x, y).alpha = 0; | ||
} | ||
); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Phaser from "phaser"; | ||
import Graphics from "../assets/Graphics"; | ||
|
||
const speed = 150; | ||
|
||
export default class Player { | ||
public sprite: Phaser.Physics.Arcade.Sprite; | ||
private keys: Phaser.Input.Keyboard.CursorKeys; | ||
|
||
constructor(x: number, y: number, scene: Phaser.Scene) { | ||
scene.anims.create({ | ||
key: "player-walk", | ||
frames: scene.anims.generateFrameNumbers("player", { | ||
frames: Graphics.player.indices.south | ||
}), | ||
frameRate: 8, | ||
repeat: -1 | ||
}); | ||
|
||
this.sprite = scene.physics.add.sprite(x, y, "player", 0); | ||
this.sprite.setSize(13, 12); | ||
this.sprite.setOffset(9, 16); | ||
|
||
this.keys = scene.input.keyboard.createCursorKeys(); | ||
} | ||
|
||
update() { | ||
const keys = this.keys; | ||
const body = <Phaser.Physics.Arcade.Body>this.sprite.body; | ||
|
||
// Stop any previous movement from the last frame | ||
body.setVelocity(0); | ||
|
||
// Horizontal movement | ||
if (keys.left!.isDown) { | ||
body.setVelocityX(-speed); | ||
} else if (keys.right!.isDown) { | ||
body.setVelocityX(speed); | ||
} | ||
|
||
// Vertical movement | ||
if (keys.up!.isDown) { | ||
body.setVelocityY(-speed); | ||
} else if (keys.down!.isDown) { | ||
body.setVelocityY(speed); | ||
} | ||
|
||
// Normalize and scale the velocity so that sprite can't move faster along a diagonal | ||
body.velocity.normalize().scale(speed); | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.