Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: None | Size: 2.45 KB | Hits: 14 | Expires: Never
Copy text to clipboard
  1. SuperStrict
  2.  
  3. Graphics 800,600
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10. Function rectsOverlap:Int(x0:Int, y0:Int, w0:Int, h0:Int, x1:Int, y1:Int, w1:Int, h1:Int)
  11.  
  12.         If x0 > (x1 + w1) Or (x0 + w0) < x1 Or y0 > (y1 + h1) Or (y0 + h0) < y1 Then Return False
  13.         Return True
  14.  
  15. End Function
  16.  
  17.  
  18. Type Entity
  19. 'an entity is something that collides with or causes collisions
  20.         Global dynamicList:TList = CreateList() 'for entities that collide with things IE players/enemies.
  21.         Global staticList:TList = CreateList() 'for entities that don't need collision detection/response IE walls.
  22.        
  23.         Field xPos:Float, yPos:Float
  24.         Field xVel:Float, yVel:Float
  25.        
  26.         Field width:Int, height:Float
  27.        
  28.        
  29.         Method handleDynamicEntities()
  30.                 Local inc:Float 'helper variable, do we move away or toward collision.
  31.                
  32.                 'apply gravity
  33.                 yVel:+1
  34.                
  35.                
  36.                 xPos:+ xVel
  37.                 inc = 1
  38.                 If(xVel > 0) inc = -1
  39.                 For Local o:Entity = EachIn staticList
  40.                         While rectsOverlap( xPos, yPos, width, height, o.xPos, o.yPos, o.width, o.height)
  41.                                 xPos:+inc
  42.                                 xVel = 0
  43.                         Wend
  44.                 Next
  45.                
  46.                 yPos:+ yVel
  47.                 inc = 1
  48.                 If(yVel > 0) inc = -1
  49.                 For Local o:Entity = EachIn staticList
  50.                         While rectsOverlap( xPos, yPos, width, height, o.xPos, o.yPos, o.width, o.height)
  51.                                 yPos:+inc
  52.                                 yVel = 0
  53.                         Wend
  54.                 Next
  55.                
  56.                
  57.                 'reset xVelocity so we don't have sliding
  58.                 xVel = 0
  59.         End Method
  60.        
  61.         Method draw()
  62.                 DrawRect xPos, yPos, width, height
  63.         End Method
  64.        
  65.        
  66.        
  67.        
  68.         Function createWall:Entity(x:Int, y:Int, w:Int, h:Int)
  69.                 Local e:Entity = New Entity
  70.                 e.xPos = x
  71.                 e.yPos = y
  72.                 e.width = w
  73.                 e.height = h
  74.                 staticList.addLast(e)
  75.                 Return e
  76.         End Function
  77.  
  78.         Function createDynamic:Entity(x:Int, y:Int, w:Int, h:Int)
  79.                 Local e:Entity = New Entity
  80.                 e.xPos = x
  81.                 e.yPos = y
  82.                 e.width = w
  83.                 e.height = h
  84.                 dynamicList.addLast(e)
  85.                 Return e
  86.         End Function   
  87.        
  88.         Function updateEntities()
  89.                 For Local o:Entity = EachIn dynamicList
  90.                         o.handleDynamicEntities()
  91.                 Next
  92.         End Function
  93.        
  94.         Function drawEntities()
  95.                 For Local o:Entity = EachIn dynamicList
  96.                         o.draw()
  97.                 Next
  98.                 For Local o:Entity = EachIn staticList
  99.                         o.draw()
  100.                 Next
  101.         End Function
  102.  
  103.  
  104.  
  105.  
  106. End Type
  107.  
  108.  
  109. Entity.createWall(0,500,1000,40)
  110. Entity.createWall(0,10,40,1000)
  111.  
  112. Global player:Entity = Entity.createDynamic(200,200,40,40)
  113.  
  114. While(Not KeyHit(KEY_ESCAPE))
  115.         Cls
  116.                 If(KeyDown(KEY_LEFT)) player.xVel = -6
  117.                 If(KeyDown(KEY_RIGHT)) player.xVel = 6
  118.                 If(KeyHit(KEY_UP)) player.yVel = -18
  119.                
  120.                 Entity.updateEntities()
  121.                 Entity.drawEntities()
  122.         Flip
  123. Wend