lutro-platformer/main.lua

204 lines
5.8 KiB
Lua
Raw Permalink Normal View History

2015-02-17 19:17:09 +00:00
require "global"
require "tiled"
require "anim"
require "ninja"
2017-06-25 08:43:05 +00:00
require "obake"
2017-06-25 10:00:45 +00:00
require "coin"
2017-06-25 13:46:44 +00:00
require "bigcoin"
2017-06-25 12:04:11 +00:00
require "porc"
2017-07-01 16:50:19 +00:00
require "demon"
2017-06-26 15:39:04 +00:00
require "elevator"
2017-06-26 21:26:37 +00:00
require "shuriken"
2017-07-01 16:50:19 +00:00
require "fireball"
2017-06-29 13:01:54 +00:00
require "sword"
2015-02-17 19:17:09 +00:00
function lutro.conf(t)
t.width = SCREEN_WIDTH
t.height = SCREEN_HEIGHT
end
local add_entity_from_map = function(object)
if object.type == "ground" then
table.insert(entities, object)
2017-07-02 19:50:27 +00:00
elseif object.type == "slopeleft" then
table.insert(entities, object)
elseif object.type == "sloperight" then
table.insert(entities, object)
2017-06-25 21:56:38 +00:00
elseif object.type == "bridge" then
table.insert(entities, object)
2017-06-26 15:39:04 +00:00
elseif object.type == "stopper" then
table.insert(entities, object)
elseif object.type == "elevator" then
table.insert(entities, newElevator(object))
2017-06-26 00:23:54 +00:00
elseif object.type == "spikes" then
table.insert(entities, object)
2017-06-25 10:00:45 +00:00
elseif object.type == "coin" then
table.insert(entities, newCoin(object))
2017-06-25 13:46:44 +00:00
elseif object.type == "bigcoin" then
table.insert(entities, newBigcoin(object))
2017-06-25 12:04:11 +00:00
elseif object.type == "porc" then
table.insert(entities, newPorc(object))
2017-07-01 16:50:19 +00:00
elseif object.type == "demon" then
table.insert(entities, newDemon(object))
2017-06-25 13:06:49 +00:00
elseif object.type == "obake" then
table.insert(entities, newObake(object))
elseif object.type == "ninja" then
ninja = newNinja(object)
table.insert(entities, ninja)
2015-02-17 19:17:09 +00:00
end
end
2017-06-27 08:50:11 +00:00
function entities_remove(entity)
for i=1, #entities do
if entities[i] == entity then
table.remove(entities, i)
end
end
end
2015-02-17 19:17:09 +00:00
function lutro.load()
camera_x = 0
camera_y = 0
2017-06-27 08:37:54 +00:00
camera_x_offset = 0
camera_y_offset = 0
gold = 0
2017-06-25 12:43:05 +00:00
hp = 3
2017-06-29 14:06:03 +00:00
screen_shake = 0
2017-06-28 15:59:08 +00:00
2015-02-23 00:14:37 +00:00
lutro.graphics.setBackgroundColor(0, 0, 0)
2017-06-28 15:59:08 +00:00
2017-06-28 15:53:42 +00:00
bg0 = lutro.graphics.newImage("assets/forestbackground0.png")
bg1 = lutro.graphics.newImage("assets/forestbackground1.png")
bg2 = lutro.graphics.newImage("assets/forestbackground2.png")
2017-06-28 15:59:08 +00:00
barfont = lutro.graphics.newImageFont("assets/barfont.png", "0123456789 xGHh")
2017-06-27 09:54:52 +00:00
lutro.graphics.setFont(barfont)
2017-06-28 15:59:08 +00:00
2017-07-02 19:50:27 +00:00
map = tiled_load("assets/slopes.json")
2015-02-17 19:17:09 +00:00
tiled_load_objects(map, add_entity_from_map)
2017-06-25 10:04:06 +00:00
2021-04-24 05:12:30 +00:00
sfx_coin = lutro.audio.newSource("assets/coin.wav", "static")
sfx_jump = lutro.audio.newSource("assets/jump.wav", "static")
sfx_step = lutro.audio.newSource("assets/step.wav", "static")
sfx_hit = lutro.audio.newSource("assets/hit.wav", "static")
sfx_porc = lutro.audio.newSource("assets/porc.wav", "static")
sfx_dead = lutro.audio.newSource("assets/dead.wav", "static")
sfx_throw = lutro.audio.newSource("assets/throw.wav", "static")
sfx_gameover = lutro.audio.newSource("assets/gameover.wav", "static")
sfx_enemyhit = lutro.audio.newSource("assets/enemyhit.wav", "static")
sfx_enemydie = lutro.audio.newSource("assets/enemydie.wav", "static")
sfx_porchit = lutro.audio.newSource("assets/porchit.wav", "static")
sfx_porcdie = lutro.audio.newSource("assets/porcdie.wav", "static")
sfx_shurikencollide = lutro.audio.newSource("assets/shurikencollide.wav", "static")
sfx_fireballspit = lutro.audio.newSource("assets/fireballspit.wav", "static")
sfx_fireballcollide = lutro.audio.newSource("assets/fireballcollide.wav", "static")
sfx_airgather = lutro.audio.newSource("assets/airgather.wav", "static")
2015-02-17 19:17:09 +00:00
end
2021-04-24 05:12:30 +00:00
RETRO_DEVICE_ID_JOYPAD_B = 1
RETRO_DEVICE_ID_JOYPAD_Y = 2
RETRO_DEVICE_ID_JOYPAD_SELECT = 3
RETRO_DEVICE_ID_JOYPAD_START = 4
RETRO_DEVICE_ID_JOYPAD_UP = 5
RETRO_DEVICE_ID_JOYPAD_DOWN = 6
RETRO_DEVICE_ID_JOYPAD_LEFT = 7
RETRO_DEVICE_ID_JOYPAD_RIGHT = 8
RETRO_DEVICE_ID_JOYPAD_A = 9
RETRO_DEVICE_ID_JOYPAD_X = 10
RETRO_DEVICE_ID_JOYPAD_L = 11
RETRO_DEVICE_ID_JOYPAD_R = 12
RETRO_DEVICE_ID_JOYPAD_L2 = 13
RETRO_DEVICE_ID_JOYPAD_R2 = 14
RETRO_DEVICE_ID_JOYPAD_L3 = 15
RETRO_DEVICE_ID_JOYPAD_R3 = 16
2017-06-25 22:43:48 +00:00
2021-04-24 05:12:30 +00:00
function lutro.update(dt)
JOY_LEFT = lutro.joystick.isDown(1, RETRO_DEVICE_ID_JOYPAD_LEFT)
JOY_RIGHT = lutro.joystick.isDown(1, RETRO_DEVICE_ID_JOYPAD_RIGHT)
JOY_DOWN = lutro.joystick.isDown(1, RETRO_DEVICE_ID_JOYPAD_DOWN)
JOY_A = lutro.joystick.isDown(1, RETRO_DEVICE_ID_JOYPAD_A)
JOY_B = lutro.joystick.isDown(1, RETRO_DEVICE_ID_JOYPAD_B)
JOY_X = lutro.joystick.isDown(1, RETRO_DEVICE_ID_JOYPAD_X)
JOY_Y = lutro.joystick.isDown(1, RETRO_DEVICE_ID_JOYPAD_Y)
2017-06-25 22:43:48 +00:00
2017-06-29 14:06:03 +00:00
if screen_shake > 0 then
screen_shake = screen_shake - 1
end
2017-06-25 23:18:38 +00:00
if hp > 0 then
2017-07-01 16:50:19 +00:00
--if screen_shake == 0 then
2017-06-29 14:06:03 +00:00
for i=1, #entities do
if entities[i] and entities[i].update then
entities[i]:update(dt)
end
2017-06-25 23:18:38 +00:00
end
2017-07-01 16:50:19 +00:00
--end
2017-06-25 23:18:38 +00:00
else
ninja:update(dt)
2015-02-17 19:17:09 +00:00
end
2015-02-17 19:17:09 +00:00
detect_collisions()
-- camera
camera_x = - ninja.x + SCREEN_WIDTH/2 - ninja.width/2;
2017-06-27 08:37:54 +00:00
if ninja.direction == "right" then
if JOY_RIGHT and camera_x_offset > -48 then
camera_x_offset = camera_x_offset - 1
end
else
if JOY_LEFT and camera_x_offset < 48 then
camera_x_offset = camera_x_offset + 1
end
end
camera_x = camera_x + camera_x_offset
if camera_x > 0 then
camera_x = 0
end
if camera_x < -(map.width * map.tilewidth) + SCREEN_WIDTH then
camera_x = -(map.width * map.tilewidth) + SCREEN_WIDTH
end
2015-02-17 19:17:09 +00:00
end
function lutro.draw()
2017-06-29 14:06:03 +00:00
-- Shake camera if hit
local shake_x = 0
local shake_y = 0
if screen_shake > 0 then
shake_x = 5*(math.random()-0.5)
shake_y = 5*(math.random()-0.5)
end
2015-02-23 00:14:37 +00:00
lutro.graphics.clear()
2015-02-17 19:56:16 +00:00
for i=0, 4 do
2017-06-28 15:53:42 +00:00
lutro.graphics.draw(bg2, i*bg2:getWidth() + camera_x / 8, 0)
lutro.graphics.draw(bg1, i*bg1:getWidth() + camera_x / 4, 0)
lutro.graphics.draw(bg0, i*bg0:getWidth() + camera_x / 2, 0)
2015-02-17 19:56:16 +00:00
end
lutro.graphics.push()
2017-06-29 14:06:03 +00:00
lutro.graphics.translate(camera_x + shake_x, camera_y + shake_y)
2015-02-17 19:17:09 +00:00
tiled_draw_layer(map.layers[1])
2017-06-27 21:20:58 +00:00
tiled_draw_layer(map.layers[2])
2015-02-17 19:17:09 +00:00
for i=1, #entities do
if entities[i].draw then
entities[i]:draw(dt)
end
end
2017-06-27 21:20:58 +00:00
lutro.graphics.pop()
2017-06-27 09:54:52 +00:00
local bar = ""
for i=1, 3 do
if i <= hp then
bar = bar .. "H"
else
bar = bar .. "h"
end
end
lutro.graphics.print(bar, 3, 2)
lutro.graphics.printf(gold .. "xG", -1, 2, SCREEN_WIDTH, "right")
2015-02-17 19:17:09 +00:00
end