More speed logic implemented and collision rotate effect

This commit is contained in:
SimonLarsen 2011-05-03 22:37:43 +02:00
parent 0a7257188f
commit d264ca269d
3 changed files with 38 additions and 19 deletions

View File

@ -13,6 +13,7 @@ track_quad = love.graphics.newQuad(0,48,121,5,128,128)
global_speed = 1.0
function love.load()
math.randomseed(os.time())
love.graphics.setMode(SCRNWIDTH,SCRNHEIGHT,false)
love.graphics.setBackgroundColor(255,255,255)
love.graphics.setLineWidth(2)
@ -24,7 +25,7 @@ function love.load()
track_frame = 0
nextCloud = 0
train = Train.create(10,1)
train = Train.create(10,math.random(1,2))
train.x = -190
end
@ -45,7 +46,7 @@ function love.update(dt)
Train.update(train,dt)
if train.x < -200 then
train.x = WIDTH
train.speed = global_speed*math.random(TRAIN_MIN_SPEED,TRAIN_MAX_SPEED)
train.speed = math.random(TRAIN_MIN_SPEED,TRAIN_MAX_SPEED)
train.type = math.random(1,2)
end
@ -69,13 +70,6 @@ function love.draw()
if v.speed < 37 then Cloud.draw(v) end
end
-- Draw train
Train.draw(train)
-- Draw player
love.graphics.setColor(255,255,255,255)
Player.draw(pl)
-- Draw foreground clouds
for i,v in ipairs(clouds) do
if v.speed >= 37 then Cloud.draw(v) end
@ -85,6 +79,13 @@ function love.draw()
for i=0,2 do
love.graphics.drawq(imgSprites,track_quad,i*121 - track_frame,92)
end
-- Draw train
Train.draw(train)
-- Draw player
love.graphics.setColor(255,255,255,255)
Player.draw(pl)
end
function loadResources()

View File

@ -4,11 +4,12 @@ JUMP_POWER = -300
GRAVITY = 1000
PLAYER_WIDTH = 14
PLAYER_HEIGHT = 21
PLAYER_START_X = 64
function Player.create()
local self = {}
self.frame = 0
self.x = 14
self.x = PLAYER_START_X
self.y = 71
self.yspeed = 0
self.onGround = true
@ -26,20 +27,30 @@ function Player.update(self,dt)
self.onGround = false
-- Update position
if self.status == 0 then
self.y = self.y + self.yspeed*dt
self.y = self.y + self.yspeed*dt
if self.status == 0 then -- normal ourside
self.yspeed = self.yspeed + dt*GRAVITY
if self.y > 71 then
self.y = 71
self.yspeed = 0
self.onGround = true
end
self.yspeed = self.yspeed + dt*GRAVITY
elseif self.status == 1 then
self.x = train.x-10
elseif self.status == 3 then
self.y = 66
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
end
-- Update walk frame
@ -52,7 +63,11 @@ end
function Player.draw(self)
local frame = 15*math.floor(self.frame)
local quad = love.graphics.newQuad(frame,0,15,21,128,128)
love.graphics.drawq(imgSprites,quad,self.x,self.y)
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
end
function Player.collideWithTrain(self)
@ -61,10 +76,13 @@ function Player.collideWithTrain(self)
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

View File

@ -16,7 +16,7 @@ function Train.create(speed,type)
end
function Train.update(self,dt)
self.x = self.x - self.speed*dt
self.x = self.x - self.speed*dt*global_speed
end
function Train.draw(self)