-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
167 lines (143 loc) · 4.57 KB
/
script.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*eslint-disable*/
const config = {
type: Phaser.AUTO,
width: 800,
height: 800,
pixelArt: true,
parent: 'game-container',
physics: {
default: 'arcade',
arcade: {
gravity: {y: 0},
}
},
scene: {
preload: preload,
create: create,
update: update,
}
};
const game = new Phaser.Game(config);
let cursors;
let player;
function preload () {
this.load.image('tiles', 'assets/mars-surface-tiles.png');
this.load.tilemapTiledJSON('map', 'assets/mars-surface.json');
// this.load.image('curiosity', 'assets/curiosity.gif');
this.load.atlas("atlas", "assets/atlas.png", "assets/atlas.json");
}
function create () {
// tiles, terrain
const map = this.make.tilemap({key: 'map'});
const tileset = map.addTilesetImage('mars-surface-tiles', 'tiles');
// layers
const botLayer = map.createStaticLayer('bot', tileset,0,0);
const topLayer = map.createStaticLayer('top', tileset,0,0);
topLayer.setCollisionByProperty({ collides: true });
// Starting location of Curiosity
player = this.physics.add
.sprite(1200,900, 'atlas');
this.physics.add.collider(player, topLayer);
const anims = this.anims;
anims.create({
key: "curiosity-left-drive",
frames: anims.generateFrameNames("atlas", {
prefix: "curiosity-left-drive.",
start: 0,
end: 3,
zeroPad: 3
}),
frameRate: 10,
repeat: -1
});
anims.create({
key: "curiosity-right-drive",
frames: anims.generateFrameNames("atlas", {
prefix: "curiosity-right-drive.",
start: 0,
end: 3,
zeroPad: 3
}),
frameRate: 10,
repeat: -1
});
anims.create({
key: "curiosity-front-drive",
frames: anims.generateFrameNames("atlas", {
prefix: "curiosity-front-drive.",
start: 0,
end: 3,
zeroPad: 3
}),
frameRate: 10,
repeat: -1
});
anims.create({
key: "curiosity-back-drive",
frames: anims.generateFrameNames("atlas", {
prefix: "curiosity-back-drive.",
start: 0,
end: 2,
zeroPad: 3
}),
frameRate: 10,
repeat: -1
});
const camera = this.cameras.main;
camera.startFollow(player);
camera.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
// arrow keys
cursors = this.input.keyboard.createCursorKeys();
// Debug graphics
this.input.keyboard.once("keydown_D", event => {
// Turn on physics debugging to show player's hitbox
this.physics.world.createDebugGraphic();
// Create worldLayer collision graphic above the player, but below the help text
const graphics = this.add
.graphics()
.setAlpha(0.75)
.setDepth(20);
topLayer.renderDebug(graphics, {
tileColor: null, // Color of non-colliding tiles
collidingTileColor: new Phaser.Display.Color(255, 255, 255, 255), // Color of colliding tiles
faceColor: new Phaser.Display.Color(40, 39, 37, 255) // Color of colliding face edges
});
});
}
function update () {
const speed = 50;
const prevVelocity = player.body.velocity.clone();
// Stop any previous movement from the last frame
player.body.setVelocity(0);
// Horizontal movement
if (cursors.left.isDown) {
player.body.setVelocityX(-speed);
} else if (cursors.right.isDown) {
player.body.setVelocityX(speed);
}
// Vertical movement
if (cursors.up.isDown) {
player.body.setVelocityY(-speed);
} else if (cursors.down.isDown) {
player.body.setVelocityY(speed);
}
// Normalize and scale the velocity so that player can't move faster along a diagonal
player.body.velocity.normalize().scale(speed);
// Update the animation last and give left/right animations precedence over up/down animations
if (cursors.left.isDown) {
player.anims.play("curiosity-left-drive", true);
} else if (cursors.right.isDown) {
player.anims.play("curiosity-right-drive", true);
} else if (cursors.up.isDown) {
player.anims.play("curiosity-back-drive", true);
} else if (cursors.down.isDown) {
player.anims.play("curiosity-front-drive", true);
} else {
player.anims.stop();
// If we were moving, pick and idle frame to use
// if (prevVelocity.x < 0) player.setTexture("atlas", "curiosity");
// else if (prevVelocity.x > 0) player.setTexture("atlas", "curiosity");
// else if (prevVelocity.y < 0) player.setTexture("atlas", "curiosity");
// else if (prevVelocity.y > 0) player.setTexture("atlas", "curiosity");
}
}