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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
CrystalPlayer = Fill:extend{
fill = {255,0,0},
height = 32,
width = 32,
onStartFrame = function(self)
if the.mouse:pressed() then
print('hi')
end
local dx = love.mouse.getX() - the.app.width / 2
local dy = love.mouse.getY() - the.app.height / 2
self.acceleration.x = dx * 10
self.acceleration.y = dy * 10
--print(dx, dy)
love.mouse.setPosition(the.app.width /2, the.app.height/2)
end
}
local velLimit = 300
--local velLimit = 50
SpacePlayer = Tile:extend{
image = 'data/ship.png',
maxVelocity = {x=velLimit,y=velLimit},
minVelocity = {x=-velLimit, y=-velLimit},
lastFired = 0,
money = 2000, -- what to use for money symbol? ✪ or ☥ or Ⓐ?
goods = {},
cargoSpace = 20,
onPlanet = false,
kills = 0,
onNew = function(self)
self.thrust = Tile:new{image = 'data/thrust.png'}
self.shield = Shield:new{ship = self}
end,
onStartFrame = function(self)
--- TAKE 2
local mouseVec = vector.new(
love.mouse.getX() - the.app.width / 2,
love.mouse.getY() - the.app.height / 2
)
self.rotation = math.atan2(mouseVec.y, mouseVec.x)
if mouseVec:len2() > 64^2 then
self.acceleration = vector.new(300, 0)
self.acceleration:rotate_inplace(self.rotation)
self.thrust.visible = true
else
self.acceleration = {x=0, y=0}
self.thrust.visible = false
if DEBUG and the.console.visible then
self.velocity = {x=0, y=0}
end
end
if the.mouse:pressed() and -- assumes left button
love.timer.getTime() - self.lastFired > 0.25 and
the.cursor:inTargetArea() then
--print('pew')
b = Bullet:new{
x = self.x + self.width / 2,
y = self.y + self.height / 2,
rotation = self.rotation,
source = self
}
self.lastFired = love.timer.getTime()
end
if the.keys:justPressed('x') then
--self:doHit()
end
end,
onUpdate = function(self)
self.thrust.x = self.x - self.width
self.thrust.y = self.y
self.thrust.rotation = self.rotation
end,
onEndFrame = function(self)
self:collide(the.rockColliders)
end,
doHit = function(self)
if self.shield.strength == 0 then
-- die
the.app.view:add( Boom:new {
x = self.x,
y = self.y,
velocity = { rotation = 5 }
})
self:die()
self.thrust:die()
else
self.shield:onHit()
end
if not self.active then
the.interface:add(GameOver:new())
end
end,
onCollide = function(self, other)
if other:instanceOf(Bullet) and other.source ~= self then
self:doHit()
other:die()
the.bullets:remove(other)
end
if other:instanceOf(RockCollider) then
the.app.view:add( Boom:new {
x = self.x,
y = self.y,
velocity = { rotation = 5 }
})
self:die()
self.thrust:die()
end
end,
buy = function(self, good, price)
if self.money >= price and self:availableSpace() > 0 then
self.goods[good] = (self.goods[good] or 0) + 1
self.money = self.money - price
end
end,
sell = function(self, good, price)
if self.goods[good] and self.goods[good] > 0 then
self.goods[good] = self.goods[good] - 1
self.money = self.money + price
end
end,
availableSpace = function(self)
local cargo = 0
for _, amt in pairs(self.goods) do
cargo = cargo + amt
end
return self.cargoSpace - cargo
end,
save = function(self)
-- save player data
the.storage.data.player = {x = self.x,
y = self.y,
money = self.money,
goods = self.goods,
cargoSpace = self.cargoSpace
}
the.storage:save()
end
}
|