UnderwareDESIGN

PlayBASIC => Show Case => Topic started by: LemonWizard on December 10, 2009, 08:34:23 PM

Title: Coming soon, platforming sh'mup
Post by: LemonWizard on December 10, 2009, 08:34:23 PM

(http://underwaredesign.com/screens/PlayBasic/Forums/LemonWizard/platforming-Shot1-3.jpg)




Soon to come.
Demo available NOW!



Title: Re: Coming soon, platforming sh'mup
Post by: kevin on December 10, 2009, 10:45:57 PM
 This looks like it could be fun..

 Had a quick look see as the code and there's bit that could improved upon.   At the start when your loading your media, you can reduce a lot of this by rolling the repetitive lines of code parts into a Function or PSUB.  

Example,

[pbcode]
; this version if the mask might be different
Psub LoadImg(File$,Mask)
   img=loadnewfximage("Resources/"+file$)
   imagemaskcolour img, Mask
EndPsub img

; or this if the mask is always the same
Psub LoadImg(File$)
   img=loadnewfximage("Resources/"+file$)
   imagemaskcolour img, rgb(255,255,255)
EndPsub img

[/pbcode]


ie.  This

[pbcode]
;create resources
dim arrows(4)
arrows(1)=loadnewfximage("Resources/Uparrow.bmp")
arrows(2)=loadnewfximage("Resources/Downarrow.bmp")
arrows(3)=loadnewfximage("Resources/Leftarrow.bmp")
arrows(4)=loadnewfximage("Resources/Rightarrow.bmp")
for temp=1 to 4
   imagemaskcolour arrows(temp), rgb(255, 255,255)
next temp

enemy=loadnewfximage("Resources/enemy.bmp")
imagemaskcolour enemy, Rgb(255, 255,255)
playerleft=loadnewfximage("Resources/playerfaceleft.bmp")
imagemaskcolour playerleft, rgb(255, 255,255)
playerright=loadnewfximage("Resources/playerfaceright.bmp")
imagemaskcolour playerright, rgb(255,255 ,255)
flatplatform=loadnewfximage("Resources/flatplatform.bmp")
imagemaskcolour flatplatform, rgb(255, 255, 255)
leftplatform=loadnewfximage("Resources/leftplatform.bmp")
imagemaskcolour leftplatform, rgb(255,255, 255)
rightplatform=loadnewfximage("Resources/rightplatform.bmp")
imagemaskcolour rightplatform, rgb(255,255, 255)
projectile=loadnewfximage("Resources/projectile.bmp")
imagemaskcolour projectile, rgb(255, 255, 255)
target=loadnewfximage("Resources/target.bmp")
imagemaskcolour target, rgb(255,255, 255)
targetflash=loadnewfximage("Resources/targetflash.bmp")
imagemaskcolour targetflash, rgb(255,255, 255)
   leftangle=loadnewfximage("Resources/leftangle.bmp")
   imagemaskcolour leftangle, rgb(255, 255, 255)   
   rightangle=loadnewfximage("Resources/rightangle.bmp")
   imagemaskcolour rightangle, rgb(255,255, 255)
   start=loadnewfximage("Resources/start.bmp")
   imagemaskcolour start, rgb(255, 255, 255)
   health=loadnewfximage("Resources/health.bmp")
   imagemaskcolour health, rgb(255, 255, 255)
   verticalwall=loadnewfximage("Resources/verticalwall.bmp")
   imagemaskcolour verticalwall, rgb(255, 255, 255)
   horizontalwall=loadnewfximage("Resources/horizontalwall.bmp")
   imagemaskcolour horizontalwall, rgb(255,255, 255)
   levelup=loadnewfximage("Resources/levelup.bmp")
   imagemaskcolour levelup, rgb(255, 255, 255)
;end of creating resources (WOW THATS ALOT TO MANUALLY TYPE OUT)
[/pbcode]

can be simplified as

[pbcode]

;create resources
dim arrows(4)
arrows(1)=LoadImg("Uparrow.bmp")
arrows(2)=LoadImg("Downarrow.bmp")
arrows(3)=LoadImg("Leftarrow.bmp")
arrows(4)=LoadImg("Rightarrow.bmp")


enemy            =LoadImg("enemy.bmp")
playerleft      =LoadImg("playerfaceleft.bmp")
playerright      =LoadImg("playerfaceright.bmp")
flatplatform   =LoadImg("flatplatform.bmp")
leftplatform   =LoadImg("leftplatform.bmp")
rightplatform   =LoadImg("rightplatform.bmp")
projectile      =LoadImg("projectile.bmp")
target         =LoadImg("target.bmp")
targetflash      =LoadImg("targetflash.bmp")
leftangle      =LoadImg("leftangle.bmp")
rightangle      =LoadImg("rightangle.bmp")
start            =LoadImg("start.bmp")
health         =LoadImg("health.bmp")
verticalwall   =LoadImg("verticalwall.bmp")
horizontalwall   =LoadImg("horizontalwall.bmp")
levelup         =LoadImg("levelup.bmp")


; this version assumes the if the mask is always $ffffff
Psub LoadImg(File$)
   img=loadnewfximage("Resources/"+file$)
   imagemaskcolour img, rgb(255,255,255)
EndPsub img

[/pbcode]
 

The other thing to watch out for, is that the Projectiles are never actually deleted.  So the array just grows and grows and grows...    


Title: Re: Coming soon, platforming sh'mup
Post by: LemonWizard on December 11, 2009, 02:01:22 PM

Yeah that's what I'm going to work out next.
Problem is..
what if one projectile that's further ahead in the array is off screen.
But, the other one, say array index 20 is offscreen.
But array index 28 isn't.

Since it's mostly linear.  How do I remove a single index from an array and redim it without messing with the data?

IE


[pbcode]
dim myarray(10)
myarray(10)=30
myarray(1)=769 ; >getscreenwidth() it's off screen

for temp=1 to 10
if myarray(temp)>getscreenwidth() then reset=true
if myarray(temp)>getscreenheight() then reset=true
if myarray(temp)<0 then reset=true

if reset=true

(Redimming, and re-ordering of array code here?)

endif

next temp

;this example only handles a single dimensioal non typed array but the concept is the same

[/pbcode]
Title: Re: Coming soon, platforming sh'mup
Post by: kevin on December 11, 2009, 04:07:30 PM
 There's a few ways..  


The most elegant is this Linked List Version

[pbcode]
Type tBullet
      x#,y#
      angle#
      speed#
Endtype

Dim Bullet as tBullet list


Do
   Cls 0
   
      ; add a bullet to the list
         Bullet = new tBullet
         Bullet.x =mousex()
         Bullet.y =mousey()
         fireangle#=fireangle#+0.5
         bullet.angle=fireangle#
         bullet.speed=rndrange#(2,5)


      for each Bullet()
         angle#=bullet.angle
         speed#=bullet.speed
         vx#=cos(angle#)*Speed#
         vy#=sin(angle#)*Speed#
   
         x#=Bullet.x+vx#
         y#=Bullet.y+vy#
         if pointinbox(X#,y#,0,0,800,600)=true
            circle x#,y#,5,true         
         
            Bullet.x=x#
            Bullet.y=y#

         else
            ; delete it
            Bullet=Null         
         endif            
      
      next
       

      print getListsize(Bullet())

   Sync
loop
[/pbcode]



 1D array version

  GetFreeCell() in a 1D array version

[pbcode]
Type tBullet
      x#,y#
      angle#
      speed#
Endtype

Dim Bullet(1000) as tBullet


Do
   Cls 0
   
      ; add a bullet to the list
         NewBullet=GetFreeCell(Bullet())
         
         Bullet(NewBullet).x =mousex()
         Bullet(NewBullet).y =mousey()
         fireangle#=fireangle#+0.5
         bullet(NewBullet).angle=fireangle#
         bullet(NewBullet).speed=rndrange#(2,5)
         inc ProjectileCount

      for lp=1 to GetArrayElements(Bullet(),1)
         ; check if this bullet exists
         if Bullet(lp)
            angle#=bullet(lp).angle
            speed#=bullet(lp).speed
            vx#=cos(angle#)*Speed#
            vy#=sin(angle#)*Speed#
   
            x#=Bullet(lp).x+vx#
            y#=Bullet(lp).y+vy#
            if pointinbox(X#,y#,0,0,800,600)=true
               circle x#,y#,5,true         
         
               Bullet(lp).x=x#
               Bullet(lp).y=y#

            else
               ; delete it
                  Bullet(lp)=Null         
                  dec ProjectileCount
            endif            
         endif
      next
       
      print ProjectileCount
      print getarrayelements(Bullet(),1)   ; real array size

   Sync
loop




[/pbcode]


1D Array with element Shuffle..


[pbcode]


Type tBullet
      x#,y#
      angle#
      speed#
Endtype

Dim Bullet(1000) as tBullet


Do
   Cls 0
   
      ; add a bullet to the list
         NewBullet=ProjectileCount+1

      ; init all the fields         
         Bullet(NewBullet).x =mousex()
         Bullet(NewBullet).y =mousey()
         fireangle#=fireangle#+0.5
         bullet(NewBullet).angle=fireangle#
         bullet(NewBullet).speed=rndrange#(2,5)
         ProjectileCount=NewBullet

      for lp=1 to ProjectileCount
         ; check if this bullet exists
            angle#=bullet(lp).angle
            speed#=bullet(lp).speed
            vx#=cos(angle#)*Speed#
            vy#=sin(angle#)*Speed#
   
            x#=Bullet(lp).x+vx#
            y#=Bullet(lp).y+vy#
            if pointinbox(X#,y#,0,0,800,600)=true
               circle x#,y#,5,true         
         
               Bullet(lp).x=x#
               Bullet(lp).y=y#

            else
               ; delete it, swap the last one, with the current one.
                  SwapCell(lp,ProjectileCount)

               ; since we've swap the last item for the current item, we'll have to
               ; move the loop counter and the end of list points back
                  dec lp
                  dec projectileCount
            endif            
      next
       
      print ProjectileCount
      print getarrayelements(Bullet(),1)   ; real array size

   Sync
loop


Function SwapCell(POs1,POs2)
   ; get the pointer to the arrays data
  Ptr=GetArrayPtr(Bullet()) + PBArraystruct_size

   ; read the two banks
  Bank1=PeekInt(Ptr+(Pos1*4))
  Bank2=PeekInt(Ptr+(Pos2*4))

   ; write them back in the swapped order
  PokeInt(Ptr+(Pos1*4),Bank2)
  PokeInt(Ptr+(Pos2*4),Bank1)

EndFunction


[/pbcode]


 You could also use CopyArrayCells() to move the cells backwards and copy over the a dead cell in the array.