OOP Floating Boxes With Blitz3D

Miscellaneous Forums/General Discussion/OOP Floating Boxes With Blitz3D

yep... i'm still on that OO Blitz thing...

based on a demo i saw using unigine i decided to whip up something similar in Blitz... no physics, just a lil hack... i've put together a bouyant object class and dropped a few of em around in the water (ocean class)...

the demo is on the Blitz Showcase forum...

the bouyant box class looks like this...

;------------------------------------------------------------------------------------------------------
;
;								BouyantBox Class Globals Declarations 
;
;  declare a global specifying the maximum number of instances for this  subtype here, then dimension 
;  each property to this global limit...  this gives each new instance of the subclass the capability  
;  of being manipulated as an individual object via its \objId Field in the subclass Update method
;  
;  the property names should be unique as to avoid conflicts with other variables in your program
;
;-----------------------------------------------------------------------------------------------------

 MaxBoxesAllowed=10
 Dim boxVelocityY#(MaxBoxesAllowed)
 Dim bouyancy#(MaxBoxesAllowed)
 Dim boxRot#(MaxBoxesAllowed)
 Dim bottomedOut(MaxBoxesAllowed)
 Dim Splashed(MaxBoxesAllowed)
 Dim boxFallingSnd(MaxBoxesAllowed)
 Dim boxSplashSnd(MaxBoxesAllowed)
 Dim boxFloatingSnd(MaxBoxesAllowed)
 Dim boxSndCh(MaxBoxesAllowed)
 Dim boxSndVol#(MaxBoxesAllowed)

  
