-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
272 lines (228 loc) · 6.59 KB
/
Copy pathmain.lua
File metadata and controls
272 lines (228 loc) · 6.59 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
local bump = require 'bump'
local pprint = require 'pprint'
-- Initialize Bump
local world = bump.newWorld()
X_SCALE = 0.5
Y_SCALE = 0.5
HIGH_FIVE_THRUST = 2000
GRAVITY = 4750
emitters = {}
JedMonster = {}
function JedMonster:new (o, x, y, speed, img)
o = o or {}
setmetatable(o, self)
self.__index = self
self.x = x or 600
self.y = y or 100
self.speed = speed or 150
self.img = love.graphics.newImage("assets/jed.jpeg")
return o
end
function JedMonster:move(dx, dy)
self.x, self.y, cols, len = world:move(self, self.x + dx, self.y + dy)
end
JedHand = {}
function JedHand:new(o, x, y, speed)
local o = o or {}
setmetatable(o, self)
self.__index = self
self.x = x or 500
self.y = y or 100
self.speed = speed or 150
self.img = love.graphics.newImage("assets/highfive_right.png")
self.x_velocity = 0
self.active_collisions = false
return o
end
function JedHand:move(dx, dy)
self.x, self.y, cols, len = world:move(self, self.x + dx, self.y + dy)
if len > 0 then
slap:play()
for _, col in ipairs(cols) do
if self.active_collisions then
print("have collided with " .. tostring(col.other))
else
print("have not collided with " .. tostring(col.other))
local emitter = JedEmitter:new(nil, col["touch"].x, col["touch"].y)
self.active_collisions = true
table.insert(emitters, emitter)
end
end
else
self.active_collisions = false
end
end
function JedHand:addCollision(item)
self.active_collisions[item] = true
end
function JedHand:removeCollision(item)
self.active_collisions[item] = false
end
function JedHand:isCollided(item)
return self.active_collisions[item] ~= nil
end
Player = {}
function Player:new(o, x, y, score)
o = o or {}
setmetatable(o, self)
self.__index = self
self.x = x or 25
self.y = y or 200
self.speed = speed or 250
self.score = score or 100
self.img = love.graphics.newImage("assets/jed.jpeg")
return o
end
function Player:move(dx, dy)
self.x, self.y, cols, len = world:move(self, self.x + dx, self.y + dy)
end
PlayerHand = {}
function PlayerHand:new(o, x, y, speed)
o = o or {}
setmetatable(o, self)
self.__index = self
self.x = x or 200
self.y = y or 230
self.speed = speed or 250
self.img = love.graphics.newImage("assets/highfive_left.png")
self.x_velocity = 0
return o
end
function PlayerHand:move(dx, dy)
self.x, self.y, cols, len = world:move(self, self.x + dx, self.y + dy)
end
JedEmitter = { x = 0, y = 0 }
function JedEmitter:new (o,x,y)
o = o or {}
setmetatable(o, self)
self.__index = self
self.x = x or math.random(800)
self.y = y or math.random(600)
local img = love.graphics.newImage('assets/jed.jpeg')
self.emitter = love.graphics.newParticleSystem(img, 32)
self.emitter:setAreaSpread("none",0,0)
self.emitter:setColors(255, 255, 255, 255, 255, 255, 255, 0)
self.emitter:setDirection(-1.6)
self.emitter:setEmissionRate(300)
self.emitter:setEmitterLifetime(1.1)
self.emitter:setLinearAcceleration(-500, 5000, 500, 0)
self.emitter:setOffset(16, 16)
self.emitter:setParticleLifetime(1, 3)
self.emitter:setSizeVariation(0)
self.emitter:setSizes(1)
self.emitter:setSpeed(1500, 0)
self.emitter:setSpread(4)
return o
end
function love.load(dt)
-- Set Initial Background Color
love.graphics.setBackgroundColor(106, 239, 242)
-- Instantiate Player
player = Player:new()
world:add(player, player.x, player.y, player.img:getWidth(), player.img:getHeight())
-- Instantiate Player Hand
player_hand = PlayerHand:new()
world:add(player_hand, player_hand.x, player_hand.y, player_hand.img:getWidth(), player_hand.img:getHeight())
-- Instantiate the Jeds and the Jed Hands
jeds = {}
hands = {}
starting = 100
for i=1, 5 do
local jed = JedMonster:new()
jed.y = starting
jeds[i] = jed
local hand = JedHand:new()
hand.y = starting
hands[i] = hand
starting = starting - 200
world:add(jeds[i], jeds[i].x, jeds[i].y, jeds[i].img:getWidth(), jeds[i].img:getHeight())
world:add(hands[i], hands[i].x, hands[i].y, hands[i].img:getWidth(), hands[i].img:getHeight())
end
-- Get Window Height
windowHeight = love.graphics.getHeight()
-- Jed Particle System
-- Load Sound Effects
slap = love.audio.newSource("assets/slap.mp3", "static")
end
function love.draw(dt)
-- Draw the Player
love.graphics.draw(player.img, player.x, player.y)
-- Draw the Jeds
for _, jed in ipairs(jeds) do
love.graphics.draw(jed.img, jed.x, jed.y)
end
-- Draw the Jed Hands
for _, hand in ipairs(hands) do
love.graphics.draw(hand.img, hand.x, hand.y)
end
-- Draw Player Hand
love.graphics.draw(player_hand.img, player_hand.x, player_hand.y)
-- Draw Jed Particles
for i, emitter in ipairs(emitters) do
love.graphics.draw(emitter.emitter, emitter.x, emitter.y, 0, 0.1, 0.1)
print(i, emitter)
if not emitter.emitter:isActive() then
table.remove(emitters, i)
end
end
end
function love.update(dt)
-- Update the Player Position
if love.keyboard.isDown('w', 'up') then
player:move(0, -(player.speed * dt))
player_hand:move(0, -(player_hand.speed * dt))
elseif love.keyboard.isDown('s', 'down') then
player:move(0, player.speed * dt)
player_hand:move(0, player_hand.speed * dt)
end
-- Start High Five
if love.keyboard.isDown('space') then
if player_hand.x_velocity == 0 then
player_hand.x_velocity = HIGH_FIVE_THRUST
end
end
-- Update High Five
if player_hand.x_velocity ~= 0 then
player_hand:move(player_hand.x_velocity * dt, 0)
player_hand.x_velocity = player_hand.x_velocity - (GRAVITY * dt)
end
-- Reset Player Hand after High Five
if player_hand.x < 200 then
player_hand.x_velocity = 0
end
-- Update the position of the Jeds
for _, jed in ipairs(jeds) do
jed:move(0, jed.speed * dt)
if jed.y > 600 then
jed.y = -400
world:update(jed, jed.x, jed.y)
end
end
-- Update the position of the Jed Hands
for _, hand in ipairs(hands) do
hand:move(0, hand.speed * dt)
-- Make them high five maybe
if hand.x_velocity == 0 then
if math.random(10) % 2 == 0 then
hand.x_velocity = -HIGH_FIVE_THRUST
end
end
-- Update High Five
if hand.x_velocity ~= 0 then
hand:move(hand.x_velocity * dt, 0)
hand.x_velocity = hand.x_velocity + (GRAVITY * dt)
end
-- Reset Jed Hand after High Five
if hand.x > 500 then
hand.x_velocity = 0
end
if hand.y > 600 then
hand.y = -400
world:update(hand, hand.x, hand.y)
end
end
-- Update Jed Particles
for _, emitter in ipairs(emitters) do
emitter.emitter:update(dt)
end
end