An OOP Spaceship Movement routine

BlitzMax Forums/BlitzMax Tutorials/An OOP Spaceship Movement routine

bradford6(Posted 2005) [#1]
Spaceship with OOP

VERSION 1 of 2


Spaceship OOP
VERSION 2 of 2




Rimmsy(Posted 2005) [#2]
nice tutorial. excellent comments.


Perturbatio(Posted 2005) [#3]
nice :)

but just to make it a little more OOP:



AdrianT(Posted 2005) [#4]
Pretty useful and easy to understand even for a newbie like me :) thanks.


DivineDominion(Posted 2005) [#5]
Sound very good, but I suggest some changes in the structure. I learnt that it's better to derive everything from something like TGameObject:

Type TGameObject
  Field x:float, y:Float
  Field hImage:TImage
  '...
EndType

type TEnemy extends TGameObject
  '...
endtype

type TPlayer extends TGameObject
  '...
endtype

type TBullet extends TGameObject
  Field hPlayerWhoShot:TPlayer
endtype

type TGuardian extends TEnemy
  'blah blah
endtype


Thats useful because you can implement a method like this in TGameObject:

Method collision:Byte( hObj:TGameObject )
		
		If ( Self.x + Self.width ) > hObj.x And Self.x < ( hObj.x + hObj.width )
			
			If ( Self.y + Self.height ) > hObj.y And Self.y < ( hObj.y + hObj.height )
			
				Return True
				
			EndIf
			
		EndIf
		
		Return False
		
	EndMethod


You can check everything with everything, use things in a single list (not very clever but you can put all enemies together, for example) etc., I love to code things this way because it feels pretty cool. But that doesnt mean that your code isnt good at all, i certainly like it, its a good work :)


bradford6(Posted 2005) [#6]
great comments everyone.
nice mod, perturbio
I'll post V2 with those enhancements.


JAW(Posted 2005) [#7]
Thanks for the tutorial Bill. It is also helpful and interesting to see the progression from the original version, so it might be a good idea to include the original with the V2. That way others might be able to see how they can make their own code 'a little more OOP'.

Cheers


bradford6(Posted 2005) [#8]
OK. Just added Version 2 above. Just like many of you, I am learning this as I go along so if any Master OOPs-men want to critique, I encourage it.

in V3 we may see some enemies...


Takuan(Posted 2005) [#9]
Before BMax i used hundreds of Arrays like
EntitysVeloc(entity*100+EntityID)=1 to get quick acces to a certain entitys data.

With BMax thats all so beautyfull.......sniff


Mh, if you "release Player2" an error occures (sure).
I got around with statements like:
if player2<>Null player2.CheckControls()

Is there a faster way to ignore a non valid method call?


bradford6(Posted 2005) [#10]
In order to test out what you mean, I added this to the code.

If KeyHit(KEY_SPACE)
		Release player2
		player2.checkcontrols
	Endif


it generate this:
Unhandled Exception: Attempt to access field or method of Null object


the Variable 'Player2' is a pointer. in the player.create Function we add the player2 object to the Ship_list LIST
ListAddLast ship_list,t 


remember how we assigned Player2 the name "Kate"? We can use that as an Index instead. or you could assign an arbitrary Integer, I like using names though.

We can create a function that will remove our player.




Extron(Posted 2005) [#11]
Good tutorial.
This inspire me for the OOP version of Nehe tutorial 32. :)

I added some code in the method update() :
angular_velocity# = angular_velocity# * rotfriction#
velocity# = velocity# * friction
If velocity#<0.0001 Then velocity#=0.0

And change this to the methode show() :
DrawText "Velocity = "+ "".FromFloat(velocity)[..6],x-40,y+48

I hate this long long long float. :)
It's just a preference.