lutro-iyfct/player.lua

221 lines
4.4 KiB
Lua
Raw Normal View History

2011-04-24 01:51:32 +00:00
Player = {}
Player.__index = Player
2011-04-24 01:51:32 +00:00
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 = 54
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 = {}
setmetatable(self,Player)
2011-05-16 12:37:08 +00:00
self:reset()
return self
end
function Player:reset()
2011-04-24 22:38:00 +00:00
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-05-10 21:00:53 +00:00
self.alive = true
self.invul = true
self.invultime = 1
2011-04-24 01:51:32 +00:00
end
function Player:update(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
2011-05-10 19:05:41 +00:00
self.yspeed = self.yspeed + dt*GRAVITY
if self.status == 0 then -- normal ourside
self.y = self.y + self.yspeed*dt
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.y = self.y + self.yspeed*dt
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.y = self.y + self.yspeed*dt
self.x = self.x - dt*300
elseif self.status == 4 then -- falling through ground
self.y = self.y + 150*dt
if self.y > HEIGHT+10 then
scrn_shake = 0.25
auHit:stop() auHit:play()
self.status = 1
end
elseif self.status == 5 then -- hit by mountain
self.x = self.x - global_speed * dt * TRACK_SPEED * 1.5
2011-04-24 22:38:00 +00:00
end
-- Update walk frame
self.frame = (self.frame + 20*dt) % 6
-- Update invulnerability
if self.invultime > 0 then
self.invultime = self.invultime - dt
if self.invultime <= 0 then
self.invul = false
end
2011-04-24 01:51:32 +00:00
end
end
function Player:draw()
2011-04-24 01:51:32 +00:00
local frame = 15*math.floor(self.frame)
local quad = love.graphics.newQuad(frame,0,15,21,128,128)
if self.status == 0 then
if self.invul == false or math.floor(self.frame) % 2 == 0 then
love.graphics.drawq(imgSprites,quad,self.x,self.y)
end
elseif self.status == 1 or self.status == 5 then
love.graphics.drawq(imgSprites,quad,self.x,self.y, -self.x/10, 1,1,7,10)
else -- default case
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:kill(status)
if self.invul == true or self.alive == false then
return
end
if status ~= 4 then
scrn_shake = 0.25
auHit:stop() auHit:play()
end
if coffee >= 5 then
self.invul = true
self.invultime = 1
coffee = 0
else
self.alive = false
self.status = status
if status == 1 then
if self.yspeed > -100 then
self.yspeed = -100
end
end
end
score = math.floor(score)
if score > highscore[difficulty] then
highscore[difficulty] = score
end
end
function Player:collideWithTrain()
2011-05-15 22:41:50 +00:00
if train.alive == false then
2011-05-07 15:54:23 +00:00
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
self:kill(1)
elseif train.type == 2 then -- hit by open train
if self.yspeed >= 0 then
self.status = 3
else
self:kill(1)
end
end
-- check if landed on train
elseif train.x < self.x and train.x+125 > self.x and self.yspeed > 0 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 -- inside open train
if self.x > train.x+135 then
self.status = 0
elseif train.hasCoffee == true and
self:collideWithPoint(train.x+57,train.y+20) then
train.hasCoffee = false
coffee = coffee + 1
if coffee > 5 then coffee = 5 end
2011-05-17 19:03:40 +00:00
auCoffee:stop() auCoffee:play()
2011-04-25 11:37:05 +00:00
end
end
end
function Player:collideWithTunnel()
if tunnel.alive == false then
return
end
if self.status == 0 then
if self.y < 47 and self.x < tunnel.x and
self.x > tunnel.x-16 then
self:kill(5)
end
end
end
function Player:collideWithBirds()
2011-05-10 19:05:41 +00:00
for i,v in ipairs(birds) do
if Player.collideWithPoint(self,v.x+5.5,v.y+5) then
self:kill(1)
2011-05-10 19:05:41 +00:00
return
end
end
end
function Player:collideWithGorge()
if self.y >= 71 and self.x > gorge.x+2 and self.x < gorge.x+92 then
self:kill(4)
end
end
function Player:collideWithPoint(x,y)
2011-04-25 11:37:05 +00:00
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?
5 = hit by mountain
2011-04-25 11:37:05 +00:00
--]]