Include obake npc

This commit is contained in:
Jean-André Santoni 2017-06-25 10:43:05 +02:00
parent ef16c2eecc
commit e99d82a372
6 changed files with 79 additions and 9 deletions

View File

Before

Width:  |  Height:  |  Size: 601 B

After

Width:  |  Height:  |  Size: 601 B

View File

Before

Width:  |  Height:  |  Size: 604 B

After

Width:  |  Height:  |  Size: 604 B

View File

@ -44,6 +44,7 @@ function solid_at(x, y, exclude)
if x >= e.x and x < e.x + e.width
and y >= e.y and y < e.y + e.height
and e.type == "ground"
and e ~= exclude
then
return true;

View File

@ -2,6 +2,7 @@ require "global"
require "tiled"
require "anim"
require "ninja"
require "obake"
function lutro.conf(t)
t.width = SCREEN_WIDTH
@ -25,7 +26,9 @@ function lutro.load()
lutro.graphics.setFont(font)
map = tiled_load("assets/pagode.json")
tiled_load_objects(map, add_entity_from_map)
table.insert(entities, newNinja())
ninja = newNinja()
table.insert(entities, ninja)
table.insert(entities, newObake())
end
function lutro.update(dt)

View File

@ -172,14 +172,18 @@ function ninja:draw()
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
lutro.audio.play(self.sfx.step)
end
if e2.type == "ground" then
if math.abs(dy) < math.abs(dx) and dy ~= 0 then
self.yspeed = 0
self.y = self.y + dy
lutro.audio.play(self.sfx.step)
end
if math.abs(dx) < math.abs(dy) and dx ~= 0 then
self.xspeed = 0
self.x = self.x + dx
end
if math.abs(dx) < math.abs(dy) and dx ~= 0 then
self.xspeed = 0
self.x = self.x + dx
end
end

62
obake.lua Normal file
View File

@ -0,0 +1,62 @@
require "collisions"
local obake = {}
obake.__index = obake
function newObake()
local n = {}
n.width = 16
n.height = 16
n.xspeed = 0
n.yspeed = 0
n.x = (SCREEN_WIDTH - n.width)
n.y = 180
n.direction = "left"
n.stance = "fly"
n.t = 0
n.animations = {
fly = {
left = newAnimation(lutro.graphics.newImage(
"assets/obake_fly_left.png"), 48, 48, 100, 10),
right = newAnimation(lutro.graphics.newImage(
"assets/obake_fly_right.png"), 48, 48, 100, 10)
},
}
n.anim = n.animations[n.stance][n.direction]
return setmetatable(n, obake)
end
function obake:update(dt)
self.t = self.t + dt
-- apply speed
self.x = self.x + self.xspeed * dt;
self.x = self.x + math.cos(self.t/2.0) / 2.0
self.y = self.y + math.cos(self.t*2.0) / 4.0
if self.x > ninja.x then
self.direction = "left"
else
self.direction = "right"
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)
end
function obake:draw()
self.anim:draw(self.x - 16, self.y - 16)
end
function obake:on_collide(e1, e2, dx, dy)
end