summaryrefslogtreecommitdiff
path: root/vectorgfx/test.bmx
blob: 191a81b504dfd8fe2ab062e67ae31eb4a197ee00 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
' $Id$

Import noddybox.vectorgfx
Strict

Const SCRW=800'1280
Const SCRH=600'1024

SetGraphicsDriver GLMax2DDriver()
Graphics SCRW,SCRH,32,60' Or HARDSYNC
HideMouse()

SetBlend(ALPHABLEND)

Const SIZE:Int=20
Const MSIZE:Int=40

Local o1:TVectorGfxObject=New TVectorGfxObject
Local o2:TVectorGfxObject=New TVectorGfxObject

'Local o1:TVectorGfxObject=TVectorGfxObject.Load("obj1.2d")
'Local o2:TVectorGfxObject=TVectorGfxObject.Load("obj2.2d")

Local cm:TVectorGfxCollisionMap=TVectorGfxCollisionMap.Create(MSIZE,MSIZE)
'Local cm:TVectorGfxCollisionMap=TVectorGfxCollisionMap.Create(SCRW,SCRH)

Local l:TList=CreateList()

'Rem
l.Clear()
l.AddLast(TVectorGfxPoint.Create(-SIZE,-SIZE))
l.AddLast(TVectorGfxPoint.Create(SIZE,-SIZE))
l.AddLast(TVectorGfxPoint.Create(SIZE,SIZE))
l.AddLast(TVectorGfxPoint.Create(-SIZE,SIZE))
o1.SetPoints(l.ToArray())

l.Clear()
l.AddLast(TVectorGfxLine.Create(0,1,255,0,0,1))
l.AddLast(TVectorGfxLine.Create(1,2,0,255,0,2))
l.AddLast(TVectorGfxLine.Create(2,3,255,0,0,1))
l.AddLast(TVectorGfxLine.Create(3,0,0,255,0,2))
o1.SetLines(l.ToArray())
o1.x=400
o1.y=300
o1.Save("obj1.2d")

l.Clear()
l.AddLast(TVectorGfxPoint.Create(-SIZE/2,-SIZE/2))
l.AddLast(TVectorGfxPoint.Create(SIZE/2,-SIZE/2))
l.AddLast(TVectorGfxPoint.Create(SIZE/2,SIZE/2))
l.AddLast(TVectorGfxPoint.Create(-SIZE/2,SIZE/2))
o2.SetPoints(l.ToArray())

l.Clear()
l.AddLast(TVectorGfxLine.Create(0,1,255,255,255,0))
l.AddLast(TVectorGfxLine.Create(1,2,255,255,255,0))
l.AddLast(TVectorGfxLine.Create(2,3,255,255,255,0))
l.AddLast(TVectorGfxLine.Create(3,0,255,255,255,0))
o2.SetLines(l.ToArray())
o2.Save("obj2.2d")
'EndRem

Local o3:TList=CreateList()

For Local f:Int=2 To 40
	Local o:TVectorGfxObject=o1.Clone()
	o.x=o1.x/f
	o3.AddLast(o)
Next

Type FadeLine Extends TVectorGfxLineStyle
	Method LineToCollisionMap:Int(x1:Int, y1:Int, x2:Int, y2:Int, r:Int, g:Int, b:Int, id:Int, obj:TVectorGfxObject, line:Int, colmap:TVectorGfxCollisionMap, actor:Int, list:TList)
		Local mask:Int=0
		Local lp:TList=DoLine(x1,y1,x2,y2)
		
		For Local p:TAlgoPoint=EachIn lp
			SetColor(r,g,b)

			'Rem			
			If colmap
				If actor
					Local c:TVectorGfxCollision=colmap.GetCollision(p.x,p.y)
					
					If c
						mask:|c.mask
						If list
							list.AddLast(c)
						EndIf
					EndIf
				Else
					mask:|colmap.SetCollision(p.x,p.y,id,obj,line)
				EndIf
			EndIf
			'EndRem
			'mask:|colmap.SetCollision(p.x,p.y,id)
			Plot(p.x,p.y)
			r=Max(0,r-10)
			g=Max(0,g-10)
			b=Max(0,b-10)
		Next
		
		Return mask
	End Method
	Method LineToPointList:Int(x1:Int, y1:Int, x2:Int, y2:Int, r:Int, g:Int, b:Int, list:TList)
		Local mask:Int=0
		Local lp:TList=DoLine(x1,y1,x2,y2)
		
		For Local p:TAlgoPoint=EachIn lp
			SetColor(r,g,b)
			Plot(p.x,p.y)
			list.AddLast(TVectorGfxPoint.Create(p.x,p.y))
			r=Max(0,r-10)
			g=Max(0,g-10)
			b=Max(0,b-10)
		Next
		
		Return mask
	End Method
