I know the overlap function calls drawrect_hollow a total of (enemy.count * tile.count) times and its obviously not something i plan to have in any actual game, but the problem I have is this worked so much better on the blitzmax demo than now. The coding is exactly the same, except the demo version used graphics 640,480,0 instead of graphics 640,480.
I also removed all image references for the sake of it working on other peoples machines. I dont have any fps counts or anything but the slow down is extremely obvious.
' enemy prototype
Graphics 640,480
SetMaskColor 255,0,255
'Global gfxEnemy:TImage = LoadAnimImage("Images\enemy_1.png",32,32,0,8)
'SetImageHandle gfxEnemy,15,31
Type Wall
Field x,y
Method Draw()
DrawRect x,y,32,32
End Method
End Type
Global Wall_List:TList = CreateList()
Type Enemy
Field x#,y#,xs#,ys#,d,id
Field af,atimer
Field modea,modeb
Method Draw()
SetColor 255,255,255
'DrawImage gfxEnemy,x,y,af+(4*d)
DrawRect x-5,y-18,10,18
SetColor 255,0,0
End Method
Method Animation()
If atimer + 100 < MilliSecs() Then af = af+1; atimer = MilliSecs()
If af = 4 Then af = 0
End Method
Method Movement()
If d = 0 Then x = x - xs
If d = 1 Then x = x + xs
y = y + ys
ys = ys + 0.1
If ys > 3 Then ys = 3
End Method
End Type
Global Enemy_List:TList = CreateList()
Function add_wall(x,y)
Local NewWall:Wall
NewWall = New Wall
NewWall.x = x
NewWall.y = y
Wall_List.AddLast( NewWall )
End Function
Function add_enemy(x,y)
Local NewEnemy:Enemy
NewEnemy = New Enemy
NewEnemy.x = x
NewEnemy.y = y
NewEnemy.xs = Rnd(0.5,2)
NewEnemy.ys = Rnd(0,-2)
NewEnemy.d = Rand(0,1)
NewEnemy.af = 0
NewEnemy.atimer = MilliSecs()
NewEnemy.modea = Rand(0,2)
NewEnemy.modeb = 0
Enemy_List.AddLast( NewEnemy )
End Function
Function Enemy_Movement()
For TEnemy:Enemy = EachIn Enemy_List
Tenemy.Animation()
Local old_y# = TEnemy.y
Local old_x# = Tenemy.x
Local ex# = Tenemy.x
Local ey# = Tenemy.y
Local d = Tenemy.d
Local modea = Tenemy.modea
Local low_col = False
Local left_col = False
Local right_col = False
Local left_edge_col = 0
Local Right_edge_col = 0
Local left_jump = False
Local right_jump = False
Local left_jump_obstacle = False
Local right_jump_obstacle = False
Local overlap_low_col = False
TEnemy.Movement()
' Bot/Left/Right collisions
For Local TWall:Wall = EachIn Wall_List
If OverLap (ex-5, ey+1, ex+5, ey+2, Twall.x, Twall.y, Twall.x+32, Twall.y+32)
low_col = True
EndIf
If OverLap (ex-5, ey, ex+5, ey+1, Twall.x, Twall.y, Twall.x+32, Twall.y+32)
overlap_low_col = True
EndIf
If OverLap (ex-10, ey-10, ex-7, ey-6, Twall.x, Twall.y, Twall.x+32, Twall.y+32)
left_col = True
EndIf
If OverLap (ex+7, ey-10, ex+10, ey-6, Twall.x, Twall.y, Twall.x+32, Twall.y+32)
right_col = True
EndIf
If modea = 1 Or modea = 2
' This stays at 0 If at an edge
If OverLap (ex-12, ey+5, ex-8, ey+7, Twall.x, Twall.y, Twall.x+32, Twall.y+32)
left_edge_col = left_edge_col + 1
EndIf
' This stays at 0 If at an edge
If OverLap (ex+8, ey+5, ex+12, ey+7, Twall.x, Twall.y, Twall.x+32, Twall.y+32)
right_edge_col = right_edge_col + 1
EndIf
EndIf
' If there is a platform that the enemy can jump To
If OverLap (ex-44, ey+5, ex-42, ey+7, Twall.x, Twall.y, Twall.x+32, Twall.y+32)
left_jump = True
EndIf
' If there is a platform the enemy can jump to
If OverLap (ex+42, ey+5, ex+44, ey+7, Twall.x, Twall.y, Twall.x+32, Twall.y+32)
right_jump = True
EndIf
Next
' Potentially overwritten by events to come
If low_col = True Then TEnemy.ys = 0;
If overlap_low_col Then TEnemy.y = ey - 1
' make the guy turn around
If d = 1 And modea = 1 And low_col And right_edge_col = 0 Then TEnemy.d = Not d; tenemy.x = old_x
If d = 0 And modea = 1 And low_col And left_edge_col = 0 Then TEnemy.d = Not d; tenemy.x = old_x
' Make the guy jump
If d = 1 And modea = 2 And low_col And right_jump And right_edge_col = 0 Then Tenemy.ys = -5
If d = 0 And modea = 2 And low_col And left_jump And left_edge_col = 0 Then Tenemy.ys = -5
' turn around
If d = 0 And left_col Then TEnemy.d = Not d; tenemy.x = old_x
If d = 1 And right_col Then TEnemy.d = Not d; tenemy.x = old_x
Next
End Function
Function Enemy_Render()
For TEnemy:Enemy = EachIn Enemy_List
TEnemy.Draw()
Next
End Function
Function Wall_Render()
For TWall:Wall = EachIn Wall_List
TWall.Draw()
Next
End Function
Function OverLap (x0#, y0#, x1#, y1#, x2#, y2#, x3#, y3#)
Drawrect_hollow(x0,y0,x1,y1)
If x0 > x3 Or x1 < x2 Then Return False
If y0 > y3 Or y1 < y2 Then Return False
Return True
End Function
SetColor 255,255,255
SetBlend Alphablend
Create_Walls()
While Not KeyDown(key_escape)
Local mx = MouseX()
Local my = MouseY()
If MouseHit(1) Then add_enemy(mx,my)
SetColor 255,255,255
Wall_Render()
Enemy_Movement()
Enemy_Render()
Flip;Cls
Wend
Function Create_Walls()
For yy = 0 To 480 Step 32
add_wall(0,yy)
Next
For yy = 0 To 480 Step 32
add_wall(608,yy)
Next
For xx = 32 To 32 + 63 Step 32
add_wall(xx,272)
Next
For xx = 128 To 128 + 63 Step 32
add_wall(xx,272)
Next
For xx = 256 To 256 + 63 Step 32
add_wall(xx,272)
Next
For xx = 32 To 607 Step 32
add_wall(xx,480-32)
Next
End Function
Function DrawRect_Hollow(x1,y1,x2,y2,crop=0)
SetColor 255,0,0
DrawLine x1,y1,x2,y1
DrawLine x1,y1,x1,y2
DrawLine x2,y1,x2,y2
DrawLine x1,y2,x2,y2
End Function