-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
324 lines (226 loc) · 6.71 KB
/
main.lua
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
----------------------------------------------------------------
-- Copyright (c) 2010-2011 Zipline Games, Inc.
-- All Rights Reserved.
-- http://getmoai.com
----------------------------------------------------------------
MOAISim.openWindow ( "test", 640, 960 )
screenWidthX = 640--MOAIEnvironment.screenWidth
screenWidthY = 960--MOAIEnvironment.screenHeight
viewport = MOAIViewport.new ()
viewport:setSize ( screenWidthX, screenWidthY )
viewport:setScale ( 640, 960 )
layer = MOAILayer2D.new ()
GUIlayer = MOAILayer2D.new ()
MOAISim.pushRenderPass ( layer )
layer:setViewport ( viewport )
GUIlayer:setViewport (viewport)
gfxQuad = MOAIGfxQuad2D.new ()
gfxQuad:setTexture ( "moai.png" )
gfxQuad:setRect ( -64, -64, 64, 64 )
--font stuff
charcodes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .,:;!?()&/-'
font = MOAIFont.new ()
font:loadFromTTF ( 'arial-rounded.TTF', charcodes, 7.5, 163 )
-- This makes the touch input on the iphone register as clicks. This DOES not allow for multi touch.
--var
play = 1
ebatspeed = 10
mouseX=0
textbox1 = nil
gravity = 0
impulse = -200
score = 0
maxVelocityBall = 10
function pointerCallback ( x, y )
local oldX = mouseX
local oldY = mouseY
mouseX, mouseY = layer:wndToWorld ( x, y )
if pick then
pick:addLoc ( mouseX - oldX, mouseY - oldY )
end
end
function clickCallback (down)
if down then
StartGame()
end
end
if MOAIInputMgr.device.pointer then
-- mouse input
MOAIInputMgr.device.pointer:setCallback ( pointerCallback )
MOAIInputMgr.device.mouseLeft:setCallback ( clickCallback )
else
-- touch input
MOAIInputMgr.device.touch:setCallback (
function ( eventType, idx, x, y, tapCount )
pointerCallback ( x, y )
if eventType == MOAITouchSensor.TOUCH_DOWN then
clickCallback (true)
elseif eventType == MOAITouchSensor.TOUCH_UP then
clickCallback (false)
end
end
)
end
--collision handler
function onCollide (event, fixtureA, fixtureB, arbiter)
if event == MOAIBox2DArbiter.BEGIN then
end
if event == MOAIBox2DArbiter.END then
print( fixtureA.userdata.."collided with " .. fixtureB.userdata)
if fixtureA.userdata and fixtureB.userdata then
if fixtureA.userdata==-1 and fixtureB.userdata>3 then
--world:setGravity ( 0, gravity)
Ball:applyLinearImpulse (0, impulse)
end
if fixtureA.userdata==-1 and fixtureB.userdata>4 then
--world:setGravity ( 0, gravity)
Ball:applyLinearImpulse (0, impulse * -1)
end
end
end
if event == MOAIBox2DArbiter.PRE_SOLVE then
end
if event == MOAIBox2DArbiter.POST_SOLVE then
end
end
function MakeButton (x, y, x2, y2, xloc, yloc, text2)
buttonGFX = MOAIGfxQuad2D.new()
buttonGFX:setRect (x,y,x2,y2)
buttonGFX:setTexture ("button.png")
button = MOAIProp2D.new()
button:setDeck (buttonGFX)
button:setLoc (xloc,yloc)
layer:insertProp (button)
buttontext = addTextbox ( 0, 200, MOAITextBox.CENTER_JUSTIFY, true, 'replay?' )
userdata = 6
return button
end
function addTextbox ( top, height, alignment, yflip, textinput)
textbox = MOAITextBox.new ()
textbox:setString ( textinput )
textbox:setFont ( font )
textbox:setTextSize ( 12, 326 )
textbox:setRect ( -280, top - height, 280, top )
textbox:setAlignment ( alignment )
textbox:setYFlip ( true )
layer:insertProp ( textbox )
return textbox
end
scoretext = addTextbox ( 450, 450, MOAITextBox.LEFT_JUSTIFY, true, tostring(score) )
-- set up the world and start its simulation
world = MOAIBox2DWorld.new ()
world:setGravity ( 0, 0 )
world:setUnitsToMeters ( 1/30 )
world:start ()
layer:setBox2DWorld ( world )
--world:setDebugDrawEnabled (0)
--ball setup
function createBall ()
Ball = world:addBody ( MOAIBox2DBody.DYNAMIC)
FixtureBall = Ball:addCircle( 0, 0, 20)
FixtureBall:setDensity ( 0.01 )
FixtureBall:setFriction ( 0 )
FixtureBall:setRestitution (1)
FixtureBall.userdata = -1
FixtureBall:setFilter ( 0x01)
FixtureBall:setCollisionHandler (onCollide, MOAIBox2DArbiter.BEGIN + MOAIBox2DArbiter.END , 0x01)
--initial ball velocity
Ball:applyLinearImpulse (200, 200)
Ball:setAngularVelocity (200)
end
createBall ()
--side walls setup
leftwall = world:addBody (MOAIBox2DBody.STATIC)
FixtureLeftWall = leftwall:addRect ( -315, 480, -330, -480 )
FixtureLeftWall.userdata = 0
FixtureLeftWall:setFriction ( 0 )
rightwall = world:addBody (MOAIBox2DBody.STATIC)
FixtureRightWall = rightwall:addRect ( 315, 480, 330, -480 )
FixtureRightWall.userdata = 0
FixtureRightWall:setFriction ( 0 )
--player bat setup
Pbat = world:addBody ( MOAIBox2DBody.KINEMATIC)
FixBat = Pbat:addRect( -60, 35, 60,-35)
FixBatcircleleft = Pbat:addCircle(-60,0,35)
FixBatcircleRight = Pbat:addCircle(60,0,35)
FixBat.userdata = 3
FixBat:setFriction ( 0 )
FixBatcircleleft.userdata = 3
FixBatcircleleft:setFriction ( 0 )
FixBatcircleRight.userdata = 3
FixBatcircleRight:setFriction ( 0 )
Pbat:setTransform ( 0, -435)
--enemy bat
EBat = world:addBody (MOAIBox2DBody.KINEMATIC)
FixEBat = EBat:addRect( -60, 35, 60,-35)
FixEBat.userdata = 4
FixEBat:setFriction ( 0 )
FixEBatcircleleft = EBat:addCircle(-60,0,35)
FixEBatcircleRight = EBat:addCircle(60,0,35)
FixEBatcircleleft.userdata = 3
FixEBatcircleleft:setFriction ( 0 )
FixEBatcircleRight.userdata = 3
FixEBatcircleRight:setFriction ( 0 )
EBat:setTransform ( 0, 435)
function StartGame ()
--play = 1
world:start()
layer:removeProp (button)
layer:removeProp (textboxg)
layer:removeProp (buttontext)
end
function GameOver ( top, height, alignment )
textboxg = MOAITextBox.new ()
textboxg:setString ( 'GAME OVER' )
textboxg:setFont ( font )
textboxg:setTextSize ( 12, 326 )
textboxg:setRect ( -100, top - height, 100, top )
textboxg:setAlignment ( alignment )
textboxg:setYFlip ( true )
layer:insertProp ( textboxg )
replaybut = MakeButton (-100, -100, 100, 100, 0, 0, 'yep')
end
--game loop
function main ()
while play == 1 do
coroutine.yield()
scoretext:setString ( tostring(score) )
ballx,bally = Ball:getPosition ()
EBatx,EBaty = EBat:getPosition ()
PBatx,PBaty = Pbat:getPosition ()
ebatspeed = ballx - EBatx
pbatspeed = mouseX - PBatx
--DIFFICULTY
if score > 0 or score == 0 then
difficulty = 1 + score * 2
elseif score < -1 then
difficulty = 1
end
EBat:setLinearVelocity(ebatspeed * difficulty ,0)
Pbat:setLinearVelocity(pbatspeed * 20 ,0)
--reset ball
if bally <-460 then
score = score -1
Ball:destroy ()
createBall ()
end
if bally >460 then
score = score +1
Ball:destroy ()
createBall ()
end
if score > 4 or score <-4 then
score = 0
--play = 0
world:stop ()
Pbat:setTransform (0,0)
EBat:setTransform (0,0)
GameOver ( 100, 100, MOAITextBox.CENTER_JUSTIFY , true)
end
end
end
bally = 0
ballx = 0
--Begin the game loop
thread = MOAICoroutine.new()
thread:run( main )