-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcannon.lua
More file actions
148 lines (88 loc) · 2.38 KB
/
cannon.lua
File metadata and controls
148 lines (88 loc) · 2.38 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
module (..., package.seeall)
local director = require ("director")
local projectile = require("projectile")
local physics = require( "physics" )
local globalLayer
local _H = display.contentHeight
local _W = display.contentWidth
local cannonInstance
local newAngle
local timerID
local canShoot = false
local delayToShoot = 300
local bulletLayer
local laserSound = audio.loadSound("laser.wav")
local canCreate = true
function removeAllProjectiles()
for i=1, bulletLayer.numChildren do
local child = bulletLayer[i]
if child then
if child.x < -200 or child.x > _W + 200 or child.y < 0 or child.y > _H then
bulletLayer:remove(i)
end
end
end
bulletLayer:removeSelf()
bulletLayer = display.newGroup()
globalLayer:insert(bulletLayer)
end
function checkOutOfWindow(event)
canCreate = false
for i=1, bulletLayer.numChildren do
local child = bulletLayer[i]
if child then
if child.x < -200 or child.x > _W + 200 or child.y < 0 or child.y > _H then
bulletLayer:remove(i)
end
end
end
canCreate = true
end
function new()
globalLayer = display.newGroup()
bulletLayer = display.newGroup()
globalLayer:insert(bulletLayer)
cannonInstance = display.newImage("cannon.png", true)
cannonInstance.name = "cannon"
cannonInstance.x = _W/2
cannonInstance.y = _H/2
--cannonInstance:scale(0.5, 0.5)
physics.addBody(cannonInstance, "kinematic", {density=0.1, friction=1.0, bounce=0.5, radius=50, isSensor=true});
globalLayer:insert(cannonInstance)
Runtime:addEventListener("enterFrame", checkOutOfWindow)
return globalLayer
end
function shoot()
canShoot = true
timerID = timer.performWithDelay(delayToShoot, shoot)
if canCreate then
audio.play( laserSound )
bulletLayer:insert(projectile.new(newAngle))
end
end
function startShooting()
if not canShoot then
shoot()
end
end
function stopShooting()
if timerID then
timer.cancel(timerID)
timerID = nil
canShoot = false
end
end
function setRotation(_x, _y)
local relativeX = cannonInstance.x - _x
local relativeY = cannonInstance.y - _y
-- fix for the left quadrant
if(_x <= _W/2) then
newAngle = math.deg( math.atan(relativeY/relativeX) + math.pi )
else
newAngle = math.deg( math.atan(relativeY/relativeX) )
end
cannonInstance.rotation = newAngle
end
function getRotation()
return cannonInstance.rotation
end