UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on June 05, 2012, 02:47:18 AM

Title: Sonic the Hedgehog Loop Mock Up
Post by: kevin on June 05, 2012, 02:47:18 AM

Sonic the Hedgehog Loop Mock Up


  In this demo, the sonic character runs through the screen and around  the loop.   It works by simply aligning the character to a path, the tricky bit (in this example) was creating a path that matched the backdrop picture or there abouts.  


Video




[pbcode]

; PROJECT : Sonic_Loop_Mock_Up
; AUTHOR  : Kevin Picone
; CREATED : 12/27/2011
; EDITED  : 12/27/2011
; ---------------------------------------------------------------------

   
   // Load the backdrop picture   
   loadimage "gfx\happybirthdaysonic_249856.jpg",1

   // load the walking frames
   Dim WalkFrames(6)
   For lp =0 to 6
         WalkFrames(lp)=LoadNewFXIMage("gfx\sonicwalk"+str$(lp+1)+".bmp")
   next


   // declare type to hold our points along our loop
   Type Vector2D
         x#,y#   
   EndType

   // delcare an array
   Dim Points(100) as Vector2D


   // Set the Base location of the loop curse in the mock up demo
   basex=340
   basey=190


   // Make the path that represents the loop in the mock up
   PointCount=Make_Vert_List(BAseX,BaseY)


   // define the Player structure
   Type tPlayer
         Segment
         Dist#
         x#
         y#      
         sprite
         frameIndex
         CurrentAngle#
   EndType
   

   ; create player variable
   Dim player as tplayer

   ; init it's starting segments and distance moved
   PLayer.Segment=0
   PLayer.Dist#=0

   ; init the sprite the player will use for it's visual representation
   iw=GetIMageWidth(WalkFrames(0))
   ih=GetIMageHeight(WalkFrames(0))
   Spr=NewSprite(0,0,WalkFrames(0))
   SpriteDrawMode Spr,2
   SpriteHandle Spr,iw/-2,-ih
   scalesprite spr,1.5
   spritefilter spr,on
   
   ; store the sprite index in the player structure
   Player.Sprite=Spr


   ; set PB to limit the program to a max of 60 frames per second   
   setfps 60
   

   ;  ------------------------------------------------------------------------
   ;  ------------------------>> Main Loop <<---------------------------------
   ;  ------------------------------------------------------------------------

   do

         ; clear screen to black   
         cls


         ; draw the backdrop
         DrawImage 1,0,0,false

         // ----------------------------------------
         // Show Path
         // ----------------------------------------
         lockbuffer
         for lp =1 to PointCount
               oldx#=points(lp-1).x
               oldy#=points(lp-1).y
               x#=points(lp).x
               y#=points(lp).y
               linec oldx#,oldy#,x#,y#,$ffffff
         next
         unlockbuffer
   
         // ----------------------------------------
         // move player
         // ----------------------------------------
         repeat
         
            MoveFlag=true
            oldx#=points(PLayer.Segment).x#
            oldy#=points(PLayer.Segment).y#
            nextx#=points(PLayer.Segment+1).x#
            nexty#=points(PLayer.Segment+1).y#
            SegLength#=GetDistance2D(oldx#,oldy#,nextx#,nexty#)

            if PLayer.dist#>SegLength#
                  moveFlag=false
                  PLayer.dist#-=SegLength#
                  PLayer.Segment++
                  if PLayer.Segment=>PointCount
                     PLayer.Segment=0
                     Player.dist#=0               
                  endif
                  
            endif

         until MoveFlag=true
         

         // calc normal for our movement vector
         nx#=(Nextx#-oldx#)/SegLength#   
         ny#=(Nexty#-oldy#)/SegLength#   

         // get the players current screen position   
         x#=oldx#+nx#*PLayer.dist#
         y#=oldy#+ny#*PLayer.dist#
            
         // draw a circle at this position
         circle x#,y#,5


         // set the sprites current animation frame
         SpriteImage PLayer.Sprite, WalkFRames(player.frameIndex)
         PLayer.FrameIndex=mod(PLayer.FrameIndex+1,7)


         // position the player sprite at players current position
         PositionSprite Player.sprite,x#,y#


         // get the angle of the floor in this section   
         FloorAngle#=getangle2d(0,0,nx#,ny#)
   
         /// interpolate the players current angle with and change of angle
         CurrentAngle#=curveangle(FloorAngle#,player.currentangle,5)

         // rotate the sprite to this direction   
         RotateSprite PLayer.Sprite,CurrentAngle#

         // remember the player sprites current rotiation angle
         player.currentangle=CurrentAngle#

         // move the player forward 5 units
         PLayer.dist#+=5      


         // draw the sprite now   
         drawsprite PLayer.sprite
         
         
         
      sync

   loop




pSub Make_Vert_List(BaseX,BaseY)

            Dim TempPoints(100) as Vector2D

            Sections=32
            RAdiusY=100
            
            Startingx=310
            StartingY=315
            
            EndingX=StartingY+80
            EndingY=StartingY

            Oldx#=StartingX
            oldy#=StartingY
            Index=0
            
            // map the loop
            for lp =0 to Sections-1
               angle#=lp*(360.0/Sections)
               x#=basex+cos(angle#)*110

               
                if angle#=>90 and Angle#<180
                   RadiusY-=2
                endif
                if angle#=>0 and Angle#<90
                   RadiusY+=2
                endif

               y#=basey+sin(angle#)*RadiusY

               Temppoints(index).x=x#
               Temppoints(index).y=y#
               index++
   
               oldx#=x#
               oldy#=y#
               
            next                  


            // make the vert list that sorta matches the shape
            
            srcIndex=0
            DestIndex=0

            points(destindex).x=0
            points(destindex).y=Startingy

            destindex++
            points(destindex).x=StartingX
            points(destindex).y=Startingy

            for lp =sections-1 to 0 step -1
                  destIndex++
                  SrcIndex =mod(lp+8,Sections)                     
                  points(destindex).x=Temppoints(SrcINdex).x
                  points(destindex).y=Temppoints(srcINdex).y
            next

            destIndex++
            points(destindex).x=EndingX
            points(destindex).y=Endingy


            destIndex++
            points(destindex).x=800
            points(destindex).y=Endingy

         
            drawimage 1,0,0,false

            for lp =1 to destIndex
                  oldx#=points(lp-1).x
                  oldy#=points(lp-1).y
                  x#=points(lp).x
                  y#=points(lp).y

                  linec oldx#,oldy#,x#,y#,$ffffff
            
                  wait 50
                  Sync
            next
            



EndPsub DestIndex


 [/pbcode]


Related To

 This was originally posted in using rotate sprite to animate ambience circles  (http://www.underwaredesign.com/forums/index.php?topic=3765.0)



Download

 Source Code and PlayBASIC project Attached