-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo-platform.html
155 lines (128 loc) · 4.4 KB
/
demo-platform.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>GameLib</title>
<link href='css/gamelib.css' rel='stylesheet'>
<style>
html,
body {
height: 100%;
}
body {
margin: 0;
display: flex;
flex-direction: column;
/* This centers our sketch horizontally. */
justify-content: center;
/* This centers our sketch vertically. */
align-items: center;
}
p {
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<game></game>
<p>Use your arrow keys!</p>
<script src="js/gamelib.js"></script>
<script>
var msg;
var coins = 0;
function message(text) {
msg.img.text = text;
//console.log(text);
}
class Player extends Figure {
constructor(x, y, width, height, img) {
super(x, y, width, height, img, "Player");
this.jump = 0;
this.onGround = false;
}
update(obj) {
if (this.jump == 0) {
super.move(0, 3);
} else {
super.move(0, -3);
this.jump = this.jump - 3;
this.onGround = false;
}
}
collidate(obj) {
this.onGround = true;
if (obj.type == "Coin") {
message("Coins: " + coins);
return false;
} else {
return true;
}
}
keyHold(key) {
switch (key) {
case "ArrowLeft":
// Do something for "left arrow" key press.
this.img.costume = "left";
this.move(-3, 0);
break;
case "ArrowRight":
// Do something for "right arrow" key press.
this.img.costume = "right";
this.move(3, 0);
break;
default:
return; // Quit when this doesn't handle the key event.
}
}
keyPressed(key) {
switch (key) {
case "ArrowUp":
// Do something for "up arrow" key press.
if (this.onGround) this.jump = 120;
break;
default:
this.img.costumes["idle"] = [this.img.costumes[this.img.costume][0]];
this.img.costume = "idle";
}
}
}
class Coin extends Figure {
constructor(x, y, width, height, img) {
super(x, y, width, height, img, "Coin");
this.value = 1;
this.onGround = false;
}
update(obj) {
super.move(0, 3);
}
collidate(obj) {
if (obj.type == "Player") {
coins = coins + this.value;
this.value = 0;
this.img = new StaticNothing(this.width, this.height);
}
return false;
}
}
(async function () {
var world = new World("game", 800, 600);
world.background = new StaticImg("img/backgrounds/sky.png", 800, 600);
var player = new Player(50, 400, 64, 64, new Sprite("img/character-plattformer/dude.png", 32, 48));
player.img.costumes["left"] = [0, 1, 2, 3];
player.img.costumes["right"] = [5, 6, 7, 8];
player.img.costumes["idle"] = [4];
player.img.costume = "idle";
world.addEntity(player);
var coin = new Coin(300, 300, 16, 16, new Sprite("img/objects/coins.png", 16, 16));
coin.img.costumes["default"] = [0, 1, 2, 3, 4, 5, 6, 7];
coin.img.costume = "default";
world.addEntity(coin);
var ground = new Figure(0, 550, 800, 50, new StaticBackground("#215F3E", 800, 50));
world.addEntity(ground);
msg = new Figure(20, 20, 64, 64, new StaticText("Coins: " + coins));
world.addEntity(msg);
await world.play();
})()
</script>
</body>
</html>