-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
244 lines (187 loc) · 6.24 KB
/
player.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
function gen_next_valid(curr_x , curr_y , x_direction , y_direction , x_velocity , y_velocity)
{
var next_x = Math.max(0 , Math.min(num_box_width -1 , curr_x + x_direction * x_velocity))
var next_y = Math.max(0 , Math.min(num_box_height -1 , curr_y + y_direction * y_velocity))
return [next_x , next_y]
}
function gen_neighbors(curr_x , curr_y)
{
var x_start = Math.max(0 , curr_x - 1)
var x_end = Math.min(num_box_width - 1 , curr_x + 1)
var y_start = Math.max(0 , curr_y - 1)
var y_end = Math.min(num_box_height - 1 , curr_y + 1)
var neighbors = new Array()
for(var i = x_start; i <= x_end; i++)
{
for(var j = y_start; j <= y_end; j++)
{
if(!(i == curr_x && j == curr_y))
{
neighbors.push([i , j])
}
}
}
return neighbors
}
function music_maker_move(current_time)
{
if(current_time % this.frequency == 0)
{
var neighbors = gen_neighbors(this.x_index , this.y_index)
//var num_neighbors = Math.floor(Math.random() * neighbors.length)
var num_neighbors = neighbors.length
for(i = 0; i < num_neighbors; i++)
{
var neighbor_x = neighbors[i][0]
var neighbor_y = neighbors[i][1]
var direction_x = neighbor_x - this.x_index
var direction_y = direction_x == 0 ? (neighbor_y - this.y_index) : 0
var note = new music_note(neighbor_x , neighbor_y , direction_x , direction_y , 1 , 1 , [this.color[0] + 16, this.color[1] + 16, this.color[2] + 16] , this.tone , 6)
actors.push(note)
//Should replace all these accesses with one that will support multiple game boards
game_board_arr[neighbor_x][neighbor_y] = note
}
}
}
function player_move(current_time)
{
var next_index = gen_next_valid(this.x_index , this.y_index , this.x_direction , this.y_direction , this.x_velocity , this.y_velocity)
var next_x = next_index[0]
var next_y = next_index[1]
if(next_x == this.x_index &&
next_y == this.y_index)
{
if(this.x_direction < 0)
{
start_game_with_player(num_box_width - 1 , this.y_index)
}
else if(this.x_direction > 0)
{
start_game_with_player(0 , this.y_index)
}
else if(this.y_direction > 0)
{
start_game_with_player(this.x_index , 0)
}
else if(this.y_direction < 0)
{
start_game_with_player(this.x_index , num_box_height - 1)
}
}
this.x_index = next_x
this.y_index = next_y
}
function check_horizontal(curr_x , curr_y)
{
return game_board_get(curr_x - 1 , curr_y) instanceof music_note && game_board_get(curr_x + 1 , curr_y) instanceof music_note
}
function check_vertical(curr_x , curr_y)
{
return game_board_get(curr_x , curr_y - 1) instanceof music_note && game_board_get(curr_x , curr_y + 1) instanceof music_note
}
function check_edge_down_right(curr_x , curr_y)
{
return game_board_get(curr_x , curr_y + 1) instanceof music_note && game_board_get(curr_x + 1 , curr_y) instanceof music_note
}
function check_edge_down_left(curr_x , curr_y)
{
return game_board_get(curr_x , curr_y + 1) instanceof music_note && game_board_get(curr_x - 1 , curr_y) instanceof music_note
}
function check_edge_up_right(curr_x , curr_y)
{
return game_board_get(curr_x , curr_y - 1) instanceof music_note && game_board_get(curr_x + 1 , curr_y) instanceof music_note
}
function check_edge_up_left(curr_x , curr_y)
{
return game_board_get(curr_x , curr_y - 1) instanceof music_note && game_board_get(curr_x - 1 , curr_y) instanceof music_note
}
function check_crook_up_left(curr_x , curr_y)
{
return game_board_get(curr_x - 1 , curr_y - 1) instanceof music_note && game_board_get(curr_x, curr_y + 1) instanceof music_note
}
function check_crook_up_right(curr_x , curr_y)
{
return game_board_get(curr_x + 1 , curr_y - 1) instanceof music_note && game_board_get(curr_x, curr_y + 1) instanceof music_note
}
function check_crook_down_left(curr_x , curr_y)
{
return game_board_get(curr_x - 1 , curr_y + 1) instanceof music_note && game_board_get(curr_x, curr_y -1) instanceof music_note
}
function check_crook_down_right(curr_x , curr_y)
{
return game_board_get(curr_x + 1 , curr_y + 1) instanceof music_note && game_board_get(curr_x, curr_y - 1) instanceof music_note
}
function simple_move(current_time)
{
var next_valid = gen_next_valid(this.x_index , this.y_index , this.x_direction , this.y_direction , this.x_velocity , this.y_velocity)
var next_x = next_valid[0]
var next_y = next_valid[1]
this.intensity -= 1
this.color[0] += 2
this.color[1] += 2
this.color[2] += 2
this.x_index = next_x
this.y_index = next_y
}
function music_note_move(current_time)
{
if((check_edge_down_right(this.x_index , this.y_index)
|| check_edge_down_left(this.x_index , this.y_index))
^
(check_crook_up_right(this.x_index , this.y_index)
|| check_crook_up_left(this.x_index , this.y_index))
&&
!(check_vertical(this.x_index , this.y_index)))
{
var note = new music_note(this.x_index , this.y_index , 0 , -1 , 0 , 1 , this.color , this.tone , this.intensity)
actors.push(note)
note.simple_move()
}
else if((check_edge_up_left(this.x_index , this.y_index)
|| check_edge_up_right(this.x_index , this.y_index))
^
(check_crook_down_left(this.x_index , this.y_index)
|| check_crook_down_right(this.x_index , this.y_index))
&&
!(check_vertical(this.x_index , this.y_index)))
{
var note = new music_note(this.x_index , this.y_index , 0 , 1 , 0 , 1 , this.color , this.tone , this.intensity)
actors.push(note)
note.simple_move()
}
this.simple_move()
}
function music_note(x_index , y_index , x_direction , y_direction , x_velocity , y_velocity , color , tone , intensity)
{
this.x_index = x_index
this.y_index = y_index
this.x_direction = x_direction
this.y_direction = y_direction
this.x_velocity = x_velocity
this.y_velocity = y_velocity
this.color = color
this.tone = tone
this.intensity = intensity
this.simple_move = simple_move
this.move = music_note_move
}
function music_maker(x_index , y_index , frequency , tone , color)
{
this.x_index = x_index
this.y_index = y_index
this.color = color
this.frequency = frequency
this.tone = tone
this.move = music_maker_move
}
function player(x_index , y_index , x_direction , y_direction , x_velocity , y_velocity , color)
{
this.x_index = x_index
this.y_index = y_index
this.x_direction = x_direction
this.y_direction = y_direction
this.x_velocity = x_velocity
this.y_velocity = y_velocity
this.color = color
this.move = player_move
}