Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Simple Platformer.bmx
- 'To Do: airborne and onboard are redundant, adjust for moving platforms
- SuperStrict
- Graphics 800, 600
- '=============================
- 'Physics options
- Const HEAD_BUMP:Int = True
- Const FULL_STOP:Int = True
- Const AIRBORNE_JUMP:Int = True
- Const REALISTIC_JUMP:Int = False
- Const DIME_TURN:Int = False
- '=============================
- 'Very messy beyond this point, I copy and pasted all the Include's
- '=============================
- Function LineInterpolate:Float(v1:Float, v2:Float, t:Float, flags:Int=0, parameters:Float[]=Null) 't=[0...1]
- Return v1*(1-t) + v2*(t)
- End Function
- '=============================
- '=============================
- Type TVariableController
- Global global_map:TMap = New TMap
- '=============================
- Field key:String
- Field target:Float Ptr
- '=============================
- Function Update_All()
- Local vc:TVariableController
- Local map_enum:TMapEnumerator = global_map.Values()
- For vc = EachIn map_enum
- vc.Update()
- Next
- End Function
- '=============================
- Method Update() Abstract
- '=============================
- Method Destroy()
- global_map.Remove(Self)
- End Method
- '=============================
- End Type
- '=============================
- '=============================
- Type TRampController Extends TVariableController
- Field start_value:Float
- Field start_time:Int
- Field end_value:Float
- Field end_time:Int
- Field duration:Int
- Field previous_value:Float
- Field difference:Float
- Field InterpFunc:Float(v1:Float, v2:Float, t:Float, fflags:Int, fparams:Float[])
- Field flags:Int
- Field parameters:Float[]
- '=============================
- Function Find:TRampController(target:Float Ptr)
- Local key:String = String( Int(target) )
- Return TRampController( global_map.ValueForKey(key) )
- End Function
- '=============================
- Function Create:TRampController(target:Float Ptr, value:Float, duration:Int, InterpFunc:Float(v1:Float, v2:Float, t:Float, fflags:Int, fparams:Float[]), flags:Int, params:Float[])
- Local ramp:TRampController = Find(target)
- If ramp<>Null
- ramp.Destroy() 'new ramps override old ones
- EndIf
- '=============================
- ramp:TRampController = New TRampController
- ramp.target = target; Local key:String = String( Int(target) ); global_map.Insert(key, ramp)
- '=============================
- ramp.start_value = target[0]
- ramp.start_time = MilliSecs()
- ramp.end_value = value
- ramp.end_time = ramp.start_time+duration
- ramp.duration = duration
- ramp.difference = ramp.end_value-ramp.start_value
- ramp.previous_value = target[0]
- '=============================
- ramp.InterpFunc = InterpFunc
- ramp.flags = flags
- ramp.parameters = params
- '=============================
- Return ramp
- End Function
- '=============================
- Method Update()
- If target[0]<>previous_value
- Destroy() 'ramp is interrupted (doesn't respond if the variable is 'set' to it's current value)
- Return 'multiple ramps on the same variable would destroy eachother at this step
- EndIf
- Local current_time:Int = MilliSecs()
- If current_time >= end_time
- target[0] = end_value
- Destroy()
- Return
- EndIf
- Local time_passed:Float = current_time-start_time
- target[0] = InterpFunc(start_value, end_value, time_passed/duration, flags, parameters)
- previous_value = target[0]
- End Method
- '=============================
- End Type
- '=============================
- Function Ramp(f:Float Var, value:Float, milliseconds:Int, InterpFunc:Float(v1:Float, v2:Float, t:Float, fflags:Int, fparams:Float[]) = LineInterpolate, flags:Int = 0, params:Float[] = Null)
- Local target:Float Ptr = Varptr(f)
- Local r:TRampController = TRampController.Create(target, value, milliseconds, InterpFunc, flags, params)
- End Function
- '=============================
- Function ReverseRamp(f:Float Var, milliseconds:Int=0)
- Local target:Float Ptr = Varptr(f)
- Local ramp1:TRampController = TRampController.Find(target)
- If ramp1 = Null Then Return
- If milliseconds = 0
- Local time_passed:Int = MilliSecs()-ramp1.start_time
- milliseconds = time_passed
- EndIf
- Local ramp2:TRampController = TRampController.Create(target, ramp1.start_value, milliseconds, ramp1.InterpFunc, ramp1.flags, ramp1.parameters)
- End Function
- '=============================
- Function HaltRamp(f:Float Var)
- Local target:Float Ptr = Varptr(f)
- Local ramp:TRampController = TRampController.Find(target)
- If ramp<>Null
- ramp.Destroy()
- Return
- EndIf
- End Function
- '=============================
- Function RampXY(x:Float Var, y:Float Var, x_value:Float, y_value:Float, milliseconds:Int, InterpFunc:Float(v1:Float, v2:Float, t:Float, fflags:Int, fparams:Float[]) = LineInterpolate, flags:Int = 0, params:Float[] = Null)
- Local x_target:Float Ptr = Varptr(x), y_target:Float Ptr = Varptr(y)
- Local xr:TRampController = TRampController.Create(x_target, x_value, milliseconds, InterpFunc, flags, params)
- Local yr:TRampController = TRampController.Create(y_target, y_value, milliseconds, InterpFunc, flags, params)
- End Function
- '=============================
- Type BaseAbstract
- Method ToFocus:TFocus() Abstract
- End Type
- '=============================
- Type Base Extends BaseAbstract 'Generic; trackable object types will extend this
- Method ToFocus:TFocus()
- Return Null
- End Method
- End Type
- '=============================
- Type TFocus
- Field x:Int, y:Int
- End Type
- '=============================
- '=============================
- cTPlayer Type.bmx
- '=============================
- Type TPlayer Extends Base
- Const size:Int = 20'+1
- '=============================
- Const xv_cap:Float = 8
- Const drag:Float = 0.94
- Const terminal_yv:Float = 10
- Const gravity:Float = 0.5
- Const jump:Float = -10
- '=============================
- Const xa:Float = 2
- '=============================
- Field x:Float, y:Float
- Field xv:Float, yv:Float
- Field face:Int
- Field airborne:Int
- '=============================
- Field onboard:TPlatform
- '=============================
- Method Move()
- If KeyDown(KEY_RIGHT)
- xv = xv+xa
- xv = Min(xv, xv_cap)
- EndIf
- If KeyDown(KEY_LEFT)
- xv = xv-xa
- xv = Max(xv, -xv_cap)
- EndIf
- If KeyHit(KEY_UP)
- If AIRBORNE_JUMP Or airborne = False
- If REALISTIC_JUMP
- yv :+jump
- yv = Max(yv, -terminal_yv)
- Else
- yv = jump
- EndIf
- airborne = True
- EndIf
- EndIf
- End Method
- '=============================
- Method Update()
- x :+ xv
- xv :* drag
- If airborne = True
- yv :+ gravity
- yv = Min(yv, terminal_yv)
- y :+ yv
- Local collisions:Object[] = CollideRect(x-size/2, y-size/2, size+1, size+1, 1, 0)
- If collisions <> Null
- Local platform:TPlatform = TPlatform(collisions[0])
- If y < platform.y
- If yv > 0
- onboard = platform
- airborne = False
- yv = 0
- y = onboard.Height(x)-size/2
- EndIf
- Else
- If HEAD_BUMP = True
- If FULL_STOP = True Then yv = 0
- y = platform.y+platform.h+size/2
- EndIf
- EndIf
- EndIf
- Else
- Local h:Int = onboard.Height(x)
- If h > onboard.y + onboard.h 'fallen off platform
- onboard = Null
- airborne = True
- Else
- y = h-size/2
- EndIf
- EndIf
- End Method
- '=============================
- Method ToFocus:TFocus()
- Local focus:TFocus = New TFocus
- focus.x = x; focus.y = y
- Return focus
- End Method
- '=============================
- Method Draw()
- Local xy:Int[] = Camera.Map(x-size/2, y-size/2)
- DrawRect xy[0], xy[1], size+1, size+1
- DrawText "Yv:"+yv, 10, 10
- End Method
- End Type
- '=============================
- 'TPlatform Type.bmx
- '=============================
- Type TPlatform Extends Base
- Field x:Int, y:Int
- Field w:Int, h:Int
- '=============================
- Function Create:TPlatform(x:Int, y:Int, w:Int, h:Int)
- Local platform:TPlatform = New TPlatform
- platform.x = x; platform.y = y
- platform.w = w; platform.h = h
- Return platform
- End Function
- '=============================
- Method Height:Int(xx:Int, half_w:Int = 10)
- If (xx < x-half_w) Or (xx > x+w+half_w) Then Return y+h+1
- Return y
- End Method
- '=============================
- Method ToFocus:TFocus()
- Local focus:TFocus = New TFocus
- focus.x = x+w/2; focus.y = y+h/2
- Return focus
- End Method
- '=============================
- Method Draw()
- Local xy:Int[] = Camera.Map(x, y)
- DrawRect xy[0], xy[1], w, h
- End Method
- End Type
- '=============================
- '=============================
- Type TRamp Extends TPlatform
- Field h1:Int, h2:Int
- Field xy:Float[]
- '=============================
- Function CreateRamp:TRamp(x:Int, y:Int, w:Int, h1:Int, h2:Int)
- Local ramp:TRamp = New TRamp
- ramp.x = x; ramp.y = y
- ramp.w = w
- ramp.h1 = h1; ramp.h2 = h2
- ramp.h = Max(h1, h2)
- ramp.xy = [Float(x), Float(y+ramp.h), Float(x), Float(y+ramp.h-h1), Float(x+w), Float(y+ramp.h-h2), Float(x+w), Float(y+ramp.h)]
- Return ramp
- End Function
- '=============================
- Method Height:Int(xx:Int, half_w:Int)
- Local n:Float = Float(xx-x)/Float(w)
- Return y+h-((1-n)*h1 + n*h2)
- End Method
- '=============================
- Method Draw()
- 'SetColor 120, 120, 120
- 'DrawRect x, y, w, h
- 'SetColor 255, 255, 255
- DrawPoly xy
- End Method
- End Type
- '=============================
- '=============================
- 'Level Type.bmx
- '=============================
- Type Level
- Global platform_list:TList = New TList
- '=============================
- Function Draw()
- Local platform:TPlatform
- For platform = EachIn platform_list
- platform.Draw()
- Next
- End Function
- '=============================
- Function AddPlatform(x:Int, y:Int, w:Int, h:Int)
- Local platform:TPlatform = TPlatform.Create(x, y, w, h)
- platform_list.AddLast(platform)
- End Function
- '=============================
- Function AddRamp(x:Int, y:Int, w:Int, h1:Int, h2:Int)
- Local ramp:TRamp = TRamp.CreateRamp(x, y, w, h1, h2)
- platform_list.AddLast(ramp)
- End Function
- '=============================
- Function Update()
- Local platform:TPlatform
- For platform = EachIn Level.platform_list
- CollideRect platform.x, platform.y, platform.w, platform.h, 0, 1, platform
- Next
- End Function
- End Type
- '=============================
- '=============================
- 'Camera Type.bmx
- '=============================
- Type Camera
- Const TRAVEL_TIME:Int = 100
- Const ZOOM_TIME:Int = 1000
- '=============================
- Global x:Float, y:Float 'has to be floats to be compatible with Variable Controllers
- Global zoom:Float = 1
- Global target:Base
- '=============================
- Function Set()
- If target <> Null
- Local focus:TFocus = target.ToFocus()
- 'x = focus.x
- 'y = focus.y
- RampXY(x, y, focus.x, focus.y, TRAVEL_TIME) 'doesn't work
- Else
- Print Null
- EndIf
- Local view_w:Int = 800/zoom, view_h:Int = 600/zoom
- 'SetScale 1, 1
- Local xy:Int[] = Camera.Map(-x+view_w/2, -y+view_h/2)
- SetOrigin xy[0], xy[1]
- SetScale zoom, zoom
- End Function
- '=============================
- Function Track(new_target:Base)
- target = new_target
- End Function
- '=============================
- Function Map:Int[](unmapped_x:Int, unmapped_y:Int)
- Return [Int(unmapped_x*zoom), Int(unmapped_y*zoom)]
- End Function
- '=============================
- Function Set_Zoom(new_zoom:Float)
- Ramp(zoom, new_zoom, ZOOM_TIME)
- End Function
- '=============================
- End Type
- '=============================
- '=============================
- Type HUD
- Function Draw()
- DrawText "Arrow keys to move, QAZWSX for camera, space to restart.", 10, 580
- End Function
- End Type
- '=============================
- '=============================
- Global player:TPlayer = New TPlayer
- player.x = 400; player.y = 300
- player.airborne = True
- '=============================
- Init_Level()
- '=============================
- '=============================
- While Not KeyHit(KEY_ESCAPE)
- player.Move()
- SetScale 1, 1
- SetOrigin 0, 0
- ResetCollisions
- Level.Update()
- player.Update()
- TVariableController.Update_All()
- '=============================
- If KeyHit(KEY_Q) Then Camera.Track( player )
- If KeyHit(KEY_A) Then Camera.Track( Base( Level.platform_list.First() ) )
- If KeyHit(KEY_Z) Then Camera.Track( Base( Level.platform_list.Last() ) )
- '=============================
- If KeyHit(KEY_W) Then Camera.Set_Zoom( camera.zoom*2 )
- If KeyHit(KEY_S) Then Camera.Set_Zoom( 1 )
- If KeyHit(KEY_X) Then Camera.Set_Zoom( camera.zoom/2 )
- '=============================
- If KeyHit(KEY_SPACE) Then Init_Level()
- '=============================
- HUD.Draw()
- Camera.Set()
- player.Draw()
- Level.Draw()
- Flip
- Cls
- Wend
- End
- '=============================
- '=============================
- Function Init_Level()
- Level.AddPlatform(10, 420, 780, 20)
- Level.AddPlatform(10, 300, 200, 20)
- 'Level.AddRamp(420, 250, 200, 20, 80)
- Camera.Track(player)
- Camera.Set_Zoom(1)
- End Function
- '=============================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement