-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
455 lines (351 loc) · 13.4 KB
/
main.lua
File metadata and controls
455 lines (351 loc) · 13.4 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
-- Project: Ant project.
-- Copyright (C) 2010 ANSCA Inc. All Rights Reserved.
---------------------------------------------------------------------------------------
local sprite = require( "sprite" )
local physics = require( "physics" )
require 'middleclass'
local BoidBase = require( "BoidBase" )
local Boid = require('Boid')
-------------------------------------
--Initialization
display.setStatusBar(display.HiddenStatusBar)
local background = display.newImage("background.png")
background:setReferencePoint(display.TopLeftReferencePoint)
physics.start()
-- No gravity
physics.setGravity( 0, 0 )
------------------------------------
-- Local/Global For Ant moving around the screen
local touch2down = false
local radius = 10
local xdirection = 1
local ydirection = 1
local xspeed = 1.5
local yspeed = 1.4
local xpos = display.contentWidth * 0.5
local ypos = display.contentHeight * 0.5
-- Get current edges of visible screen (accounting for the areas cropped by "zoomEven" scaling mode in config.lua)
local screenTop = display.screenOriginY
local screenBottom = display.viewableContentHeight + display.screenOriginY
local screenLeft = display.screenOriginX
local screenRight = display.viewableContentWidth + display.screenOriginX
-- Background screens initialization
local moon = display.newImage("moon.png", 22, 19)
local mountain_big = display.newImage("mountain_big.png", 132-240, 92)
local mountain_big2 = display.newImage("mountain_big.png", 132-720, 92)
local mountain_sma = display.newImage("mountain_small.png", 84, 111)
local mountain_sma2 = display.newImage("mountain_small.png", 84 - 480, 111)
local tree_s = display.newImage("tree_s.png", 129-30, 151)
local tree_s2 = display.newImage("tree_s.png", 270 + 10,151)
local tree_l = display.newImage("tree_l.png", 145, 131)
local tree_s3 = display.newImage("tree_s.png", 129-30 - 320, 151)
local tree_s4 = display.newImage("tree_s.png", 270 + 10 - 320,151)
local tree_l2 = display.newImage("tree_l.png", 145 - 320, 131)
local tree_s5 = display.newImage("tree_s.png", 129 - 30 - 640, 151)
local tree_s6 = display.newImage("tree_s.png", 270 + 10 - 640,151)
local tree_l3 = display.newImage("tree_l.png", 145 - 640, 131)
local fog = display.newImage("Fog.png", 0, 214)
local fog2 = display.newImage("Fog.png",-480,214)
background.x = 0
background.y = 0
local tree_l_sugi = display.newImage("tree_l_sugi.png", 23, 0)
local tree_l_take = display.newImage("tree_l_take.png", 151, 0)
-- local rakkan = display.newImage("rakkan.png", 19, 217)
-- local rakkann = display.newImage("rakkann.png", 450, 11)
local wallOutSide = display.newRect(480,0,200,320)
wallOutSide:setFillColor(0,0,0)
local wallOutSide2 = display.newRect(-200,0,200,320)
wallOutSide2:setFillColor(0,0,0)
local tPrevious = system.getTimer()
------ Test Boid constants
-- Define some limiting constants
MAX_FORCE = 3.5
MAX_SPEED =2.0
-- Start our wanderer in the center
local wanderers = {}
-- Test Ant sprite - texturepacker
local texturepacker = sprite.newSpriteSheetFromData( "texturepacker.png", require("texturepacker").getSpriteSheetData() )
local spriteSet = sprite.newSpriteSet(texturepacker,1,6)
sprite.add(spriteSet,"texturepacker",1,6,1000,0)
local spriteInstance = sprite.newSprite(spriteSet)
spriteInstance:setReferencePoint(display.BottomRightReferencePoint)
spriteInstance.x = 480
spriteInstance.y = 320
spriteInstance:scale(.5,.5)
-- Create spriteInstance2 - Walking Ant
local spriteInstance2 = sprite.newSprite(spriteSet)
spriteInstance2:setReferencePoint(display.BottomRightReferencePoint)
spriteInstance2.x = 200
spriteInstance2.y = 200
spriteInstance2:scale(.5,.5)
-- Create spriteInstance3 - Stationary Ant that can be dragged.
local spriteInstance3 = sprite.newSprite(spriteSet)
spriteInstance3:scale(.5,.5)
-- spriteInstance3:setReferencePoint(display.BottomRightReferencePoint)
-- spriteInstance3.x = 100
-- spriteInstance3.y = 100
-- Add Ants to physics model
--spriteInstance.myName="Ant-1"
--physics.addBody( spriteInstance, "static", { friction=0.5} )
--spriteInstance2.myName="Ant-2"
--physics.addBody( spriteInstance2, { density=1.0, friction=0.1, bounce=.5 } )
spriteInstance3.myName="Ant-3"
physics.addBody(spriteInstance3, "kinematic",
{ friction=0.0, bounce=0.0, density=0.0, radius=spriteInstance3.contentWidth/2.0 }
)
-- Angle functions
function angleBetween ( srcObj, dstObj )
local xDist = dstObj.x-srcObj.x ; local yDist = dstObj.y-srcObj.y
local angleBetween = math.deg( math.atan( yDist/xDist ) )
if ( srcObj.x < dstObj.x ) then angleBetween = angleBetween+90 else angleBetween = angleBetween-90 end
return angleBetween - 90
end
function angleBetweenPoints ( srcObjx, srcObjy, dstObjx, dstObjy)
local xDist = dstObjx-srcObjx ; local yDist = dstObjy-srcObjy
local angleBetweenPoints = math.deg( math.atan( yDist/xDist ) )
if ( srcObjx < dstObjx ) then angleBetweenPoints = angleBetweenPoints + 90 else angleBetweenPoints = angleBetweenPoints - 90 end
return angleBetweenPoints - 90
end
------------------------
--Events
------------------------
local function move(event)
local tDelta = event.time - tPrevious
tPrevious = event.time
local xOffset = ( 0.2 * tDelta )
moon.x = moon.x + xOffset*0.05
fog.x = fog.x + xOffset
fog2.x = fog2.x + xOffset
mountain_big.x = mountain_big.x + xOffset*0.5
mountain_big2.x = mountain_big2.x + xOffset*0.5
mountain_sma.x = mountain_sma.x + xOffset*0.5
mountain_sma2.x = mountain_sma2.x + xOffset*0.5
tree_s.x = tree_s.x + xOffset
tree_s2.x = tree_s2.x + xOffset
tree_l.x = tree_l.x + xOffset
tree_s3.x = tree_s3.x + xOffset
tree_s4.x = tree_s4.x + xOffset
tree_l2.x = tree_l2.x + xOffset
tree_s5.x = tree_s5.x + xOffset
tree_s6.x = tree_s6.x + xOffset
tree_l3.x = tree_l3.x + xOffset
tree_l_sugi.x = tree_l_sugi.x + xOffset * 1.5
tree_l_take.x = tree_l_take.x + xOffset * 1.5
if moon.x > 480 + moon.width / 2 then
moon:translate ( -480*2 , 0)
end
if fog.x > 480 + fog.width / 2 then
fog:translate( -480 * 2, 0)
end
if fog2.x > 480 + fog2.width / 2 then
fog2:translate( -480 * 2, 0)
end
if mountain_big.x > 480 + mountain_big.width / 2 then
mountain_big:translate(-480*2 , 0)
end
if mountain_big2.x > 480 + mountain_big2.width / 2 then
mountain_big2:translate(-480*2 , 0)
end
if mountain_sma.x > 480 + mountain_sma.width / 2 then
mountain_sma:translate(-480*2,0)
end
if mountain_sma2.x > 480 + mountain_sma2.width / 2 then
mountain_sma2:translate(-480*2,0)
end
if tree_s.x > 480 + tree_s.width / 2 then
tree_s:translate(-480*2 , 0)
end
if tree_s2.x > 480 + tree_s2.width / 2 then
tree_s2:translate(-480*2 , 0)
end
if tree_l.x > 480 + tree_l.width / 2 then
tree_l:translate(-480*2 , 0)
end
if tree_s3.x > 480 + tree_s3.width / 2 then
tree_s3:translate(-480*2 , 0)
end
if tree_s4.x > 480 + tree_s4.width / 2 then
tree_s4:translate(-480*2 , 0)
end
if tree_l2.x > 480 + tree_l2.width / 2 then
tree_l2:translate(-480*2 , 0)
end
if tree_s5.x > 480 + tree_s5.width / 2 then
tree_s5:translate(-480*2 , 0)
end
if tree_s6.x > 480 + tree_s6.width / 2 then
tree_s6:translate(-480*2 , 0)
end
if tree_l3.x > 480 + tree_l3.width / 2 then
tree_l3:translate(-480*2 , 0)
end
if tree_l_sugi.x > 480 + tree_l_sugi.width / 2 then
tree_l_sugi:translate(-480*4,0)
end
if tree_l_take.x > 480 + tree_l_take.width / 2 then
tree_l_take:translate(-480*5,0)
end
local SPEED = 0.3
-- constantly adjust velocity to track touch instance
spriteInstance3:setLinearVelocity(
SPEED * (spriteInstance.x - spriteInstance3.x),
SPEED * (spriteInstance.y - spriteInstance3.y))
-- add rotation tracking for Touch Ant.
spriteInstance3.rotation = angleBetween(spriteInstance3,spriteInstance) + 90
end
local function printTouch( event )
if event.target then
local bounds = event.target.contentBounds
-- print( "event(" .. event.phase .. ") ("..event.x..","..event.y..") bounds: "..bounds.xMin..","..bounds.yMin..","..bounds.xMax..","..bounds.yMax )
end
end
local function onTouch( event )
local t = event.target
-- Print info about the event. For actual production code, you should
-- not call this function because it wastes CPU resources.
-- printTouch(event)
local phase = event.phase
if "began" == phase then
-- Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
-- Spurious events can be sent to the target, e.g. the user presses
-- elsewhere on the screen and then moves the finger over the target.
-- To prevent this, we add this flag. Only when it's true will "move"
-- events be sent to the target.
t.isFocus = true
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if "moved" == phase then
-- Make object move (we subtract t.x0,t.y0 so that moves are
-- relative to initial grab point, rather than object "snapping").
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
-- Important to return true. This tells the system that the event
-- should not be propagated to listeners of any objects underneath.
return true
end
-- Touch event for the Bouncing sprite (if it is touched, stop moving
local function onTouch2( event )
local t = event.target
-- Print info about the event. For actual production code, you should
-- not call this function because it wastes CPU resources.
-- printTouch(event)
local phase = event.phase
if "began" == phase then
-- Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
-- Spurious events can be sent to the target, e.g. the user presses
-- elsewhere on the screen and then moves the finger over the target.
-- To prevent this, we add this flag. Only when it's true will "move"
-- events be sent to the target.
t.isFocus = true
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
touch2down = true
spriteInstance2.timeScale=6
elseif t.isFocus then
if "moved" == phase then
-- Make object move (we subtract t.x0,t.y0 so that moves are
-- relative to initial grab point, rather than object "snapping").
-- t.x = event.x - t.x0
-- t.y = event.y - t.y0
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
touch2down = false
spriteInstance2.timeScale=1
end
end
-- Important to return true. This tells the system that the event
-- should not be propagated to listeners of any objects underneath.
return true
end
-- Boid moving event
function animateboid(event)
for i=1,#wanderers do
local wanderer = wanderers[i]
local prevx = wanderer.displayObject.x
local prevy = wanderer.displayObject.y
wanderer:wander()
wanderer:run()
-- add rotation sprite towards direction walking
wanderer.displayObject.rotation = angleBetweenPoints(wanderer.displayObject.x,wanderer.displayObject.y,prevx, prevy)+270
end
end
-- Event to keep the ant moving.
local function animate(event)
if touch2down == true then
return
end
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
if ( xpos > screenRight - radius or xpos < screenLeft + radius ) then
xdirection = xdirection * -1;
end
if ( ypos > screenBottom - radius or ypos < screenTop + radius ) then
ydirection = ydirection * -1;
end
local prevx = spriteInstance2.x
local prevy = spriteInstance2.y
spriteInstance2:translate( xpos - spriteInstance2.x, ypos - spriteInstance2.y)
local curx = spriteInstance2.x
local cury = spriteInstance2.y
-- add rotation tracking for bouncing Ant. - rotate towards walking direction
spriteInstance2.rotation = angleBetweenPoints(curx,cury,prevx, prevy)+270
end
-- Collision detection for Ant 1 hitting Ant 2
local function onLocalCollision( self, event )
if ( event.phase == "began" ) then
print( self.myName .. ": collision began with " .. event.other.myName )
elseif ( event.phase == "ended" ) then
print( self.myName .. ": collision ended with " .. event.other.myName )
end
end
--------------------
-- Instantiation
-- Ant #2 - No touch event Bouncer
spriteInstance2:prepare("texturepacker")
spriteInstance2:play()
spriteInstance2:addEventListener("touch",onTouch2)
-- Collision Event
--spriteInstance2.collision = onLocalCollision
--spriteInstance2:addEventListener( "collision", spriteInstance2 )
-- Ant #1 - w/ touch event
spriteInstance:prepare("texturepacker")
spriteInstance:play()
--Add to accept touch
--Add to accept touch
spriteInstance:addEventListener("touch",onTouch)
-- Add Collision listener
-- spriteInstance.collision = onLocalCollision
--spriteInstance:addEventListener( "collision", spriteInstance )
-- Ant #3 - Hunter
spriteInstance3:prepare("texturepacker")
spriteInstance3:play()
-- Set up Boids
math.randomseed( os.time() )
for i=1,20 do
local loc = Vector2D:new(display.contentWidth / 2,display.contentWidth / 2)
local spriteInstanceBoid = sprite.newSprite(spriteSet)
spriteInstanceBoid:scale(.5,.5)
spriteInstanceBoid:prepare("texturepacker")
spriteInstanceBoid:play()
-- local wanderer = Boid:new(loc,MAX_FORCE,MAX_SPEED,spriteInstanceBoid)
local wanderer = BoidBase:new(loc,MAX_FORCE,MAX_SPEED,spriteInstanceBoid,4)
table.insert(wanderers,wanderer)
end
Runtime:addEventListener( "enterFrame", animateboid )
Runtime:addEventListener( "enterFrame", animate )
Runtime:addEventListener("enterFrame",move)