2015-02-17 19:17:09 +00:00
|
|
|
require "collisions"
|
|
|
|
|
|
|
|
local ninja = {}
|
|
|
|
ninja.__index = ninja
|
|
|
|
|
|
|
|
function newNinja()
|
|
|
|
local n = {}
|
|
|
|
n.width = 16
|
|
|
|
n.height = 32
|
|
|
|
n.xspeed = 0
|
|
|
|
n.yspeed = 0
|
|
|
|
n.xaccel = 200
|
|
|
|
n.yaccel = 1000
|
|
|
|
n.x = (SCREEN_WIDTH - n.width) / 2
|
|
|
|
n.y = (SCREEN_HEIGHT - n.height) / 2
|
|
|
|
n.direction = "left"
|
|
|
|
n.stance = "fall"
|
2015-08-20 19:37:09 +00:00
|
|
|
n.DO_JUMP = 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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
n.anim = n.animations[n.stance][n.direction]
|
2015-02-21 22:55:39 +00:00
|
|
|
n.sfx = {
|
2015-02-23 00:14:37 +00:00
|
|
|
jump = lutro.audio.newSource("assets/jump.wav"),
|
|
|
|
step = lutro.audio.newSource("assets/step.wav")
|
2015-02-21 22:55:39 +00:00
|
|
|
}
|
2015-02-17 19:17:09 +00:00
|
|
|
return setmetatable(n, ninja)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ninja:on_the_ground()
|
2015-08-20 19:37:09 +00:00
|
|
|
return solid_at(self.x + 1, self.y + 32, self)
|
2015-02-17 19:17:09 +00:00
|
|
|
or solid_at(self.x + 15, self.y + 32, self)
|
|
|
|
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")
|
2015-08-20 19:37:09 +00:00
|
|
|
local JOY_A = lutro.input.joypad("a")
|
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
|
|
|
|
self.yspeed = -300
|
|
|
|
lutro.audio.play(self.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
|
2015-08-23 19:21:05 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
-- camera
|
2015-08-21 04:21:36 +00:00
|
|
|
camera_x = - self.x + SCREEN_WIDTH/2 - self.width/2;
|
|
|
|
if camera_x > 0 then
|
|
|
|
camera_x = 0
|
2015-02-17 19:17:09 +00:00
|
|
|
end
|
2015-08-21 04:21:36 +00:00
|
|
|
if camera_x < -(map.width * map.tilewidth) + SCREEN_WIDTH then
|
|
|
|
camera_x = -(map.width * map.tilewidth) + SCREEN_WIDTH
|
2015-02-17 19:17:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ninja:draw()
|
|
|
|
self.anim:draw(self.x - 16, self.y - 16)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ninja:on_collide(e1, e2, dx, dy)
|
|
|
|
if math.abs(dy) < math.abs(dx) and dy ~= 0 then
|
|
|
|
self.yspeed = 0
|
|
|
|
self.y = self.y + dy
|
2015-02-21 22:55:39 +00:00
|
|
|
lutro.audio.play(self.sfx.step)
|
2015-02-17 19:17:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if math.abs(dx) < math.abs(dy) and dx ~= 0 then
|
|
|
|
self.xspeed = 0
|
|
|
|
self.x = self.x + dx
|
|
|
|
end
|
2015-03-05 03:47:22 +00:00
|
|
|
end
|