Created a test obstacle to test collisions with.
[urggr.git] / src / urggr_package / playership.lua
blobb7a5a2870019c8999a6e33e0047b7dfba138f406
1 --[[
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/>.
18 --]]
20 require 'fail.utils'
21 require 'fail.math'
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
29 superclass = Ship,
31 function( self, level )
32 Ship.init( self )
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 )
59 end
62 function PlayerShip:MoveLeft( pressed )
63 if( pressed ) then
64 self.Speed.y = -5
65 else
66 self.Speed.y = 0
67 end
68 end
70 function PlayerShip:MoveRight( pressed )
71 if( pressed ) then
72 self.Speed.y = 5
73 else
74 self.Speed.y = 0
75 end
76 end
78 function PlayerShip:MoveUp( pressed )
79 if( pressed ) then
80 self.Speed.z = 5
81 else
82 self.Speed.z = 0
83 end
84 end
86 function PlayerShip:MoveDown( pressed )
87 if( pressed ) then
88 self.Speed.z = -5
89 else
90 self.Speed.z = 0
91 end
92 end
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?
101 collectgarbage()
102 -- print( deltatime )
103 -- self.camframe.LocalToParent.position.x = 7
104 -- self.camframe:dirty()
105 --print(self.frame)
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 )
110 self.frame:dirty()