Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Simple Platformer.bmx
  2. 'To Do: airborne and onboard are redundant, adjust for moving platforms
  3. SuperStrict
  4. Graphics 800, 600
  5. '=============================
  6. 'Physics options
  7. Const HEAD_BUMP:Int = True
  8. Const FULL_STOP:Int = True
  9. Const AIRBORNE_JUMP:Int = True
  10. Const REALISTIC_JUMP:Int = False
  11. Const DIME_TURN:Int = False
  12. '=============================
  13.  
  14.  
  15.  
  16.  
  17. 'Very messy beyond this point, I copy and pasted all the Include's
  18.  
  19. '=============================
  20. Function LineInterpolate:Float(v1:Float, v2:Float, t:Float, flags:Int=0, parameters:Float[]=Null)       't=[0...1]
  21.     Return v1*(1-t) + v2*(t)
  22. End Function
  23. '=============================
  24.  
  25. '=============================
  26. Type TVariableController
  27.     Global global_map:TMap = New TMap
  28.     '=============================
  29.     Field key:String
  30.     Field target:Float Ptr
  31.     '=============================
  32.     Function Update_All()
  33.         Local vc:TVariableController
  34.         Local map_enum:TMapEnumerator = global_map.Values()
  35.         For vc = EachIn map_enum
  36.             vc.Update()
  37.         Next
  38.     End Function
  39.     '=============================
  40.     Method Update() Abstract
  41.     '=============================
  42.     Method Destroy()
  43.         global_map.Remove(Self)
  44.     End Method
  45.     '=============================
  46. End Type
  47. '=============================
  48.  
  49.  
  50.  
  51. '=============================
  52. Type TRampController Extends TVariableController
  53.     Field start_value:Float
  54.     Field start_time:Int
  55.     Field end_value:Float
  56.     Field end_time:Int
  57.     Field duration:Int
  58.     Field previous_value:Float
  59.     Field difference:Float
  60.     Field InterpFunc:Float(v1:Float, v2:Float, t:Float, fflags:Int, fparams:Float[])
  61.     Field flags:Int
  62.     Field parameters:Float[]
  63.     '=============================
  64.     Function Find:TRampController(target:Float Ptr)
  65.         Local key:String = String( Int(target) )
  66.         Return TRampController( global_map.ValueForKey(key) )
  67.     End Function
  68.     '=============================
  69.     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[])
  70.         Local ramp:TRampController = Find(target)
  71.         If ramp<>Null
  72.             ramp.Destroy()      'new ramps override old ones
  73.         EndIf
  74.         '=============================
  75.         ramp:TRampController = New TRampController
  76.         ramp.target = target;   Local key:String = String( Int(target) );   global_map.Insert(key, ramp)
  77.         '=============================
  78.         ramp.start_value = target[0]
  79.         ramp.start_time = MilliSecs()
  80.         ramp.end_value = value
  81.         ramp.end_time = ramp.start_time+duration
  82.         ramp.duration = duration
  83.         ramp.difference = ramp.end_value-ramp.start_value
  84.         ramp.previous_value = target[0]
  85.         '=============================
  86.         ramp.InterpFunc = InterpFunc
  87.         ramp.flags = flags
  88.         ramp.parameters = params
  89.         '=============================
  90.         Return ramp
  91.     End Function
  92.     '=============================
  93.     Method Update()
  94.         If target[0]<>previous_value
  95.             Destroy()       'ramp is interrupted (doesn't respond if the variable is 'set' to it's current value)
  96.             Return          'multiple ramps on the same variable would destroy eachother at this step
  97.         EndIf
  98.         Local current_time:Int = MilliSecs()
  99.         If current_time >= end_time
  100.             target[0] = end_value
  101.             Destroy()
  102.             Return
  103.         EndIf
  104.         Local time_passed:Float = current_time-start_time
  105.         target[0] = InterpFunc(start_value, end_value, time_passed/duration, flags, parameters)
  106.         previous_value = target[0]
  107.     End Method
  108.     '=============================
  109. End Type
  110. '=============================
  111. 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)
  112.     Local target:Float Ptr = Varptr(f)
  113.     Local r:TRampController = TRampController.Create(target, value, milliseconds, InterpFunc, flags, params)
  114. End Function
  115. '=============================
  116. Function ReverseRamp(f:Float Var, milliseconds:Int=0)
  117.     Local target:Float Ptr = Varptr(f)
  118.     Local ramp1:TRampController = TRampController.Find(target)
  119.     If ramp1 = Null Then Return
  120.     If milliseconds = 0
  121.         Local time_passed:Int = MilliSecs()-ramp1.start_time
  122.         milliseconds = time_passed
  123.     EndIf
  124.     Local ramp2:TRampController = TRampController.Create(target, ramp1.start_value, milliseconds, ramp1.InterpFunc, ramp1.flags, ramp1.parameters)
  125. End Function
  126. '=============================
  127. Function HaltRamp(f:Float Var)
  128.     Local target:Float Ptr = Varptr(f)
  129.     Local ramp:TRampController = TRampController.Find(target)
  130.     If ramp<>Null
  131.         ramp.Destroy()
  132.         Return
  133.     EndIf
  134. End Function
  135. '=============================
  136.  
  137.  
  138. 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)
  139.     Local x_target:Float Ptr = Varptr(x), y_target:Float Ptr = Varptr(y)
  140.     Local xr:TRampController = TRampController.Create(x_target, x_value, milliseconds, InterpFunc, flags, params)
  141.     Local yr:TRampController = TRampController.Create(y_target, y_value, milliseconds, InterpFunc, flags, params)
  142. End Function
  143.  
  144. '=============================
  145.  
  146. Type BaseAbstract
  147.     Method ToFocus:TFocus() Abstract
  148. End Type
  149. '=============================
  150. Type Base   Extends BaseAbstract    'Generic; trackable object types will extend this
  151.     Method ToFocus:TFocus()
  152.         Return Null
  153.     End Method
  154. End Type
  155. '=============================
  156. Type TFocus
  157.     Field x:Int, y:Int
  158. End Type
  159. '=============================
  160. '=============================
  161. cTPlayer Type.bmx
  162. '=============================
  163. Type TPlayer Extends Base
  164.     Const size:Int = 20'+1
  165.     '=============================
  166.     Const xv_cap:Float = 8
  167.     Const drag:Float = 0.94
  168.     Const terminal_yv:Float = 10
  169.     Const gravity:Float = 0.5
  170.     Const jump:Float = -10
  171.     '=============================
  172.     Const xa:Float = 2
  173.     '=============================
  174.     Field x:Float, y:Float
  175.     Field xv:Float, yv:Float
  176.     Field face:Int
  177.     Field airborne:Int
  178.     '=============================
  179.     Field onboard:TPlatform
  180.     '=============================
  181.     Method Move()
  182.         If KeyDown(KEY_RIGHT)
  183.             xv = xv+xa
  184.             xv = Min(xv, xv_cap)
  185.         EndIf
  186.         If KeyDown(KEY_LEFT)
  187.             xv = xv-xa
  188.             xv = Max(xv, -xv_cap)
  189.         EndIf
  190.         If KeyHit(KEY_UP)
  191.             If AIRBORNE_JUMP Or airborne = False
  192.                 If REALISTIC_JUMP
  193.                     yv :+jump
  194.                     yv = Max(yv, -terminal_yv)
  195.                 Else
  196.                     yv = jump
  197.                 EndIf
  198.                 airborne = True
  199.             EndIf
  200.         EndIf
  201.     End Method
  202.     '=============================
  203.     Method Update()
  204.         x :+ xv
  205.         xv :* drag
  206.         If airborne = True
  207.             yv :+ gravity
  208.             yv = Min(yv, terminal_yv)
  209.             y :+ yv
  210.             Local collisions:Object[] = CollideRect(x-size/2, y-size/2, size+1, size+1, 1, 0)
  211.             If collisions <> Null
  212.                 Local platform:TPlatform = TPlatform(collisions[0])
  213.                 If y < platform.y
  214.                     If yv > 0
  215.                         onboard = platform
  216.                         airborne = False
  217.                         yv = 0
  218.                         y = onboard.Height(x)-size/2
  219.                     EndIf
  220.                 Else
  221.                     If HEAD_BUMP = True
  222.                         If FULL_STOP = True Then yv = 0
  223.                         y = platform.y+platform.h+size/2
  224.                     EndIf
  225.                 EndIf
  226.             EndIf
  227.         Else
  228.             Local h:Int = onboard.Height(x)
  229.             If h > onboard.y + onboard.h        'fallen off platform
  230.                 onboard = Null
  231.                 airborne = True
  232.             Else
  233.                 y = h-size/2
  234.             EndIf
  235.         EndIf
  236.  
  237.     End Method
  238.     '=============================
  239.     Method ToFocus:TFocus()
  240.         Local focus:TFocus = New TFocus
  241.         focus.x = x; focus.y = y
  242.         Return focus
  243.     End Method
  244.     '=============================
  245.     Method Draw()
  246.         Local xy:Int[] = Camera.Map(x-size/2, y-size/2)
  247.         DrawRect xy[0], xy[1], size+1, size+1
  248.         DrawText "Yv:"+yv, 10, 10
  249.     End Method
  250. End Type
  251. '=============================
  252.  
  253. 'TPlatform Type.bmx
  254. '=============================
  255. Type TPlatform Extends Base
  256.     Field x:Int, y:Int
  257.     Field w:Int, h:Int
  258.     '=============================
  259.     Function Create:TPlatform(x:Int, y:Int, w:Int, h:Int)
  260.         Local platform:TPlatform = New TPlatform
  261.         platform.x = x; platform.y = y
  262.         platform.w = w; platform.h = h
  263.         Return platform
  264.     End Function
  265.     '=============================
  266.     Method Height:Int(xx:Int, half_w:Int = 10)
  267.         If (xx < x-half_w) Or (xx > x+w+half_w) Then Return y+h+1
  268.         Return y
  269.     End Method
  270.     '=============================
  271.     Method ToFocus:TFocus()
  272.         Local focus:TFocus = New TFocus
  273.         focus.x = x+w/2; focus.y = y+h/2
  274.         Return focus
  275.     End Method
  276.     '=============================
  277.     Method Draw()
  278.         Local xy:Int[] = Camera.Map(x, y)
  279.         DrawRect xy[0], xy[1], w, h
  280.     End Method
  281. End Type
  282. '=============================
  283.  
  284.  
  285.  
  286. '=============================
  287. Type TRamp Extends TPlatform
  288.     Field h1:Int, h2:Int
  289.     Field xy:Float[]
  290.     '=============================
  291.     Function CreateRamp:TRamp(x:Int, y:Int, w:Int, h1:Int, h2:Int)
  292.         Local ramp:TRamp = New TRamp
  293.         ramp.x = x; ramp.y = y
  294.         ramp.w = w
  295.         ramp.h1 = h1; ramp.h2 = h2
  296.         ramp.h = Max(h1, h2)
  297.         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)]
  298.         Return ramp
  299.     End Function
  300.     '=============================
  301.     Method Height:Int(xx:Int, half_w:Int)
  302.         Local n:Float = Float(xx-x)/Float(w)
  303.         Return y+h-((1-n)*h1 + n*h2)
  304.     End Method
  305.     '=============================
  306.     Method Draw()
  307.         'SetColor 120, 120, 120
  308.         'DrawRect x, y, w, h
  309.         'SetColor 255, 255, 255
  310.         DrawPoly xy
  311.     End Method
  312. End Type
  313. '=============================
  314.  
  315.  
  316. '=============================
  317. 'Level Type.bmx
  318. '=============================
  319. Type Level
  320.     Global platform_list:TList = New TList
  321.     '=============================
  322.     Function Draw()
  323.         Local platform:TPlatform
  324.         For platform = EachIn platform_list
  325.             platform.Draw()
  326.         Next
  327.     End Function
  328.     '=============================
  329.     Function AddPlatform(x:Int, y:Int, w:Int, h:Int)
  330.         Local platform:TPlatform = TPlatform.Create(x, y, w, h)
  331.         platform_list.AddLast(platform)
  332.     End Function
  333.     '=============================
  334.     Function AddRamp(x:Int, y:Int, w:Int, h1:Int, h2:Int)
  335.         Local ramp:TRamp = TRamp.CreateRamp(x, y, w, h1, h2)
  336.         platform_list.AddLast(ramp)
  337.     End Function
  338.     '=============================
  339.     Function Update()
  340.         Local platform:TPlatform
  341.         For platform = EachIn Level.platform_list
  342.             CollideRect platform.x, platform.y, platform.w, platform.h, 0, 1, platform
  343.         Next
  344.     End Function
  345. End Type
  346. '=============================
  347.  
  348. '=============================
  349. 'Camera Type.bmx
  350. '=============================
  351. Type Camera
  352.     Const TRAVEL_TIME:Int = 100
  353.     Const ZOOM_TIME:Int = 1000
  354.     '=============================
  355.     Global x:Float, y:Float     'has to be floats to be compatible with Variable Controllers
  356.     Global zoom:Float = 1
  357.     Global target:Base
  358.     '=============================
  359.     Function Set()
  360.         If target <> Null
  361.             Local focus:TFocus = target.ToFocus()
  362.             'x = focus.x
  363.             'y = focus.y
  364.             RampXY(x, y, focus.x, focus.y, TRAVEL_TIME) 'doesn't work
  365.         Else
  366.             Print Null
  367.         EndIf
  368.         Local view_w:Int = 800/zoom, view_h:Int = 600/zoom
  369.         'SetScale 1, 1
  370.         Local xy:Int[] = Camera.Map(-x+view_w/2, -y+view_h/2)
  371.         SetOrigin xy[0], xy[1]
  372.         SetScale zoom, zoom
  373.     End Function
  374.     '=============================
  375.     Function Track(new_target:Base)
  376.         target = new_target
  377.     End Function
  378.     '=============================
  379.     Function Map:Int[](unmapped_x:Int, unmapped_y:Int)
  380.         Return [Int(unmapped_x*zoom), Int(unmapped_y*zoom)]
  381.     End Function
  382.     '=============================
  383.     Function Set_Zoom(new_zoom:Float)
  384.         Ramp(zoom, new_zoom, ZOOM_TIME)
  385.     End Function
  386.     '=============================
  387. End Type
  388. '=============================
  389.  
  390. '=============================
  391. Type HUD
  392.     Function Draw()
  393.         DrawText "Arrow keys to move, QAZWSX for camera, space to restart.", 10, 580
  394.     End Function
  395. End Type
  396. '=============================
  397.  
  398. '=============================
  399. Global player:TPlayer = New TPlayer
  400. player.x = 400; player.y = 300
  401. player.airborne = True
  402. '=============================
  403. Init_Level()
  404. '=============================
  405.  
  406.  
  407.  
  408. '=============================
  409. While Not KeyHit(KEY_ESCAPE)
  410.     player.Move()
  411.     SetScale 1, 1
  412.     SetOrigin 0, 0
  413.     ResetCollisions
  414.     Level.Update()
  415.     player.Update()
  416.     TVariableController.Update_All()
  417.     '=============================
  418.     If KeyHit(KEY_Q) Then Camera.Track( player )
  419.     If KeyHit(KEY_A) Then Camera.Track( Base(  Level.platform_list.First()  ) )
  420.     If KeyHit(KEY_Z) Then Camera.Track( Base(  Level.platform_list.Last()  ) )
  421.     '=============================
  422.     If KeyHit(KEY_W) Then Camera.Set_Zoom( camera.zoom*2 )
  423.     If KeyHit(KEY_S) Then Camera.Set_Zoom( 1  )
  424.     If KeyHit(KEY_X) Then Camera.Set_Zoom( camera.zoom/2 )
  425.     '=============================
  426.     If KeyHit(KEY_SPACE) Then Init_Level()
  427.     '=============================
  428.     HUD.Draw()
  429.     Camera.Set()
  430.     player.Draw()
  431.     Level.Draw()
  432.     Flip
  433.     Cls
  434. Wend
  435. End
  436. '=============================
  437.  
  438.  
  439.  
  440. '=============================
  441. Function Init_Level()
  442.     Level.AddPlatform(10, 420, 780, 20)
  443.     Level.AddPlatform(10, 300, 200, 20)
  444.     'Level.AddRamp(420, 250, 200, 20, 80)
  445.     Camera.Track(player)
  446.     Camera.Set_Zoom(1)
  447. End Function
  448. '=============================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement