Blame view

game/rocket.lua 5.42 KB
ca0e8212   Geoffrey PREUD'HOMME   Code original
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
  rocket = class:new()

  

  rocketspeed = 300

  maxrocketspeed = 100

  

  function rocket:init()

  	self.x = 115

  	self.y = 60

  	self.r = 0

  	self.speedx = 0

  	self.ground = true

  	self.startingoffset = 0

  	self.thrusttimer = 0

  	thrusts = {}

  	machinegundelay = 0

  	self.explosiondelay = 0

  	self.explosiontimer = 0

  end

  

  function rocket:update(dt)

  	if self.ground then

  		if self.x > 50 then

  			self.x = self.x - dt*50

  			if self.x < 50 then

  				self.x = 50

  			end

  		end

  	else

  		self.startingoffset = (math.random()*2-1)*0.5

  	end

  	

  	if self.starting then

  		self.startingtimer = self.startingtimer + dt

  		self.startingoffset = (math.random()*2-1)*2

  		

  		if self.startingtimer > 2 then

  			self.y = self.y - dt*5

  			self:thrusts(dt)

  		end

  		

  		if self.startingtimer > 7 then

  			self.y = self.y + dt*50

  			

  			if self.y > 70 then

  				self.starting = false

  				self.y = 70

  				changegamestate("scene3")

  				self.inflight = true

  			end

  		end

  	end

  	

  	if self.inflight then

  		if love.keyboard.isDown("right") and self.x < 100 then

  			self.speedx = self.speedx + dt*rocketspeed

  		elseif self.speedx > 0 then

  			self.speedx = math.max(0, self.speedx - dt*rocketspeed)

  		end

  		

  		if love.keyboard.isDown("left") and self.x > 0 then

  			self.speedx = self.speedx - dt*rocketspeed

  		elseif self.speedx < 0 then

  			self.speedx = math.min(0, self.speedx + dt*rocketspeed)

  		end

  		

  		self.speedx = math.min(maxrocketspeed, math.max(-maxrocketspeed, self.speedx))

  	

  		self.x = self.x + self.speedx*dt

  		if self.x > 100 then

  			self.x = 100

  		elseif self.x < 0 then

  			self.x = 0

  		end

  			

  		self.r = self.speedx/200

  	

  		if #pressedkeys > 0 then

  			machinegundelay = machinegundelay + dt

  			while machinegundelay > machinedelay do

  				self:fire()

  				machinegundelay = machinegundelay - machinedelay

  			end

  		end

  		

  		if wheatleytimer then

  			wheatleytimer = wheatleytimer + dt*2

  			wheatleyr = wheatleyr + dt*10

  			

  			if wheatleytimer >= 0.95 and wheatleytimer - dt*2 < 0.95 then

  				table.insert(explosions, explosion:new(self.x-20, self.y-20))

  				rocketimg = love.graphics.newImage("graphics/rocketkaputt.png")

  				changegamestate("scene4")

  				self.hit = true

  				self.inflight = false

  			end

  		end

  	end

  	shakeamount = self.startingoffset*3

  	

  	if self.hit then

  		wheatleytimer = wheatleytimer + dt*2

  		wheatleyr = wheatleyr + dt*10

  		if wheatleytimer < 4 then

  			if self.r > 0 then

  				self.r = self.r - dt

  			else

  				self.r = self.r + dt

  			end

  		else

  			if self.r < math.pi*0.9 then

  				self.r = self.r + dt

  				starmover = -self.r

  				self.y = self.y - dt*20

  				if self.x > 10 then

  					self.x = self.x - dt*10

  				elseif self.x < 10 then

  					self.x = self.x + dt*10

  				end

  			end

  		end

  		

  		self.explosiontimer = self.explosiontimer + dt

  		while self.explosiontimer > self.explosiondelay do

  			self.explosiontimer = self.explosiontimer - self.explosiondelay

  			self.explosiondelay = math.random(100)/300+0.1

  			table.insert(explosions, explosion:new(self.x-16+math.random(16)-8, self.y-20+math.random(16)-8))

  		end

  		self.startingoffset = self.startingoffset*10

  	end

  	

  	--THRUSTS

  	local delete = {}

  	

  	for i, v in pairs(thrusts) do

  		if v:update(dt) == true then

  			table.insert(delete, i)

  		end

  	end

  	

  	table.sort(delete, function(a,b) return a>b end)

  	

  	for i, v in pairs(delete) do

  		table.remove(thrusts, v) --remove

  	end

  end

  

  function rocket:fire()

  	local outx = -math.sin(self.r) * -14 + self.x

  	local outy = math.cos(self.r) * -14 + self.y

  	

  	table.insert(bullets, bullet:new(outx, outy, self.r))

  end

  

  function rocket:thrusts(dt) --if you know what I mean

  	self.thrusttimer = self.thrusttimer + dt

  	local delay = 0.01

  	while self.thrusttimer > delay do

  		self.thrusttimer = self.thrusttimer - delay

  		table.insert(thrusts, thrust:new(self.x-6, self.y+10))

  		table.insert(thrusts, thrust:new(self.x, self.y+10))

  		table.insert(thrusts, thrust:new(self.x+6, self.y+10))

  	end

  end

  

  function rocket:start()

  	self.starting = true

  	self.ground = false

  	self.startingtimer = 0

  end

  

  function rocket:checkcol(x, y)

  	if math.abs(self.x-x) < 16 and math.abs(self.y-y) < 16 then

  		return true

  	end

  	return false

  end

  

  function rocket:draw()

  	local r, g, b = love.graphics.getColor()

  	for i, v in pairs(thrusts) do

  		v:draw()

  	end

  	love.graphics.setColor(r, g, b)

  	draw(rocketimg, self.x+self.startingoffset, self.y, self.r, 1, 1, 15, 16)

  	

  	if wheatleytimer then

  		if wheatleytimer <= 0.95 then

  			draw(wheatleyimg, self.x-20*(1-wheatleytimer/1), self.y-120*(1-wheatleytimer/1), wheatleyr, 1, 1, 8, 9)

  		else

  			draw(wheatleyimg, self.x-20*(1-0.95/1)-(wheatleytimer-0.95)*200, self.y-120*(1-wheatleytimer/1)+(wheatleytimer-0.95)*20, wheatleyr, 1, 1, 8, 9)

  		end

  	end

  end

  

  function rocket:wheatleyattack()

  	wheatleytimer = 0

  	wheatleyr = 0

  	space:play()

  end

  

  --THRUST

  

  thrust = class:new()

  

  function thrust:init(x, y)

  	self.x = x

  	self.y = y

  	self.starty = rockets[1].y

  	self.dir = (math.random()*2-1)*0.4+math.pi/2 + rockets[1].r

  	self.speed = 40

  	self.life = 1

  end

  

  function thrust:update(dt)

  	self.x = self.x + math.cos(self.dir)*self.speed*dt

  	self.y = self.y + math.sin(self.dir)*self.speed*dt

  	

  	self.life = self.life - dt

  	if self.life <= 0 then

  		return true

  	end

  end

  

  function thrust:draw()

  	love.graphics.setColor(255, 255, 255, 255*self.life)

  	love.graphics.rectangle("fill", self.x*scale, self.y*scale+(rockets[1].y-self.starty)*scale, scale, scale)

  end