TGC Codebase Backup



Character Jumping Routine by JHA

25th Jan 2004 23:12
Summary

Shows how to make your 2D or 3D character(s) Jump. Currently only supports 2D Trig Calculations for X and Y axis.



Description

Shows how to make your 2D / 3D character(s) Jump. Currently only supports 2D Trig Calculations for the X and Y axis though. Good for Platform type games. No media required. It simply creates a Sphere object for controlling. It would be easy to incorporate your own objects.

If someone could modify this to incorporate the Z axis, I would be very greatful.

Code was adapted from Ultimate AMOS book, for the Amiga, written by Jason Holborn. (C)1993. Code adapted by Joe Austin, (c) 2004.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ` ***********************************
` *** Jump Routine Acquired From: ***
` *** Ultimate AMOS (C) 1993      ***
` *** Written By: Jason Holborn   ***
` ***********************************
` *** Adaption By Joe Austin      ***
` *** (C) January 25, 2004        ***
` ***********************************

`Standard Setup Code
Sync On : Sync Rate 75 : Hide Mouse
Set Text Font "Arial" : Set Text Size 14 : Set Text Transparent

`Dimension Variables ---------------------
Dim Direction() as Integer = 0
Dim XOffSet() as Integer = 0
Dim YOffSet() as Integer = 0
Dim Mario(4) as Integer

mSpeed = 4

Mario(0) = 0   `X Position
Mario(1) = 0   `Y Position
Mario(2) = 0   `Status - 0=Walk 1=Jump
Mario(3) = 0   `Jump Angle

`Creation --------------------------------
Make Camera 1
Make Object Sphere 1, 20
Color Object 1, RGB(175,95,5)

`Location
Position Camera 1, 0, 50, 200

`** Main Loop ** -------------------------
Do

   If RightKey() = 1
      If Mario(2) = 0
         Mario(0) = Mario(0) - mSpeed
         Direction() = -1
      EndIf
   EndIf

   If LeftKey() = 1
      If Mario(2) = 0
         Mario(0) = Mario(0) + mSpeed
         Direction() = 1
      EndIf
   EndIf

   If LeftKey() = 0 and RightKey() = 0 Then Direction() = 0

   If SpaceKey() = 1
      If Mario(2) = 0
         Mario(2) = 1
         XOffSet() = Mario(0)
         YOffSet() = Mario(1)
      EndIf
   EndIf

   If Mario(2) = 1 Then Gosub _Jump

   If Mario(0) > 148
      Mario(0) = 148
   EndIf

   If Mario(0) < -148
      Mario(0) = -148
   EndIf

   Position Object 1, Mario(0), Mario(1), Object Position Z(1)
   Point Camera 1, 0, Camera Position Y(1), 0

   Gosub _Display
   Sync
Loop

`SubRoutines - Would have done a Function,
`but you cannot pass an array into one, yet.
_Jump:

   Height = 100             `Max Height of Jump
   Width = 90               `Width of Jump
   JumpSpeed = mSpeed * 2   `Speed of Jump

   If Mario(3) < 181

      `Jump Left
      If Direction() = -1
         X = Cos(Mario(3)) * Width/2
         Mario(0) = XOffSet() + X - Width/2
      EndIf

      `Jump Right
      If Direction() = 1
         X = -Cos(Mario(3)) * Width/2
         Mario(0) = XOffSet() + X + Width/2
      EndIf

      Y = Sin(Mario(3)) * Height/2

      Mario(1) = YOffSet() + Y
      Mario(3) = Mario(3) + JumpSpeed

   Else

      `Reset angle and Status
      Mario(1) = YOffSet()
      Mario(2) = 0
      Mario(3) = 0

   EndIf

Return

_Display:

   Text 0, 0, "Direction: " + Str$(Direction())
   Text 0, 10, "XOffSet: " + Str$(XOffSet())
   Text 0, 20, "Speed: " + Str$(mSpeed)
   Text 0, 30, "X Pos: " + Str$(Mario(0))
   Text 0, 40, "Y Pos: " + Str$(Mario(1))
   Text 0, 50, "Status: " + Str$(Mario(2))
   Text 0, 60, "Jump Angle: " + Str$(Mario(3))
   Text 0, 480, "Press Space to Jump and the Left / Right Arrow Keys to move."
Return