2 Urggr - An horizontal scrolling shoot'em up.
3 Copyright 2008 Antoine Chavasse <a.chavasse@gmail.com>
5 This file is part of Urggr.
7 Urggr is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 3
9 as published by the Free Software Foundation.
11 Urggr is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 sg
= require
'fail.scenegraph'
23 require
'fail.scenegraph.shapes'
24 input
= require
'fail.game.input'
25 col
= require
'fail.bullet.collision'
27 PlayerShip
= fail
.utils
.class
31 function( self
, level
)
34 self
.frame
= sg
.Frame()
36 -- Create a renderable for the player ship. For now we just conjure a procedural cone
37 -- programmaticaly, at some point it'll be a renderable loaded from disk.
38 local mat
= sg
.Material()
39 mat
.Emission
.value
= fail
.math
.Vector4f( 1, 0, 0, 1 )
40 mat
.Specular
.value
= fail
.math
.Vector4f( 0, 0, 0, 1 )
41 self
.renderable
= sg
.shapes
.Cube( mat
, self
.frame
)
43 self
.Speed
= fail
.math
.Vector3f()
45 level
:addRenderable( self
.renderable
)
47 -- Create a collision object for the player ship. A box for the time being since the other shape classes
48 -- aren't wrapped in fail yet.
49 -- TODO: also provide a way to set the collision filter mask.
50 self
.CollisionObject
= col
.Object()
51 self
.CollisionObject
.pShape
= col
.BoxShape( fail
.math
.Vector3f( 0.5, 0.5, 0.5 ) )
52 self
.CollisionObject
.pFrame
= self
.frame
53 level
:addCollisionObject( self
.CollisionObject
)
55 input
.InputManager
.GetInstance().Actions
.Left
:bindHandler( function( pressed
) self
:MoveLeft( pressed
) end )
56 input
.InputManager
.GetInstance().Actions
.Right
:bindHandler( function( pressed
) self
:MoveRight( pressed
) end )
57 input
.InputManager
.GetInstance().Actions
.Up
:bindHandler( function( pressed
) self
:MoveUp( pressed
) end )
58 input
.InputManager
.GetInstance().Actions
.Down
:bindHandler( function( pressed
) self
:MoveDown( pressed
) end )
62 function PlayerShip
:MoveLeft( pressed
)
70 function PlayerShip
:MoveRight( pressed
)
78 function PlayerShip
:MoveUp( pressed
)
86 function PlayerShip
:MoveDown( pressed
)
95 function PlayerShip
:update( deltatime
)
97 -- TODO: investigate why the thing crashes after a while if we don't explicitly
98 -- call collect garbage regularly like so.
99 -- Perhaps a garbage collection happening from an event handler or something results into a deadlock?
102 -- print( deltatime )
103 -- self.camframe.LocalToParent.position.x = 7
104 -- self.camframe:dirty()
107 self
.frame
.LocalToParent
.position
.x
= self
.frame
.LocalToParent
.position
.x
+ self
.Speed
.x
* ( deltatime
/ 1000 )
108 self
.frame
.LocalToParent
.position
.y
= self
.frame
.LocalToParent
.position
.y
+ self
.Speed
.y
* ( deltatime
/ 1000 )
109 self
.frame
.LocalToParent
.position
.z
= self
.frame
.LocalToParent
.position
.z
+ self
.Speed
.z
* ( deltatime
/ 1000 )