lutro-iyfct/player.lua

127 lines
2.5 KiB
Lua
Raw Normal View History

2011-04-24 01:51:32 +00:00
Player = {}
2011-04-24 22:38:00 +00:00
JUMP_POWER = -300
GRAVITY = 1000
2011-04-25 11:37:05 +00:00
PLAYER_WIDTH = 14
PLAYER_HEIGHT = 21
PLAYER_START_X = 64
2011-04-24 22:38:00 +00:00
2011-04-24 01:51:32 +00:00
function Player.create()
2011-04-24 22:38:00 +00:00
local self = {}
self.frame = 0
self.x = PLAYER_START_X
2011-04-24 22:38:00 +00:00
self.y = 71
self.yspeed = 0
self.onGround = true
2011-04-25 11:37:05 +00:00
self.status = 0
2011-04-24 22:38:00 +00:00
return self
2011-04-24 01:51:32 +00:00
end
function Player.update(self,dt)
2011-04-24 22:38:00 +00:00
-- Check keyboard input
if love.keyboard.isDown(' ') and self.onGround == true then
self.yspeed = JUMP_POWER
self.onGround = false
end
2011-04-25 11:37:05 +00:00
self.onGround = false
2011-04-24 22:38:00 +00:00
-- Update position
self.y = self.y + self.yspeed*dt
if self.status == 0 then -- normal ourside
self.yspeed = self.yspeed + dt*GRAVITY
2011-04-25 11:37:05 +00:00
if self.y > 71 then
self.y = 71
self.yspeed = 0
self.onGround = true
end
elseif self.status == 3 then -- inside train
self.yspeed = self.yspeed + dt*GRAVITY
if self.y > 66 then
self.y = 66
self.yspeed = 0
self.onGround = true
elseif self.y < 60 then
self.y = 60
self.yspeed = 0
end
elseif self.status == 1 then -- hit by train
self.x = self.x - dt*300
2011-04-24 22:38:00 +00:00
end
-- Update walk frame
2011-04-24 01:51:32 +00:00
self.frame = self.frame + 20*dt
if self.frame >= 6 then
self.frame = 0
end
end
function Player.draw(self)
local frame = 15*math.floor(self.frame)
local quad = love.graphics.newQuad(frame,0,15,21,128,128)
if self.status == 1 then
love.graphics.drawq(imgSprites,quad,self.x,self.y, -self.x/10, 1,1,7,10)
else
love.graphics.drawq(imgSprites,quad,self.x,self.y)
end
2011-04-24 01:51:32 +00:00
end
2011-04-25 11:37:05 +00:00
function Player.collideWithTrain(self)
2011-05-07 15:54:23 +00:00
if train.alive == false then
return
end
if self.status == 0 then
2011-04-25 11:37:05 +00:00
-- check collision with front of train
if Player.collideWithPoint(self,train.x+4,train.y+10) or
Player.collideWithPoint(self,train.x+2,train.y+24) then
if train.type == 1 then -- hit by closed train
print("Hit by train at global speed " .. global_speed)
self.status = 1 -- hit by train
self.yspeed = -100
if self.y < train.y-9 then
self.y = train.y-9
end
elseif train.type == 2 then -- hit by open train
self.status = 3
end
end
-- check if landed on train
if train.x < self.x and train.x+125 > self.x then
if self.y > 35 then
self.y = 35
self.yspeed = 0
self.onGround = true
2011-04-25 11:37:05 +00:00
end
end
end
if self.status == 3 then
if self.x > train.x+135 then
self.status = 0
2011-04-25 11:37:05 +00:00
end
end
end
function Player.collideWithPoint(self,x,y)
if x > self.x and x < self.x+PLAYER_WIDTH
and y > self.y and y < self.y+PLAYER_HEIGHT then
return true
else
return false
end
end
--[[ Status values:
0 = alive
1 = hit by train
2 = hit by bird
3 = inside train
4 = falling through ground
2011-04-25 11:37:05 +00:00
--]]