1+ scene = PhysicsScene (0 , Vector3 (200 , 200 , 200 ))
2+
3+ ground = ScenePrimitive (ScenePrimitive .TYPE_PLANE , 10 , 10 )
4+ ground :loadTexture (" Resources/green_texture.png" )
5+
6+ scene :addPhysicsChild (ground , 6 , 0 )
7+
8+ for i = 1 , 1 do
9+ local box = ScenePrimitive (ScenePrimitive .TYPE_BOX , 0.8 , 0.8 , 0.8 )
10+ box :loadTexture (" Resources/pink_texture.png" )
11+ box :Roll (- 45 + math.random () % 90 )
12+ box :Pitch (- 45 + math.random () % 90 )
13+ box :setPosition (- 2 + math.random () % 4 , i * 0.5 , - 2 + math.random () % 4 )
14+
15+ scene :addPhysicsChild (box , 0 , 1 )
16+ end
17+
18+ player = ScenePrimitive (ScenePrimitive .TYPE_BOX , 0.5 , 1 , 0.5 )
19+ player :loadTexture (" Resources/pink_texture.png" )
20+ player :setColor (1 , 1 , 0 , 1 )
21+ player :setPosition (2 , 1 , 2 )
22+
23+ playerController = scene :addCharacterChild (player , 10 , 1 , 0.5 )
24+ local walkSpeed = 0
25+ local rotateSpeed = 0
26+ local playerDirection = 0
27+
28+ testBox = ScenePrimitive (ScenePrimitive .TYPE_BOX , 2 , 2 , 2 )
29+ testBox :loadTexture (" Resources/pink_texture.png" )
30+ testBox :setColor (0.3 , 0.5 , 1 , 0.4 )
31+ testBox :setPosition (2 ,1 ,- 2 )
32+
33+ scene :addCollisionChild (testBox , 0 )
34+
35+ hud = Scene (Scene .SCENE_2D_TOPLEFT )
36+ onGroundLabel = SceneLabel (" Arrow keys to control, spacebar to jump, press R to reset" , 16 )
37+ onGroundLabel :setAnchorPoint (Vector3 (- 1 , - 1 , 0 ))
38+ onGroundLabel :setPosition (0 ,0 )
39+ hud :addChild (onGroundLabel )
40+ onGroundLabel = SceneLabel (" On Ground:" , 16 )
41+ onGroundLabel :setAnchorPoint (Vector3 (- 1 , - 1 , 0 ))
42+ onGroundLabel :setPosition (0 , 32 )
43+
44+ hud :addChild (onGroundLabel )
45+
46+
47+ scene :getDefaultCamera ():setPosition (7 , 7 , 7 )
48+ scene :getDefaultCamera ():lookAt (Vector3 (0 , 0 , 0 ), Vector3 (1 , 1 , 1 ))
49+
50+ function handleKeyEvent (t , e )
51+ if not e :getDispatcher () == CoreServices .getInstance ():getCore ():getInput () then return end
52+
53+ local inputEvent = safe_cast (e , InputEvent )
54+
55+ local eventKeyCode = e :getEventCode ()
56+ if eventKeyCode == InputEvent .EVENT_KEYDOWN then
57+ local keyCode = inputEvent :keyCode ()
58+
59+ if keyCode == KEY_r then
60+ playerController :warpCharacter (Vector3 (2 , 1 , 2 ))
61+ elseif keyCode == KEY_UP then
62+ walkSpeed = 0.05
63+ elseif keyCode == KEY_DOWN then
64+ walkSpeed = - 0.05
65+ elseif keyCode == KEY_LEFT then
66+ rotateSpeed = 3
67+ elseif keyCode == KEY_RIGHT then
68+ rotateSpeed = - 3
69+ elseif keyCode == KEY_SPACE then
70+ playerController :jump ()
71+ end
72+ elseif eventKeyCode == InputEvent .EVENT_KEYUP then
73+ if inputEvent .key == KEY_DOWN then
74+ walkSpeed = 0
75+ elseif inputEvent .key == KEY_RIGHT then
76+ rotateSpeed = 0
77+ end
78+ end
79+ end
80+
81+ CoreServices .getInstance ():getCore ():getInput ():addEventListener (nil , handleKeyEvent , InputEvent .EVENT_KEYDOWN )
82+ CoreServices .getInstance ():getCore ():getInput ():addEventListener (nil , handleKeyEvent , InputEvent .EVENT_KEYUP )
83+
84+ function Update (elapsed )
85+ playerDirection = playerDirection + rotateSpeed * elapsed
86+
87+ player :setYaw (playerDirection * (180 / math.pi ))
88+ playerController :setWalkDirection (Vector3 (walkSpeed * math.cos (playerDirection ), 0 , walkSpeed * math.sin (- playerDirection )))
89+
90+ if playerController :onGround () then
91+ onGroundLabel :setText (" On Ground: YES" )
92+ else
93+ onGroundLabel :setText (" On Ground: NO" )
94+ end
95+
96+ res = scene :testCollision (player , testBox )
97+ if res .collided then
98+ testBox :setColor (1 , 1 , 0 , 0.5 )
99+ else
100+ testBox :setColor (0 , 1 , 1 , 0.5 )
101+ end
102+ end
0 commit comments