lutro-platformer/ninja.lua

215 lines
4.5 KiB
Lua
Raw Normal View History

2015-02-17 19:17:09 +00:00
require "collisions"
local ninja = {}
ninja.__index = ninja
2017-06-25 13:06:49 +00:00
function newNinja(object)
local n = object
2015-02-17 19:17:09 +00:00
n.width = 16
n.height = 32
n.xspeed = 0
n.yspeed = 0
n.xaccel = 200
n.yaccel = 1000
2017-06-25 13:06:49 +00:00
n.direction = "right"
2015-02-17 19:17:09 +00:00
n.stance = "fall"
2017-06-25 10:00:45 +00:00
n.type = "ninja"
2015-08-20 19:37:09 +00:00
n.DO_JUMP = 0
2017-06-25 12:36:36 +00:00
n.hit = 0
2015-02-17 19:17:09 +00:00
n.animations = {
stand = {
2015-02-23 00:14:37 +00:00
left = newAnimation(lutro.graphics.newImage(
2015-02-17 19:17:09 +00:00
"assets/ninja_stand_left.png"), 48, 48, 100, 10),
2015-02-23 00:14:37 +00:00
right = newAnimation(lutro.graphics.newImage(
2015-02-17 19:17:09 +00:00
"assets/ninja_stand_right.png"), 48, 48, 100, 10)
},
run = {
2015-02-23 00:14:37 +00:00
left = newAnimation(lutro.graphics.newImage(
2015-02-17 19:17:09 +00:00
"assets/ninja_run_left.png"), 48, 48, 1, 10),
2015-02-23 00:14:37 +00:00
right = newAnimation(lutro.graphics.newImage(
2015-02-17 19:17:09 +00:00
"assets/ninja_run_right.png"), 48, 48, 1, 10)
},
jump = {
2015-02-23 00:14:37 +00:00
left = newAnimation(lutro.graphics.newImage(
2015-02-17 19:17:09 +00:00
"assets/ninja_jump_left.png"), 48, 48, 1, 10),
2015-02-23 00:14:37 +00:00
right = newAnimation(lutro.graphics.newImage(
2015-02-17 19:17:09 +00:00
"assets/ninja_jump_right.png"), 48, 48, 1, 10)
},
fall = {
2015-02-23 00:14:37 +00:00
left = newAnimation(lutro.graphics.newImage(
2015-02-17 19:17:09 +00:00
"assets/ninja_fall_left.png"), 48, 48, 1, 10),
2015-02-23 00:14:37 +00:00
right = newAnimation(lutro.graphics.newImage(
2015-02-17 19:17:09 +00:00
"assets/ninja_fall_right.png"), 48, 48, 1, 10)
},
2017-06-25 08:16:29 +00:00
duck = {
left = newAnimation(lutro.graphics.newImage(
"assets/ninja_duck_left.png"), 48, 48, 1, 10),
right = newAnimation(lutro.graphics.newImage(
"assets/ninja_duck_right.png"), 48, 48, 1, 10)
},
2017-06-25 12:36:36 +00:00
hit = {
left = newAnimation(lutro.graphics.newImage(
"assets/ninja_hit_left.png"), 48, 48, 1, 10),
right = newAnimation(lutro.graphics.newImage(
"assets/ninja_hit_right.png"), 48, 48, 1, 10)
},
2015-02-17 19:17:09 +00:00
}
n.anim = n.animations[n.stance][n.direction]
2017-06-25 10:04:06 +00:00
2015-02-17 19:17:09 +00:00
return setmetatable(n, ninja)
end
function ninja:on_the_ground()
2017-06-25 21:56:38 +00:00
return (solid_at(self.x + 1, self.y + 32, self)
or solid_at(self.x + 15, self.y + 32, self))
and self.yspeed >= 0
2015-02-17 19:17:09 +00:00
end
function ninja:update(dt)
2015-08-20 19:37:09 +00:00
local JOY_LEFT = lutro.input.joypad("left")
2015-03-05 03:47:22 +00:00
local JOY_RIGHT = lutro.input.joypad("right")
2017-06-25 08:16:29 +00:00
local JOY_DOWN = lutro.input.joypad("down")
2015-08-20 19:37:09 +00:00
local JOY_A = lutro.input.joypad("a")
2015-02-17 19:17:09 +00:00
2017-06-25 12:36:36 +00:00
if self.hit > 0 then
self.hit = self.hit - 1
end
2015-02-17 19:17:09 +00:00
-- gravity
if not self:on_the_ground() then
self.yspeed = self.yspeed + self.yaccel * dt
self.y = self.y + dt * self.yspeed
end
-- jumping
2015-08-20 19:37:09 +00:00
if JOY_A then
self.DO_JUMP = self.DO_JUMP + 1
else
self.DO_JUMP = 0
end
if self.DO_JUMP == 1 and self:on_the_ground() then
self.y = self.y - 1
2017-06-25 21:56:38 +00:00
self.yspeed = -330
2017-06-25 10:04:06 +00:00
lutro.audio.play(sfx_jump)
2015-02-17 19:17:09 +00:00
end
-- moving
2015-08-20 19:37:09 +00:00
if JOY_LEFT then
2015-02-17 19:17:09 +00:00
self.xspeed = self.xspeed - self.xaccel * dt;
if self.xspeed < -200 then
self.xspeed = -200
end
self.direction = "left";
end
2015-08-20 19:37:09 +00:00
if JOY_RIGHT then
2015-02-17 19:17:09 +00:00
self.xspeed = self.xspeed + self.xaccel * dt;
if self.xspeed > 200 then
self.xspeed = 200
end
self.direction = "right";
end
-- apply speed
self.x = self.x + self.xspeed * dt;
-- decelerating
if not (JOY_RIGHT and self.xspeed > 0)
and not (JOY_LEFT and self.xspeed < 0)
2015-02-17 19:17:09 +00:00
and self:on_the_ground()
then
if self.xspeed > 0 then
self.xspeed = self.xspeed - 10
if self.xspeed < 0 then
self.xspeed = 0;
end
elseif self.xspeed < 0 then
self.xspeed = self.xspeed + 10;
if self.xspeed > 0 then
self.xspeed = 0;
end
end
end
-- animations
if self:on_the_ground() then
if self.xspeed == 0 then
self.stance = "stand"
else
self.stance = "run"
end
else
if self.yspeed > 0 then
self.stance = "fall"
else
self.stance = "jump"
end
end
2017-06-25 08:16:29 +00:00
if JOY_DOWN then
if self:on_the_ground() then
self.xspeed = 0
self.stance = "duck"
end
end
2017-06-25 12:36:36 +00:00
if self.hit > 0 then
self.stance = "hit"
end
2015-02-17 19:17:09 +00:00
local anim = self.animations[self.stance][self.direction]
-- always animate from first frame
if anim ~= self.anim then
anim.timer = 0
end
self.anim = anim;
self.anim:update(dt)
end
function ninja:draw()
self.anim:draw(self.x - 16, self.y - 16)
end
function ninja:on_collide(e1, e2, dx, dy)
2017-06-25 08:43:05 +00:00
if e2.type == "ground" then
if math.abs(dy) < math.abs(dx) and dy ~= 0 then
self.yspeed = 0
self.y = self.y + dy
2017-06-25 10:04:06 +00:00
lutro.audio.play(sfx_step)
2017-06-25 08:43:05 +00:00
end
if math.abs(dx) < math.abs(dy) and dx ~= 0 then
self.xspeed = 0
self.x = self.x + dx
end
2015-02-17 19:17:09 +00:00
2017-06-25 21:56:38 +00:00
elseif e2.type == "bridge" then
if math.abs(dy) < math.abs(dx) and dy ~= 0 and self.yspeed > 0.0 then
self.yspeed = 0
self.y = self.y + dy
lutro.audio.play(sfx_step)
end
2017-06-25 12:36:36 +00:00
elseif (e2.type == "obake" or e2.type == "porc") and self.hit == 0 then
2017-06-25 12:43:05 +00:00
lutro.audio.play(sfx_hit)
2017-06-25 12:36:36 +00:00
screen_shake = 0.25
self.hit = 60
if dx > 0 then
self.xspeed = 200
else
self.xspeed = -200
end
self.y = self.y - 1
self.yspeed = -1
2017-06-25 12:43:05 +00:00
hp = hp - 1
2017-06-25 12:36:36 +00:00
2015-02-17 19:17:09 +00:00
end
2015-03-05 03:47:22 +00:00
end