Blame view

game/asteroid.lua 1.82 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
  asteroid = class:new()

  

  function asteroid:init(x, y, size)

  	self.x = x or math.random(80)+10

  	self.y = y or -10

  	self.r = math.random(math.pi*20)/10

  	self.size = size or 1

  	self.rspeed = (math.random()*2-1)*10

  	self.speedx = math.random(5)+20

  	self.speedy = math.random(5)+10

  	self.i = math.random(2)

  	self.direction = math.random(2)

  	if self.direction == 2 then

  		self.direction = -1

  	end

  	

  	if self.size == 1 then

  		self.hp = 12*realasteroiddelay

  	else

  		self.hp = 6*realasteroiddelay

  	end

  end

  

  function asteroid:update(dt)

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

  	self.y = self.y + self.speedy*dt

  

  	if self.x > 90 then

  		self.direction = -1

  	elseif self.x < 10 then

  		self.direction = 1

  	end

  	

  	if self.y > 40 then

  		self.speedy = 100

  		self.direction = 0

  	end

  	

  	self.r = self.r + self.rspeed*dt

  	

  	if self.y > 120 then

  		self.dead = true

  	end

  	

  	return self.dead

  end

  

  function asteroid:hit()

  	self.hp = self.hp - 1

  	if self.hp <= 0 then

  		if self.size == 1 then

  			table.insert(asteroids, asteroid:new(self.x, self.y, 2))

  			table.insert(asteroids, asteroid:new(self.x, self.y, 2))

  			pointsget(10)

  		else

  			pointsget(10)

  		end

  		lastexplosion = {self.x, self.y}

  		self.dead = true

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

  		backgroundwhite = 1

  	end

  end

  

  function asteroid:checkcol(x, y)

  	if self.size == 1 then --big

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

  			return true

  		end

  	else

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

  			return true

  		end

  	end

  	return false

  end

  

  function asteroid:draw()

  	if self.size == 1 then

  		draw(_G["asteroid-big" .. self.i .. "img"], self.x, self.y, self.r, 1, 1, 11, 9)

  	else

  		draw(_G["asteroid-small" .. self.i .. "img"], self.x, self.y, self.r, 1, 1, 11, 9)

  	end

  end