Main Menu

Inherit Me

Started by kevin, July 06, 2004, 08:12:46 PM

Previous topic - Next topic

kevin

   This example shows how you can create a parent Type, And Then spawn other; child types from it.   So now our child object types (i.e Aliens/Bullets in this example) contain both the common properties of the parent As well As extending these with their own properties.  You Not limited there, the children can spawn children etc etc.    Creating entire family tree of;  Data types.  Now since the child types inherit their the properties from the parent and we can pass typed arrays to functions given a suitable typed function, this now means that we can  not only pass the parent into a function, but any children of that parent as well.. So common sets of functions can now; be shared between the various data types.. providing they were derived (spawned) from the same parent..

All in all, it mean we can create more compact code, through more; re-usable functions.



PlayBASIC Code: [Select]
; Create the Parent Type from which we'll derive the child types
Type MyObject
status,X#,Y#,Class,Spr,score,speed#,direction#
EndType

; Create the alien
Type tAliens As MyObject
Name$
AI
hits
TimeToFire
EndType

; Create the Bullet type
Type tBullets As myObject
ExtraStuff
EndType

; create the two arrays to hold the entities
Dim Aliens(0) As tAliens
Dim Bullets(0) As tBullets

; Create a New Type
Function Create(me().MyObject,X#,y#)
index=GetFree(me().myobject)
me(index).status=true
Me(index).x =x#
Me(index).x =x#
Spr=GetFreeSprite()
me(index).spr=spr
CreateSprite Spr
PositionSpriteXyz spr,x#,y#,10
EndFunction index

; Delete a type instance
Function Delete(me().MyObject,index)
If me(index).status=true
DeleteSprite me(index).spr
ClearArrayCells me().myobject,index,1,1,0
EndIf
EndFunction


; Positon
Function Position(me().MyObject,Index,X#,y#)
Me(index).x =x#
Me(index).x =x#
PositionSprite me(index).spr,x#,y#
EndFunction
;

Function PointAt(me().MyObject,Index,HereX#,Herey#)
x#=GetSpriteX(me(index).spr)
y#=GetSpriteY(me(index).spr)
Angle#=GetAngle2D(x#,y#,herex#,herey#)
Me(index).direction =angle#
EndFunction


Function GetFree(me().myobject)
Repeat
Size=GetArrayElements(me().myobject,1)
index=FindArrayCell(me().myobject,0,1,Size,0)
If Index=-1
ReDim Me(size+25) As MyObject
EndIf
Until index<>-1
EndFunction Index


; ================================
; Load Media
; ================================

Global img_Ship=1 :LoadImage "..\..\..\gfx\ship.bmp",img_Ship
Global img_Ball=2 :LoadImage "..\..\..\gfx\bubble_64x64.bmp",img_Ball
Global img_Bullet=3 :CircleC 10,10,10,1,$ff0000 : GetImage img_bullet,0,0,20,20


; ================================
; Main Loop
; ================================

Do
Cls 0
Print "Making Life Easier With Inherited Types"
DrawImage 1,MouseX(),MouseY(),1
UpdateBullets()
UpdateAliens()
DrawOrderedSprites
Sync
Loop





; ================================
; Control the aliens
; ================================

Function UpdateAliens()
Static TimeToMakeAlien

ScreenWidth=GetScreenWidth()
ScreenHeight=GetScreenHeight()

If TimeToMakeAlien=<0
Index=Create(Aliens().tAliens,Rnd(ScreenWidth),Rnd(ScreenHeight))
SpriteImage ALiens(index).spr,img_Ball
Aliens(index).speed=Rnd(100)/50.0
Aliens(index).Direction=Rnd(360)
Aliens(index).TimeToFire=Rnd(100)

TimeToMakeAlien=Rnd(100)
Else
DEC TimeToMakeAlien
EndIf

For ThisAlien=0 To GetArrayElements(Aliens().taliens,1)
If Aliens(ThisAlien).status
Spr=ALiens(ThisAlien).spr
speed#=aliens(thisALien).speed
Direction#=aliens(thisALien).direction
MoveSprite spr,CosRadius(Direction#,Speed#),SinRadius(Direction#,speed#)

ShootDelay=Aliens(index).TimeToFire-1
If ShootDelay<0
ShootDelay=Rnd(100)
CreateBullet(GetSpriteX(spr),GetSpriteY(spr))
EndIf

If SpriteInRegion(spr,0,0,ScreenWidth,ScreenHeight)=false
Delete(Aliens().tAliens,ThisAlien)
Continue
EndIf

Aliens(index).TimeToFire=ShootDelay
EndIf
Next
EndFunction


; ================================
; Control the aliens
; ================================

Function CreateBUllet(x#,y#)
BulletIndex=Create(Bullets().tBullets,x#,y#)
Login required to view complete source code



kevin

#1
repost - Same code as above, easier to read though.


PlayBASIC Code: [Select]
; ==============================================================================
; PlayBasic Inherited Types Example
; By Kevin Picone
; www.PlayBasic.com
; ==============================================================================

; This example shows how you can create a parent Type, then spawn other
; child types from it. So now our child object types (i.e Aliens/Bullets
; in this example) contain both the common properties of the parent, as well
; as extending these with their own properties. You Not limited there, the
; children can in turn spawn children etc etc. Creating an entire family
; tree of Data types.
;
; Now since the child types inherit their the properties from the parent,
; and we can pass typed arrays into functions given a suitable typed function,
; this now means that we can not only pass the parent into a function, but
; any children of that parent as well.. So common sets of functions can now
; be shared between the various Data types.. providing they were derived
; (spawned) from the same parent..

; All in all, this will help us create more compact code, through functions
; that are re-use friendly..

; ==============================================================================

Cls 0

; Create the Parent Type from which we'll dereive the child types
Type MyObject
status,X#,Y#,Class,Spr,score,speed#,direction#
EndType

; Create the alien
Type tAliens As MyObject
Name$
AI
hits
TimeToFire
EndType

; Create the Bullet Type
Type tBullets As myObject
ExtraStuff
EndType


; create the two arrays To hold the entities
Dim Aliens(0) As tAliens
Dim Bullets(0) As tBullets

; Create a New Type
Function Create(me().MyObject,X#,y#)
index=GetFree(me().myobject)
me(index).status=True
Me(index).x =x#
Me(index).x =x#
Spr=GetFreeSprite()
me(index).spr=spr
CreateSprite Spr
PositionSpriteXYZ spr,x#,y#,10
EndFunction index

; Delete a Type instance
Function Delete(me().MyObject,index)
If me(index).status=True
DeleteSprite me(index).spr
ClearArrayCells me().myobject,index,1,1,0
EndIf
EndFunction

; Positon
Function Position(me().MyObject,Index,X#,y#)
Me(index).x =x#
Me(index).x =x#
PositionSprite me(index).spr,x#,y#
EndFunction

;
Function PointAt(me().MyObject,Index,HereX#,Herey#)
x#=GetSpriteX(me(index).spr)
y#=GetSpriteY(me(index).spr)
Angle#=GetAngle2D(x#,y#,herex#,herey#)
Me(index).direction =angle#
EndFunction


Function GetFree(me().myobject)
Repeat
Size=GetArrayElements(me().myobject,1)
index=FindArrayCell(me().myobject,0,1,Size,0)
If Index=-1
ReDim Me(size+25) As MyObject
EndIf
Until index<>-1
EndFunction Index

; ================================
; Load Media
; ================================

Global img_Ship=1 :LoadImage "..\..\..\gfx\ship.bmp",img_Ship
Global img_Ball=2 :LoadImage "..\..\..\gfx\bubble_64x64.bmp",img_Ball
Global img_Bullet=3 :CircleC 10,10,10,1,$ff0000 : GetImage img_bullet,0,0,21,21


; ================================
; Main Loop
; ================================


Do
Cls 0
Print "Making Life Easier With Inherited Types"

; Update the Bullet objects
UpdateBullets()

; Update the Aliens
UpdateAliens()

; DRaw the Z ordered Sprites
DrawOrderedSprites

; Draw the PLayer
DrawImage 1,MouseX()-(GetImageWidth(img_ship)/2),MouseY()-(GetImageHeight(img_ship)/2),1

Sync
Loop





; ================================
; Control the aliens
; ================================

Function UpdateAliens()
Static TimeToMakeAlien

ScreenWidth=GetScreenWidth()
ScreenHeight=GetScreenHeight()

If TimeToMakeAlien=<0
Index=Create(Aliens().tAliens,Rnd(ScreenWidth),Rnd(ScreenHeight))
SpriteImage ALiens(index).spr,img_Ball
Aliens(index).speed=Rnd(100)/50.0
Aliens(index).Direction=Rnd(360)
Aliens(index).TimeToFire=Rnd(100)
Login required to view complete source code



ale870

Hello,

I found this interesting example, but I noticed the following code:

Type MyObject
     status,X#,Y#,Class,Spr,score,speed#,direction#
EndType


There is a name "Class" which is highlighted from the editor, but I cannot get any info in the help. Furthermore I eliminated it, and the example works as well.

So I wish to know if "Class" word is a PB reserved instruction or... what?

Thank you!
--Alessandro

kevin


Today 'Class/EndClass' are reserved words.  They are to be implemented in VM2, when we can break free of VM1 - Never though it'd take this long :(


ale870

Wow! I smell about OOP!  :P

I'm very excited waiting for next version (I know several OOP languages!  ;) and I like OO concept.

Thank you !
--Alessandro