-
Notifications
You must be signed in to change notification settings - Fork 1
/
Topdown map collision.bb
157 lines (143 loc) · 2.9 KB
/
Topdown map collision.bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
AppTitle "Topdown RPG player map collision - cursor keys to move."
Graphics 640,480,16,2
SetBuffer BackBuffer()
Global mw = 20
Global mh = 15
Global tw = 32
Global th = 32
Global px = 0
Global py = 0
Global pw = tw
Global ph = th
Dim map(mw,mh)
readlevel()
While KeyDown(1) = False
Cls
moveplayer
playeritemcollision
drawlevel
drawplayer
Flip
Wend
End
Function moveplayer()
Local x=0
Local y=0
If KeyDown(200) ; up
y=-1
End If
If KeyDown(205) ; right
x=1
End If
If KeyDown(208) ; down
y=1
End If
If KeyDown(203) ; left
x=-1
End If
If px+x < 0 Then x=0
If px+x > mw*tw Then x=0
If py+y < 0 Then y=0
If py+y > mh*th Then y=0
If playermapcollision(px+x,py) = False
px=px+x
End If
If playermapcollision(px,py+y) = False
py=py+y
End If
End Function
Function playermapcollision(x1,y1)
Local cx=x1/tw
Local cy=y1/th
For y2=cy-1 To cy+1
For x2=cx-1 To cx+1
If x2>=0 And x2<=mw And y2>=0 And y2<=mh
If map(x2,y2) = 1 ; is the map around the player a 1 value
If RectsOverlap(x2*tw,y2*th,tw,th,x1,y1,pw,ph)
;
; Here the player is inside a wall
; a value 1 on the map
;
Return True
;
End If
End If
End If
Next
Next
; no collision occured
Return False
End Function
Function playeritemcollision()
Local cx=px/tw
Local cy=py/th
For y2=cy-1 To cy+1
For x2=cx-1 To cx+1
If x2>=0 And x2<=mw And y2>=0 And y2<=mh
If map(x2,y2) = 3 ; is the map around the player a 3 value
If RectsOverlap(x2*tw+8,y2*th+8,tw-16,th-16,px,py,pw,ph)
;
; Here the player touches a map item (3)
; We remove it from the map
;
map(x2,y2) = 0
End If
End If
End If
Next
Next
End Function
Function drawplayer()
Color 0,0,255
Oval px,py,pw,ph
End Function
Function drawlevel()
For y=0 To mh-1
For x=0 To mw-1
If map(x,y) = 1
Color 255,255,255
Rect x*tw,y*th,tw,th,True
End If
If map(x,y) = 2
Color 0,255,0
x1=x*tw
y1=y*th
Line x1+tw/2,y1,x1+tw,y1+th
Line x1+tw,y1+th,x1,y1+th
Line x1,y1+th,x1+tw/2,y1
End If
If map(x,y) = 3
Color 200,180,10
Oval x*tw+4,y*th+4,tw-8,th-8
End If
Next
Next
End Function
Function readlevel(level=1)
Select level
Case 1
Restore level1
End Select
For y=0 To mh-1
For x=0 To mw-1
Read a
map(x,y) = a
Next
Next
End Function
.level1
Data 0,0,0,3,3,3,0,1,1,1,1,0,1,1,1,1,1,1,1,1
Data 0,1,1,1,0,0,0,1,0,0,1,0,1,0,0,0,0,3,3,1
Data 0,1,1,3,0,2,0,1,0,0,1,0,1,0,0,0,0,3,3,1
Data 0,1,1,0,0,0,0,1,0,1,1,0,1,1,0,1,1,1,1,1
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0
Data 0,1,0,0,1,0,0,0,0,0,2,0,0,0,0,0,0,1,0,0
Data 0,1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1
Data 0,1,0,3,1,0,0,2,0,0,0,0,0,0,1,0,0,0,0,0
Data 0,1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0
Data 0,0,1,1,1,1,1,1,1,1,0,0,2,0,1,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0