Function UpdateBouyantBoxes(this.tObject)
      ;---------------------------------------------------------------- one time initialization
      ;
      If this\status=0
         ScaleEntity this\hEntity,10,10,10
         boxVelocityY(this\objID)=-12
         bouyancy(this\objID)=.003
         boxFallingSnd(this\ObjID)=LoadSound(theGame\System\HomeDir +"media\boxes\falling.wav")
         boxSplashSnd(this\ObjID)=LoadSound(theGame\System\HomeDir +"media\boxes\Splash.wav")
         boxFloatingSnd(this\ObjID)=LoadSound(theGame\System\HomeDir +"media\boxes\Floating.wav")
         SoundVolume boxFallingSnd(this\objID),0

		;------------------------------------------ create a splash object
		;
 
        Splash.tObject=CreateObject("SPLASH",theGame\System\HomeDir + "media\Ocean\Splash.b3d")
        SetObjectParent(Splash,this)
        PositionEntity Splash\hEntity,0,1,0
        this\status=1

      EndIf

      ;-------------------------------------------------------------------------------- always
      ; 

      ;------------------------------------ it's above the ocean and falling  
      ; 
      If EntityY(this\hEntity)>-10
        If boxVelocityY(this\objID)>-12 Then boxVelocityY(this\objID)=boxVelocityY(this\objID)-.004

        ;----------------------------------  play falling sound 
        ; 
        If Not ChannelPlaying(boxSndCh(this\objID))
          If bottomedOut(this\objID)=0 Then boxSndCh(this\objID)=PlaySound(boxFallingSnd(this\objID))
        End If
        ;----------------------------------- it already fallen but bobbing above the surface
        ;
        If Not ChannelPlaying(boxSndCh(this\objID))
           If  bottomedOut(this\objID) Then boxSndCh(this\objID)=PlaySound(boxFloatingSnd(this\objID))
        EndIf

        ;---------------------------simulate moving with current (faked right now)
		;
		If bottomedOut(this\objID)
          MoveEntity this\hEntity, -.2,0,-.15
          RotateEntity(this\hEntity),Sin(boxRot(this\objID))*10,0,Sin(boxRot(this\objID))*5
          boxRot(this\objID)=boxRot(this\objID)+1
        EndIf

      Else
          ;---------------------------------- it hit the water and is below or on the surface
          ;
          ;---------------------------------- if splashdown play splash sound
          ; 
          If Not splashed(this\objID)
            StopChannel boxSndCh(this\objID)
            If Not bottomedOut(this\objID) 
              boxSndCh(this\objID)=PlaySound(boxSplashSnd(this\objID))
              splashed(this\objID)=True
            EndIf
          Else
            If Not ChannelPlaying(boxSndCh(this\objID))
              If bottomedOut(this\objID)=1 Then boxSndCh(this\objID)=PlaySound(boxFloatingSnd(this\objID))
            End If           
          End If

          ;------------------------------------ if just impacted slow down and let sink a lil
		  ;           
          If boxVelocityY(this\objID)<-2.6 Then boxVelocityY(this\objID)=-2.6

          ;------------------------------------ check to see if bottomed out 
		  ;
          If EntityY(this\hEntity)< -10 Then  BottomedOut(this\objID)=1


          ;------------------------------------ if bottomed out then limit down delta
		  ;
          If boxVelocityY(this\objID)<-0.4 And BottomedOut(this\objID)=1 Then boxVelocityY(this\objID)=-0.4

          ;------------------------------------ allow to rise and limit rise  delta
		  ;
          If boxVelocityY(this\objID)<.22  Then boxVelocityY(this\objID)=boxVelocityY(this\objID)+ bouyancy(this\objID)


          ;------------------------------------ simulate rolling motion
		  ;
          RotateEntity(this\hEntity),Sin(boxRot(this\objID))*10,0,Sin(boxRot(this\objID))*5
          boxRot(this\objID)=boxRot(this\objID)+1

          ;------------------------------------ simulate moving with current (faked right now)
		  ;
          MoveEntity this\hEntity, -.2,0,-.15
      EndIf
   


      ;----------------------------- reset boxes to fall again (shouldn't be hardcoded here)
	  ;
      If KeyDown(19)
         bouyancy(this\objID)=.003
         boxVelocityY(this\objID)=-12
         RotateEntity this\hEntity,0,0,0
         PositionEntity(this\hEntity, EntityX(this\hEntity)+Rnd(-50,50),Rnd(1200,2700),EntityZ(this\hEntity)+Rnd(-20,20))
         bottomedOut(this\objID)=0
         splashed(this\objID)=0
         StopChannel boxSndCh(this\objID)
      EndIf

      ;--------------------------- set bouyancy to zero to sink em (shouldn't be hardcoded here)
	  ;
      If KeyDown(33)
         bouyancy(this\objID)=0
      EndIf

      ;----------------------- regain positive bouyancy to refloat em (shouldn't be hardcoded here)
	  ;
      If KeyDown(47)
         bouyancy(this\objID)=.003
      EndIf

      ;------------------------------------ keep everything in motion
	  ;
      MoveEntity this\hEntity, 0,boxVelocityY(this\objID),0
     
      ;------------------------------------ adjust sound volume continuously
	  ;
	  boxSndVol(this\objID)=1-(EntityDistance(this\hEntity,theGame\View\hEntity)/4000)
	  If boxSndVol(this\objID) >0
        SoundVolume boxFallingSnd(this\objID),boxSndVol(this\objID)
        SoundVolume boxFloatingSnd(this\objID),boxSndVol(this\objID)
        SoundVolume boxSplashSnd(this\objID),boxSndVol(this\objID)
      Else
        SoundVolume boxFallingSnd(this\objID),0
      EndIf 


      ;------------------------------------ probably not necessary
	  ;
      If boxRot(this\objID)>359 Then boxRot(this\objID)=0

          
End Function


a lil verbose for some maybe, but this method helps me keep my code and thinking a lot more modularized than doing it the 'old' way...

besides... the compiler doesn't care how i reference stuff anyways (well... not much)...

anyway... if you get a free sec... check it out...

--Mike

heeeeeyyyyyy!!!! somebody look at my stuff... if only to make fun of it...

it's really kinda cool, if i do say so myself :)

http://home.att.net/~mikey102/OOPBlitzDemo.zip

--Mike

looks good. i just wanted the boxes to get back to the surface quicker.

fixed reflections, added some stuff... backspace key now deletes objects...

ya know, this Blitz3D is pretty fast for an old BASIC programming language... in between each tick, i'm looping and doing about 3 checks on each object (that's how i got rid of the reflection anomoy), and Blitz doesn't seem to mind a bit...

--Mike