forked from galah4d/pacman-cx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpac-man.cx
More file actions
213 lines (186 loc) · 5.38 KB
/
pac-man.cx
File metadata and controls
213 lines (186 loc) · 5.38 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import "gl"
import "glfw"
import "graphical2d"
// Game constants
var WIDTH_WINDOW i32 = 840
var HEIGHT_WINDOW i32 = 930
// Game flags
var GAME_LEVEL i32 = 1
var GAME_MAX_LEVEL i32 = 10
// Directions
var UP i32 = 0
var DOWN i32 = 1
var LEFT i32 = 2
var RIGHT i32 = 3
// structs
type Cell struct {
position graphical2d.Position2D
dimensions graphical2d.Dimensions2D
color graphical2d.Color
index_x i32
index_y i32
ctype i32
}
var cells [31][28]Cell
type Ghost struct {
position graphical2d.Position2D
color graphical2d.Color
radius f32
index_x i32
index_y i32
direction i32
gtype i32
status i32
}
var ghosts [4]Ghost
type Point struct {
position graphical2d.Position2D
color graphical2d.Color
radius f32
ptype i32
index_x i32
index_y i32
active bool
}
var points [1000] Point
var points_count i32 = 0
/***************************************
* Declaration of constructor functions.
***************************************/
/* Function : newCell
Input : Spawn line (line i32)
: Spawn column (column i32)
: Cell type (ct i32)
Output : cell (cell Cell)
Description : Constructor for the Cell struct.
*/
func newCell (line i32, column i32, ct i32) (cell Cell) {
var pos graphical2d.Position2D
pos = coords2position(line, column)
var dim graphical2d.Dimensions2D
dim = graphical2d.newDimensions2D(CELL_HEIGHT, CELL_WIDTH)
cell = Cell{
position: pos,
dimensions: dim,
color: getCellColor(ct),
index_x: column,
index_y: line,
ctype: ct}
}
/* Function : newGhost
Input : Spawn line (line i32)
: Spawn column (column i32)
: Ghost type (gt i32)
: Ghost movement mode (ghost_mode i32)
Output : ghost (ghost Ghost)
Description : Constructor for the Ghost struct.
*/
func newGhost(line i32, column i32, gt i32, ghost_mode i32) (ghost Ghost) {
var pos graphical2d.Position2D
pos = coords2position(line, column)
var col graphical2d.Color
col = getGhostColor(gt)
ghost = Ghost{
position: pos,
color: col,
radius: GHOST_RADIUS,
index_x: column,
index_y: line,
direction: UP,
gtype: gt,
status: ghost_mode}
}
/* Function : newPoint
Input : Spawn line (line i32)
: Spawn column (column i32)
: Point type (pt i32)
Output : point (point Point)
Description : Constructor for the Point struct.
*/
func newPoint (line i32, column i32, pt i32) (point Point) {
var pos graphical2d.Position2D
pos = coords2position(line, column)
var col graphical2d.Color
col = graphical2d.newColor(POINT_COLOR_RED, POINT_COLOR_GREEN, POINT_COLOR_BLUE)
var rad f32
if i32.eq(pt, PATH_CELL_WITH_SIMPLE_POINT) {
rad = SIMPLE_POINT_RADIUS
}
if i32.eq(pt, PATH_CELL_WITH_SUPER_POINT) {
rad = SUPER_POINT_RADIUS
}
point = Point {
position: pos,
color: col,
radius: rad,
ptype: pt,
index_x: column,
index_y: line,
active: true}
}
/***************************************
* Color management functions.
***************************************/
/* Function : getCellColor
Input : Cell type (ctype i32)
Output : The cell color (color graphical2d.Color)
Description : Returns the color corresponding to the cell type.
*/
func getCellColor (ctype i32) (color graphical2d.Color) {
if i32.eq(ctype, PATH_CELL) {
color = graphical2d.newColor(0.0, 0.0, 0.0)
}
if i32.eq(ctype, WALL_CELL) {
color = graphical2d.newColor(0.0, 0.0, 1.0)
}
if i32.eq(ctype, TELEPORT_CELL) {
color = graphical2d.newColor(0.0, 1.0, 1.0)
}
if i32.eq(ctype, GATE_CELL) {
color = graphical2d.newColor(1.0, 0.5, 0.0)
}
}
/* Function : getGhostColor
Input : Ghost type (gtype i32)
Output : The ghost color (color graphical2d.Color)
Description : Returns the color corresponding to the ghost type.
*/
func getGhostColor (gtype i32) (color graphical2d.Color) {
if i32.eq(gtype, SHADOW_GHOST) {
color = graphical2d.newColor(1.0, 0.0, 0.0) // Red color
}
if i32.eq(gtype, SPEEDY_GHOST) {
color = graphical2d.newColor(1.0, 0.5, 1.0) // Pink color
}
if i32.eq(gtype, BASHFUL_GHOST) {
color = graphical2d.newColor(0.0, 1.0, 1.0) // Cyan color
}
if i32.eq(gtype, POKEY_GHOST) {
color = graphical2d.newColor(1.0, 0.5, 0.25) // Orange color
}
}
/***************************************
* Movement related functions.
***************************************/
/* Function : canMove
Input : movement direction (direction i32)
: vertical board position (line i32)
: horizontal board position (column i32)
Output : true (if movement is possible), false (otherwise)
Description : Checks if a game object located at (line/column) can move in the input (direction).
*/
func canMove(direction i32, line i32, column i32) (can_move bool) {
if bool.and(i32.eq(direction, UP), i32.eq(cells[i32.sub(line, 1)][column].ctype, PATH_CELL)) {
can_move = true
}
if bool.and(i32.eq(direction, DOWN), i32.eq(cells[i32.add(line, 1)][column].ctype, PATH_CELL)) {
can_move = true
}
if bool.and(i32.eq(direction, LEFT), i32.eq(cells[line][i32.sub(column, 1)].ctype, PATH_CELL)) {
can_move = true
}
if bool.and(i32.eq(direction, RIGHT), i32.eq(cells[line][i32.add(column, 1)].ctype, PATH_CELL)) {
can_move = true
}
}