-
Notifications
You must be signed in to change notification settings - Fork 231
/
index.js
65 lines (56 loc) · 1.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Author: Michael Hadley, mikewesthad.com
* Asset Credits:
* - Phaser, Rich Davey, Ilija Melentijević
*/
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
parent: "game-container",
pixelArt: true,
scene: {
preload: preload,
create: create,
update: update,
},
};
const game = new Phaser.Game(config);
let controls;
function preload() {
this.load.image("tiles", "../assets/tilesets/catastrophi_tiles_16_blue.png");
this.load.tilemapCSV("map", "../assets/tilemaps/catastrophi_level3.csv");
}
function create() {
// When loading a CSV map, make sure to specify the tileWidth and tileHeight!
const map = this.make.tilemap({ key: "map", tileWidth: 16, tileHeight: 16 });
const tileset = map.addTilesetImage("tiles");
const layer = map.createLayer(0, tileset, 0, 0); // layer index, tileset, x, y
// Phaser supports multiple cameras, but you can access the default camera like this:
const camera = this.cameras.main;
// Set up the arrows to control the camera
const cursors = this.input.keyboard.createCursorKeys();
controls = new Phaser.Cameras.Controls.FixedKeyControl({
camera: camera,
left: cursors.left,
right: cursors.right,
up: cursors.up,
down: cursors.down,
speed: 0.5,
});
// Constrain the camera so that it isn't allowed to move outside the width/height of tilemap
camera.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
// Help text that has a "fixed" position on the screen
this.add
.text(16, 16, "Arrow keys to scroll", {
font: "18px monospace",
fill: "#ffffff",
padding: { x: 20, y: 10 },
backgroundColor: "#000000",
})
.setScrollFactor(0);
}
function update(time, delta) {
// Apply the controls to the camera each update tick of the game
controls.update(delta);
}