forked from galah4d/pacman-cx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame-loop.cx
More file actions
102 lines (83 loc) · 2.16 KB
/
game-loop.cx
File metadata and controls
102 lines (83 loc) · 2.16 KB
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
package main
var GETREADY_LIMIT f64 = 2.0D
var GETREADY_TIMER f64
var GAMEMODE_GETREADY i32
var GAMEMODE_PLAYING i32 = 1
var GAMEMODE_WONBOARD i32 = 2
var GAMEMODE_DYING i32 = 3
var GAMEMODE_GAMEOVER i32 = 4
var GAMEMODE_MENU i32 = 5
var gameMode i32
func main() {
initGLFW()
var program i32 =
initOpenGL()
newlevel_label:
// Initializes all game objects
initScene()
// Game loop
for bool.not(glfw.ShouldClose("window")) &&
gameMode != GAMEMODE_GAMEOVER {
if gameMode == GAMEMODE_GETREADY {
if isTimerOver(GETREADY_TIMER, GETREADY_LIMIT) {
gameMode = GAMEMODE_PLAYING
}
} else
if gameMode == GAMEMODE_PLAYING {
updateGamePlay()
}
renderGame(program)
glfw.PollEvents()
glfw.SwapBuffers("window")
}
}
func updateGamePlay() {
for i:=0; i<4; i++ {
updateGhostMode(ghosts[i])
}
movePlayerByHeldKeys()
manageAnyDotEating()
maybeMoveGhosts()
maybeReleaseGhost()
if isLevelCompleted() {
levelUp()
goto newlevel_label
}
}
func manageAnyDotEating() {
for i:=0; i<points_count; i++ {
if points[i].active &&
i32.eq(points[i].index_x, player.index_x) &&
i32.eq(points[i].index_y, player.index_y) {
points[i].active = false
if i32.eq(points[i].ptype, PATH_CELL_WITH_SUPER_POINT) {
for j:=0; j<4; j++ {
if bool.not(i32.eq(ghosts[j].status, GHOST_MODE_IN_HOUSE)) {
ghosts[j].status = GHOST_MODE_FRIGHTENED
ghosts[j].direction = reverseDirection(ghosts[j].direction)
GHOST_FRIGHTENED_TIMER = glfw.GetTime()
}
}
}
}
}
}
func maybeMoveGhosts() {
if isTimerOver(GHOST_MOVEMENT_TIMER, GHOST_MOVEMENT_LIMIT) {
// move all ghosts
for i:=0; i<4; i++ {
moveGhost(ghosts[i])
checkPlayerGhostCollision(ghosts[i])
}
GHOST_MOVEMENT_TIMER = glfw.GetTime()
}
}
func maybeReleaseGhost() {
if isTimerOver(GHOST_RELEASE_TIMER, GHOST_CHASE_LIMIT) {
// Does not release ghosts during frightened mode
if isTimerOver(GHOST_FRIGHTENED_TIMER, GHOST_FRIGHTENED_LIMIT) {
releaseGhost()
}
GHOST_RELEASE_TIMER = glfw.GetTime()
}
}