forked from anakrusis/harmony-game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
369 lines (278 loc) · 8.43 KB
/
Copy pathmain.lua
File metadata and controls
369 lines (278 loc) · 8.43 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
require "tile"
require "camera"
require "entity"
require "player"
require "note"
require "gui"
require "chord"
TILE_FREE = 1
TILE_CNTR = 130
TILE_HOLE = 4
TILE_WALL = 5
TILE_SPAWNMOB_ANT = 131
SPR_TILESET = love.graphics.newImage("img/tileset.png");
SPR_TILESET_0 = love.graphics.newImage("img/spr_tileset_0.png");
SPR_TILESET_1 = love.graphics.newImage("img/spr_tileset_1.png");
SPR_PLAYER_0 = love.graphics.newImage("img/spr_player_0.png");
SPR_PLAYER_1 = love.graphics.newImage("img/spr_player_1.png");
SPR_ENEMY = love.graphics.newImage("img/spr_enemy.png");
SPR_HEART = love.graphics.newImage("img/spr_heart.png");
SPR_PLAYER_HEAD = love.graphics.newImage("img/spr_player_head.png");
SPR_PLAYER_STEM = love.graphics.newImage("img/spr_player_stem.png");
SPR_HUD_TILE = love.graphics.newImage("img/spr_hud_tile.png");
SPR_HUD_ROOT = love.graphics.newImage("img/spr_hud_root.png");
SND_PLAYER = love.audio.newSource("snd/snd_player.wav", "static");
SND_ORGAN1 = love.audio.newSource("snd/pianuh.wav" , "static");
SND_ORGAN2 = love.audio.newSource("snd/pianuh.wav" , "static");
SND_ORGAN3 = love.audio.newSource("snd/pianuh.wav" , "static");
function isSafeTile(tile)
for i = 1, #player.safeTiles do
if player.safeTiles[i] == tile then return true end
end
return false
end
function ChangeChord(offsetsX, offsetsY)
player.safeTiles = {};
player.chordTones = {};
for i = 1, #offsetsX do
if map[player.tileX + offsetsX[i]] ~= nil then
tile = map[player.tileX + offsetsX[i]][player.tileY + offsetsY[i]]
if tile ~= nil and tile.tiletype == 1 then
table.insert(player.safeTiles, tile)
table.insert(player.chordTones, tile.note)
-- Scans the room for entitys and kills any that are on the safe tiles
for j = 1, #room.entities do
e = room.entities[j]
if e.tileX == tile.x and e.tileY == tile.y then
e.dead = true;
end
end
end
end
end
player.chordsRemaining = player.chordsRemaining - 1;
next_turn();
if player.chordsRemaining == 0 then
player.dead = true; player.respawn_timer = player.RESPAWN_TIME;
end
end
function PlayChord()
organ_tones = {SND_ORGAN1, SND_ORGAN2, SND_ORGAN3}
for i = 1, #organ_tones do
love.audio.stop( organ_tones[i] );
end
for i = 1, #player.chordTones do
organ_tones[i]:setPitch(player.chordTones[i].num / player.chordTones[i].den);
organ_tones[i]:setVolume(0.25);
organ_tones[i]:setLooping(false);
love.audio.play(organ_tones[i]);
end
end
function CalcPitchRatio( tilex, tiley )
xdiff = tilex - room.centerX;
ydiff = tiley - room.centerY;
output = {}
numerator = 1;
denominator = 1;
xout = math.pow( 3, math.abs( xdiff ) );
yout = math.pow( 5, math.abs( ydiff ) );
if xdiff >= 0 then
numerator = numerator * xout
else
denominator = denominator * xout
end
if ydiff >= 0 then
numerator = numerator * yout
else
denominator = denominator * yout
end
output[1] = numerator; output[2] = denominator;
-- octave reduction
if output[1]/output[2] > 2.0 then
while output[1]/output[2] > 2.0 do
output[2] = output[2] * 2;
end
end
if output[1]/output[2] < 1.0 then
while output[1]/output[2] < 1.0 do
output[1] = output[1] * 2;
end
end
return output;
end
function next_turn()
for i = 1, #room.entities do
e = room.entities[i];
if not e.dead then
e:nextTurn();
end
end
end
function init_room( id )
room = rooms[id]
room_data = require(room_paths[id])
room.width = room_data.width; room.height = room_data.height;
room.entities = {};
room.particles = {};
map = {};
-- First pass on room initialization just looks for the center tile (Its marked with the red circle in the editor but not in game)
for i = 1, room.height do
for j = 1, room.width do
index = ((j-1) * room.width) + (i-1)
tilefromdata = room_data.layers[1].data[index+1]
if tilefromdata == TILE_CNTR then
room.centerX = i; room.centerY = j;
tilefromdata = TILE_FREE
end
end
end
-- Second pass on room initialization creates all the tile objects and, with the known center tile, calculates the note pitches and makes note objects
for i = 1, room.height do
map[i] = {};
for j = 1, room.width do
index = ((j-1) * room.width) + (i-1)
tilefromdata = room_data.layers[1].data[index+1]
if tilefromdata == 2 or tilefromdata == TILE_CNTR then
tilefromdata = TILE_FREE
end
if tilefromdata == TILE_SPAWNMOB_ANT then
e = EntityAnt:new{ tileX = i, tileY = j };
table.insert(room.entities, e)
tilefromdata = TILE_FREE
end
t = Tile:new{ x = i, y = j, tiletype = tilefromdata };
pitch = CalcPitchRatio(i,j)
n = Note:new{ num = pitch[1], den = pitch[2] }
t.note = n;
map[i][j] = t;
end
end
-- new player object is created every room init ( maybe it shouldnt be like that I DONT KNOW )
player = Player:new{ tileX = room.centerX, tileY = room.centerY };
end
function love.load()
success = love.window.setMode( 800, 600, {resizable=true} )
love.window.setTitle("JI Deez game")
font = love.graphics.newFont("zeldadxt.ttf", 24)
love.graphics.setFont(font)
init_gui();
room_paths = {
"room/testroom",
"room/testroom2",
"room/testroom3"}
rooms = require "rooms"
init_room(3)
end
function love.keypressed(key, scancode, isrepeat)
if not player.dead and player.chordsRemaining > 0 then
if key == "z" or key == "x" or key == "c" then
if key == "z" then
chord = room.chordshapes[1]
end
if key == "x" then
chord = room.chordshapes[2]
end
if key == "c" then
chord = room.chordshapes[3]
end
ChangeChord( chord.xoffsets, chord.yoffsets ) -- major triad
PlayChord();
end
if key == "r" then
ChangeChord({}, {});
PlayChord();
end
end
if not player.moving and not player.dead then
if key == "up" then
if player.tileY > 1 then
targettile = map[player.tileX][player.tileY - 1]
if targettile.tiletype == 1 then
player.tileY = player.tileY - 1;
player.moving = true;
end
end
elseif key == "down" then
if player.tileY < room.height then
targettile = map[player.tileX][player.tileY + 1]
if targettile.tiletype == 1 then
player.tileY = player.tileY + 1;
player.moving = true;
end
end
end
if key == "left" then
if player.tileX > 1 then
targettile = map[player.tileX - 1][player.tileY]
if targettile.tiletype == 1 then
player.tileX = player.tileX - 1;
player.moving = true;
end
end
elseif key == "right" then
if player.tileX < room.width then
targettile = map[player.tileX + 1][player.tileY]
if targettile.tiletype == 1 then
player.tileX = player.tileX + 1;
player.moving = true;
end
end
end
end
end
function love.update(dt)
player.uppercase = love.keyboard.isDown("lshift")
player:update();
for i = 1, #room.entities do
e = room.entities[i];
e:update();
end
for i = 1, #window do
e = window[i];
e:update();
end
if player.dead and player.respawn_timer <= (player.RESPAWN_TIME / 2) then
init_room(3);
end
end
function love.draw()
for i = 1, room.width do
for j = 1, room.height do
t = map[i][j]
tx = ((t.tiletype - 1) % 16) * 32
ty = math.floor((t.tiletype - 1) / 16) * 32
if t.tiletype ~= 0 then
quad = love.graphics.newQuad( tx, ty, 32, 32, SPR_TILESET:getDimensions() )
love.graphics.draw(SPR_TILESET, quad, tra_x(i*32), tra_y(j*32), 0, cam_zoom, cam_zoom)
end
end
end
for i = 1, #player.safeTiles do
tile = player.safeTiles[i]
love.graphics.draw(SPR_TILESET_0, tra_x(tile.x*32), tra_y(tile.y*32), 0, cam_zoom, cam_zoom)
end
for i = 1, #room.entities do
e = room.entities[i]
if not e.dead then
e:draw();
end
end
player:draw();
if player.dead then
opacity = 0.5 + ( math.sin( (player.RESPAWN_TIME - 20 - player.respawn_timer) * (math.pi / ( player.RESPAWN_TIME / 2 )) ) / 2 )
love.graphics.setColor(0,0,0,opacity)
love.graphics.rectangle("fill",0,0,love.graphics.getWidth(),love.graphics.getHeight())
love.graphics.setColor(1,1,1,1)
end
for i = 1, #window do
box = window[i];
box:draw();
end
for i = 0, player.health - 1 do
love.graphics.draw(SPR_HEART, 36 + (i*40), 32)
end
-- love.graphics.print(player.currentnum .. "/" .. player.currentden, 0, 0)
-- cents = 1200 * (math.log( player.currentnum / player.currentden ) / math.log( 2 ))
-- love.graphics.print(cents, 0, 80)
-- love.graphics.print("Chords remaining: " .. player.chordsRemaining, 0, 120)
end