-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquirrel.lua
More file actions
202 lines (154 loc) · 4.41 KB
/
squirrel.lua
File metadata and controls
202 lines (154 loc) · 4.41 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
module(..., package.seeall)
require "sprite"
local state = {};
local ready = false;
local shot = {};
local band_stretch = {};
local launched = false
local squirrelInstance
local decelleration = 5
local verticalDangerZone = 450
local boostOnTouch = 5
local initialX, initialY
local bullet = display.newGroup();
-- animation data
local squirrelSheetData = require "squirrelSheet"
local datat = squirrelSheetData.getSpriteSheetData()
local spriteSheet = sprite.newSpriteSheetFromData( "squirrelSheet.png", datat )
local animationSpriteSet = sprite.newSpriteSet(spriteSheet, 1, 2)
sprite.add( animationSpriteSet, "flying", 1, 2, 200, 0 )
-- graphics variables
flyingSpriteSet = sprite.newSprite( animationSpriteSet );
idleGraphics = display.newImage( "squirrelidle.png" )
loadingGraphics = display.newImage( "squirrelloading.png" )
deadGraphics = display.newImage( "squirreldead.png" )
function isLaunched()
return launched
end
function setLaunched( value )
launched = value
end
function removeListener()
Runtime:removeEventListener("touch", touchDown)
end
function reset()
setGraphics(0)
bullet.bodyType = "kinematic"
bullet.x = initialX
bullet.y = initialY
bullet:setLinearVelocity( 0, 0 )
launched = false
end
function setGraphics( number )
if( number == 0 ) then
--idle animation
flyingSpriteSet.isVisible = false
idleGraphics.isVisible = true
loadingGraphics.isVisible = false
deadGraphics.isVisible = false
end
if( number == 1 ) then
--loading animation
flyingSpriteSet.isVisible = false
idleGraphics.isVisible = false
loadingGraphics.isVisible = true
deadGraphics.isVisible = false
end
if( number == 2 ) then
--flying animation
flyingSpriteSet.isVisible = true
idleGraphics.isVisible = false
loadingGraphics.isVisible = false
deadGraphics.isVisible = false
flyingSpriteSet:play()
end
if( number == 3 ) then
--dead animation
flyingSpriteSet.isVisible = false
idleGraphics.isVisible = false
loadingGraphics.isVisible = false
deadGraphics.isVisible = true
end
end
function touchDown( event )
if ( event.phase == "began" and launched ) then
if squirrelInstance.y > verticalDangerZone then
-- if the squirrel is in the danger zone
-- handle the tap and make the squirrel glide
local velX, velY = squirrelInstance:getLinearVelocity()
if(velY<5) then
velY = velY + decelleration
else
velY = 5
end
squirrelInstance:setLinearVelocity( velX, velY )
else
-- otherwise the squirrel is not in the danger zone
-- so make him boost left or right
-- depending on touch posizion
if(event.x > display.contentWidth/2) then
squirrelInstance:applyLinearImpulse( boostOnTouch, 0, squirrelInstance.x, squirrelInstance.y )
else
local vx, vy = squirrelInstance:getLinearVelocity()
-- apply the impulse only if going right direction
if vx > 0 then
squirrelInstance:applyLinearImpulse( -boostOnTouch, 0, squirrelInstance.x, squirrelInstance.y )
end
end
end
end
end
function newSquirrel( _x, _y )
initialX = _x
initialY = _y
launched = false
-- Import easing plugin
local easingx = require("easing");
-- Bullet properties
local squirrel_bullet = {
name = "squirrel",
type = "bullet",
density = 0.1,
friction = 1.0,
bounce = 0.5,
size = 30,
rotation = 0
}
-- flying animation
flyingSpriteSet.isVisible = false;
bullet:insert( flyingSpriteSet )
-- idle image
bullet:insert( idleGraphics )
idleGraphics.x = 0
idleGraphics.y = 0
-- loading image
loadingGraphics.isVisible = false;
bullet:insert( loadingGraphics )
loadingGraphics.x = 0
loadingGraphics.y = 0
-- dead image
deadGraphics.isVisible = false;
bullet:insert( deadGraphics )
deadGraphics.x = 0
deadGraphics.y = 0
squirrelInstance = bullet;
bullet.name = "squirrel"
-- Place bullet
bullet.x = _x;
bullet.y = _y;
-- Set up physical properties
physics.addBody(bullet, "kinematic", {density=squirrel_bullet.density, friction=squirrel_bullet.friction, bounce=squirrel_bullet.bounce, radius=squirrel_bullet.size});
bullet.linearDamping = 0.0;
bullet.angularDamping = 0.0;
bullet.isBullet = true;
flyingSpriteSet:prepare("flying")
--flyingSpriteSet:prepare("dead")
--flyingSpriteSet:prepare("loading")
--flyingSpriteSet:prepare("idle")
--flyingSpriteSet:play()
Runtime:addEventListener("touch", touchDown )
return bullet;
end
function getSquirrelInstance()
return squirrelInstance
end