End Type

Type Particle
	Field x:Double
	Field y:Double
	Field a:Double
	Field v:TVector
	
	Function Create:Particle(x:Int, y:Int, v:TVector)
		Local o:Particle=New Particle
		o.x=x
		o.y=y
		o.v=v
		o.a=1
		Return o
	End Function

	Method Update()
		x:+v.x
		y:+v.y
		a:-0.01
		
		If a>0
			SetAlpha(a)
			Plot(x,y)
		EndIf
	End Method
	
	Function Process(list:TList)
		Local l:TLink=list.FirstLink()
		Local t:TLink
		
		SetColor(255,255,255)
		
		While l<>Null
			Local p:Particle=Particle(l.Value())
			p.Update()
			
			If p.a<0.01
				t=l.NextLink()
				l.Remove()
				l=t
			Else
				l=l.NextLink()
			EndIf
		Wend
		
		SetAlpha(1)
	End Function
End Type

Local list:TList=CreateList()
Local plist:TList=CreateList()

While Not KeyHit(KEY_ESCAPE)
	Local t=MilliSecs()
	
	Cls
	
	Particle.Process(plist)
	
	cm.Clear()
	cm.SetOffset(MouseX()-MSIZE/2,MouseY()-MSIZE/2)
	
	If KeyHit(KEY_F1)
		VectorGfxSetSolid()
	EndIf

	If KeyHit(KEY_F2)
		VectorGfxSetCustom(New FadeLine)
	EndIf
	
	If KeyDown(KEY_UP)
		o1.scale:+0.1
	EndIf
	
	If KeyDown(KEY_DOWN)
		o1.scale:-0.1
	EndIf
	
	o1.DrawToCollisionMap(cm)
	o1.ang=(o1.ang+10) Mod 3600
	'o2.ang=(o2.ang+5) Mod 3600

	For Local o:TVectorGfxObject=EachIn o3
		o.DrawToCollisionMap(cm)
	Next	
	
	o2.x=MouseX()
	o2.y=MouseY()
	
	list.Clear()
	o2.DrawToCollisionMap(cm,True,list)
	
	SetColor(255,0,255)
	For Local c:TVectorGfxCollision=EachIn list
		Local v:TVector=c.obj.Normal(c.line)
		plist.AddLast(Particle.Create(c.x,c.y,v))
		'DrawLine(c.x,c.y,c.x+v.x*10,c.y+v.y*10)
	Next
	
	For Local i:Int=0 To 3
		Local p:TVectorGfxPoint[]=o1.AdjustedCoords(i)
		Local v:TVector=o1.Normal(i)
		SetColor(0,0,255)
		DrawLine(o1.x+p[0].x,o1.y+p[0].y,o1.x+p[0].x+v.x*20,o1.y+p[0].y+v.y*20)
	Next
	
	SetColor(255,255,255)
	DrawText (MilliSecs()-t) + ":Col=" + o2.DrawToCollisionMap(cm),0,0
	DrawText MemAlloced(),0,10
	
	If o1.IsInside(MouseX(),MouseY())
		DrawText "INSIDE!",0,20
	EndIf
	
	Flip
	FlushMem
Wend

EndGraphics
End