-
Notifications
You must be signed in to change notification settings - Fork 12
/
train.lua
66 lines (57 loc) · 1.44 KB
/
train.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Train = {}
Train.__index = Train
normal_train_quad = love.graphics.newQuad(0,0,132,36,256,256)
open_train_quad = love.graphics.newQuad(0,48,146,36,256,256)
inside_train_quad = love.graphics.newQuad(0,96,146,36,256,256)
coffee_cup_quad = love.graphics.newQuad(96,0,26,23,128,128)
TRAIN_MIN_SPEED = 160
TRAIN_MAX_SPEED = 200
function Train.createRandom()
if math.random(1,3) == 1 then
return Train.create(2)
else
return Train.create(1)
end
end
function Train.create(type)
local self = {}
setmetatable(self,Train)
self.speed = math.random(TRAIN_MIN_SPEED,TRAIN_MAX_SPEED)
self.x = WIDTH
self.y = 56
self.type = type
self.alive = true
self.hasCoffee = false
if self.type == 2 then
if math.random(2) == 1 then
self.hasCoffee = true
end
end
return self
end
function Train:update(dt)
if self.alive == false then
return
end
self.x = self.x - self.speed*dt*global_speed
if self.x < -146 then
self.alive = false
end
end
function Train:draw()
if self.alive == false then
return
end
if self.type == 1 then -- closed train
love.graphics.draw(imgTrains,normal_train_quad,self.x,self.y)
elseif self.type == 2 then -- open train
if pl.status == 3 then -- inside
love.graphics.draw(imgTrains,inside_train_quad,self.x-7,self.y)
if self.hasCoffee == true then
love.graphics.draw(imgSprites,coffee_cup_quad,self.x+54,self.y+8)
end
else -- outside
love.graphics.draw(imgTrains,open_train_quad,self.x-7,self.y)
end
end
end