- SuperStrict
- Graphics 800,600
- Function rectsOverlap:Int(x0:Int, y0:Int, w0:Int, h0:Int, x1:Int, y1:Int, w1:Int, h1:Int)
- If x0 > (x1 + w1) Or (x0 + w0) < x1 Or y0 > (y1 + h1) Or (y0 + h0) < y1 Then Return False
- Return True
- End Function
- Type Entity
- 'an entity is something that collides with or causes collisions
- Global dynamicList:TList = CreateList() 'for entities that collide with things IE players/enemies.
- Global staticList:TList = CreateList() 'for entities that don't need collision detection/response IE walls.
- Field xPos:Float, yPos:Float
- Field xVel:Float, yVel:Float
- Field width:Int, height:Float
- Method handleDynamicEntities()
- Local inc:Float 'helper variable, do we move away or toward collision.
- 'apply gravity
- yVel:+1
- xPos:+ xVel
- inc = 1
- If(xVel > 0) inc = -1
- For Local o:Entity = EachIn staticList
- While rectsOverlap( xPos, yPos, width, height, o.xPos, o.yPos, o.width, o.height)
- xPos:+inc
- xVel = 0
- Wend
- Next
- yPos:+ yVel
- inc = 1
- If(yVel > 0) inc = -1
- For Local o:Entity = EachIn staticList
- While rectsOverlap( xPos, yPos, width, height, o.xPos, o.yPos, o.width, o.height)
- yPos:+inc
- yVel = 0
- Wend
- Next
- 'reset xVelocity so we don't have sliding
- xVel = 0
- End Method
- Method draw()
- DrawRect xPos, yPos, width, height
- End Method
- Function createWall:Entity(x:Int, y:Int, w:Int, h:Int)
- Local e:Entity = New Entity
- e.xPos = x
- e.yPos = y
- e.width = w
- e.height = h
- staticList.addLast(e)
- Return e
- End Function
- Function createDynamic:Entity(x:Int, y:Int, w:Int, h:Int)
- Local e:Entity = New Entity
- e.xPos = x
- e.yPos = y
- e.width = w
- e.height = h
- dynamicList.addLast(e)
- Return e
- End Function
- Function updateEntities()
- For Local o:Entity = EachIn dynamicList
- o.handleDynamicEntities()
- Next
- End Function
- Function drawEntities()
- For Local o:Entity = EachIn dynamicList
- o.draw()
- Next
- For Local o:Entity = EachIn staticList
- o.draw()
- Next
- End Function
- End Type
- Entity.createWall(0,500,1000,40)
- Entity.createWall(0,10,40,1000)
- Global player:Entity = Entity.createDynamic(200,200,40,40)
- While(Not KeyHit(KEY_ESCAPE))
- Cls
- If(KeyDown(KEY_LEFT)) player.xVel = -6
- If(KeyDown(KEY_RIGHT)) player.xVel = 6
- If(KeyHit(KEY_UP)) player.yVel = -18
- Entity.updateEntities()
- Entity.drawEntities()
- Flip
- Wend
