Quick and dirty fbf serialization test.
[urggr.git] / src / urggr_package / playership.lua
bloba185c5a06d439bb814cc7ac7bc5da67738e66b9e
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.level = level
35 self.frame = sg.Frame()
37 -- Create a renderable for the player ship. For now we just conjure a procedural cone
38 -- programmaticaly, at some point it'll be a renderable loaded from disk.
39 local mat = sg.Material()
40 mat.Emission.value = fail.math.Vector4f( 0, 1, 1, 1 )
41 mat.Specular.value = fail.math.Vector4f( 0, 0, 0, 1 )
42 self.renderable = sg.shapes.Cube( mat, self.frame )
45 -- Serialization test. Load the player scenegraph from a file (previously saved in urggr.lua)
46 -- self.renderable = fail.fbffile.Reader.Load( "lulz.fbf" )
47 -- self.frame = self.renderable.pFrame
49 self.Speed = fail.math.Vector3f()
51 level:addRenderable( self.renderable )
53 -- Create a collision object for the player ship. A box for the time being since the other shape classes
54 -- aren't wrapped in fail yet.
55 -- TODO: also provide a way to set the collision filter mask.
56 self.CollisionObject = col.Object()
57 self.CollisionObject.pShape = col.BoxShape( fail.math.Vector3f( 0.5, 0.5, 0.5 ) )
58 self.CollisionObject.pFrame = self.frame
59 level:addCollisionObject( self.CollisionObject )
61 input.InputManager.GetInstance().Actions.Left:bindHandler( function( pressed ) self:MoveLeft( pressed ) end )
62 input.InputManager.GetInstance().Actions.Right:bindHandler( function( pressed ) self:MoveRight( pressed ) end )
63 input.InputManager.GetInstance().Actions.Up:bindHandler( function( pressed ) self:MoveUp( pressed ) end )
64 input.InputManager.GetInstance().Actions.Down:bindHandler( function( pressed ) self:MoveDown( pressed ) end )
65 input.InputManager.GetInstance().Actions.Fire:bindHandler( function( pressed ) self:Fire( pressed ) end )
66 end
69 function PlayerShip:MoveLeft( pressed )
70 if( pressed ) then
71 self.Speed.y = -5
72 else
73 self.Speed.y = 0
74 end
75 end
77 function PlayerShip:MoveRight( pressed )
78 if( pressed ) then
79 self.Speed.y = 5
80 else
81 self.Speed.y = 0
82 end
83 end
85 function PlayerShip:MoveUp( pressed )
86 if( pressed ) then
87 self.Speed.z = 5
88 else
89 self.Speed.z = 0
90 end
91 end
93 function PlayerShip:MoveDown( pressed )
94 if( pressed ) then
95 self.Speed.z = -5
96 else
97 self.Speed.z = 0
98 end
99 end
102 function PlayerShip:Fire( pressed )
103 if( pressed ) then
104 -- TODO: might be useful to provide some easier access to copy constructors to simply this.
105 -- Perhaps in the form of a global fail.copy function?
107 bulletpos = fail.math.Vector3f( --self.frame.LocalToParent.position )
108 self.frame.LocalToParent.position.x,
109 self.frame.LocalToParent.position.y,
110 self.frame.LocalToParent.position.z
113 bulletpos.y = bulletpos.y + 0.7
115 self.level:addGameObject( urggr.PlayerBullet( self.level, bulletpos ) )
119 function PlayerShip:renderingUpdate( deltatime )
122 --collectgarbage()
123 -- print( deltatime )
124 -- self.camframe.LocalToParent.position.x = 7
125 -- self.camframe:dirty()
126 --print(self.frame)
128 self.frame.LocalToParent.position.x = self.frame.LocalToParent.position.x + self.Speed.x * ( deltatime / 1000 )
129 self.frame.LocalToParent.position.y = self.frame.LocalToParent.position.y + self.Speed.y * ( deltatime / 1000 )
130 self.frame.LocalToParent.position.z = self.frame.LocalToParent.position.z + self.Speed.z * ( deltatime / 1000 )
131 self.frame:dirty()