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
|
//
// Example of an entity system
//
const MAX_ENTS number = 10;
const UNUSED number = 0;
const QUEUED number = 1;
const ACTIVE number = 2;
const PLAYER number = 0;
const ENEMY number = 1;
const ITEM number = 2;
def Entity {
var id number;
var used number;
var type number;
var x number;
var y number;
var frame number;
var delay number;
var lives number;
var score number;
def update() {
println("updating player");
}
def erase() {
println(id, " -> erase x:", x, " y:", y, " f:", frame);
}
def draw() {
println(id, " -> read bg x:", x, " y:", y);
println(id, " -> blit x:", x, " y:", y, " f:", frame);
}
def destroy() {
used = UNUSED;
}
def test(other Entity) bool {
// TODO
return false;
}
def enemyUpdate() {
println(id, " -> updating enemy ", used);
}
def enemy() {
type = ENEMY;
update = enemyUpdate;
}
}
def Game {
var ents [10]Entity;
var next number;
def spawn(x number, y number) Entity {
var start number = next;
for {
if !ents[next].used {
ents[next].used = QUEUED;
ents[next].x = x;
ents[next].y = y;
ents[next].frame = 0;
ents[next].delay = 0;
ents[next].id = next;
return ents[next];
}
next = (next + 1) % MAX_ENTS;
if next == start {
panic("out of ents :(");
}
}
}
var player Entity = spawn(1, 1);
def erase() {
for e in ents {
if e.used == ACTIVE {
e.erase();
}
}
}
def draw() {
for e in ents {
if e.used == ACTIVE {
e.draw();
}
}
}
def update() {
for e in ents {
if e.used == ACTIVE {
e.update();
if e.type != PLAYER && e.test(player) {
if e.type == ENEMY {
player.lives = player.lives - 1;
continue;
}
if e.type == ITEM {
player.score = player.score + 10;
e.destroy();
continue;
}
}
continue;
}
if e.used == QUEUED {
e.used = ACTIVE;
}
}
}
def run() {
var loops number;
for loops < 10 {
erase();
update();
draw();
// buffer SWAP
if player.lives == 0 {
println("game over");
break;
}
loops = loops + 1;
}
if loops == 10 {
println("out of rounds");
}
}
}
var game Game;
game.player.lives = 10;
game.spawn(1, 2).enemy();
game.spawn(2, 2).enemy();
game.spawn(2, 2).enemy();
game.spawn(2, 2).enemy();
game.run();
|