Common.monkey2 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #Import "PoolMod"
  2. Const RTA:Float = 180.0/Pi
  3. Const ATR:Float = Pi/180.0
  4. Const INVALID_DISTANCE:Float = 100000000
  5. Const DEVICE_WIDTH:Int = 640
  6. Const DEVICE_HEIGHT:Int = 480
  7. '***********************************************************************************
  8. '
  9. ' renders a circle at a distance relative to a ball current position
  10. '
  11. '***********************************************************************************
  12. Function RenderAtDistance:Void(canvas:Canvas,ball:Ball,distance:Float)
  13. If Not ball.image
  14. RenderCircle(canvas,ball.P.x+ball.V.x*distance,ball.P.y+ball.V.y*distance,ball.radius)
  15. Else
  16. canvas.DrawImage(ball.image,ball.P.x+ball.V.x*distance,ball.P.y+ball.V.y*distance)
  17. Endif
  18. End Function
  19. ''***********************************************************************************
  20. '
  21. ' renders an arc at position x,y and radius,
  22. ' starting angle, and ending angle.
  23. '
  24. '************************************************************************************
  25. Function RenderArc:Void(canvas:Canvas,x:Float, y:Float, radius:Float, startAngle:Float, endAngle:Float)
  26. 'If angles are equal nothing to draw
  27. If startAngle = endAngle Then Return
  28. 'make the arc clockwise
  29. If startAngle > endAngle
  30. ' swap angles if order is counter-clockwise
  31. Local ta:Float = endAngle
  32. endAngle = startAngle
  33. startAngle = ta
  34. Endif
  35. 'number of degrees to render
  36. Local angle:Float = endAngle - startAngle
  37. ' no more than 360 degrees to render
  38. If angle > 360.0 angle = 360.0
  39. ' set the step to advance one unit at a tme
  40. Local stp:Float = 1.0/(RTA * radius)
  41. 'set the accumulator to the starting angle
  42. Local AccumAngle:Float = startAngle
  43. ' repeat until the arc is completely drawn
  44. While AccumAngle < (startAngle+angle)
  45. 'draw a pixel around the arc
  46. canvas.DrawPoint(x + Cos(AccumAngle*ATR) * radius, y + Sin(AccumAngle*ATR) * radius)
  47. 'advance to next angle
  48. AccumAngle += stp
  49. Wend
  50. End Function
  51. '***********************************************************************************
  52. '
  53. ' draws a circle at x,y and radius
  54. '
  55. '***********************************************************************************
  56. Function RenderCircle:Void(canvas:Canvas,x:Float,y:Float,radius:Float)
  57. 'start angle
  58. Local angle:Float = 0.0
  59. ' distance between each point
  60. Local stp:Float = 1.0/(RTA * radius)
  61. 'rotate upto 360 degrees
  62. While angle < 360.0
  63. canvas.DrawPoint(x + Cos(angle*ATR) * radius, y + Sin(angle*ATR) * radius)
  64. 'increment the position to draw the next point
  65. angle += stp
  66. Wend
  67. End Function
  68. '***********************************************************************************
  69. '
  70. ' Draws a line with of specific width
  71. '
  72. '
  73. '***********************************************************************************
  74. Function DrawLine_W:Void(canvas:Canvas,XPos:Float,YPos:Float,XPos2:Float,YPos2:Float,Thickness:Float=3,roundedEnds:Int=False)
  75. Local Coords:Float[] = New Float[8]
  76. Local vx:Float = XPos2 - XPos
  77. Local vy:Float = YPos2 - YPos
  78. Local Angle:Float = ATan2(vy,vx)
  79. Local c:Float = Cos(Angle*ATR)
  80. Local s:Float = Sin(Angle*ATR)
  81. Local LineLength:Float=vx*c+vy*s
  82. Local cl:Float = LineLength*c
  83. Local sl:Float = LineLength*s
  84. Local r:Float = Thickness/2.0
  85. Local sr:Float = s*r
  86. Local cr:Float = c*r
  87. 'Left top coords
  88. Coords[0]=XPos+sr
  89. Coords[1]=YPos-cr
  90. 'Right top coords
  91. Coords[2]=cl+XPos+sr
  92. Coords[3]=sl+YPos-cr
  93. 'Right bottom coords
  94. Coords[4]=cl-sr+XPos
  95. Coords[5]=sl+cr+YPos
  96. 'Left bottom coords
  97. Coords[6]=XPos-sr
  98. Coords[7]=YPos+cr
  99. canvas.DrawPoly(Coords)
  100. If roundedEnds = True
  101. canvas.DrawCircle(XPos,YPos,r)
  102. canvas.DrawCircle(XPos2,YPos2,r)
  103. Endif
  104. End Function
  105. Function LoadFrames:Image[](img:Image,x:Int,y:Int,width:Int,height:Int,count:Int)
  106. Local frames:Image[] = New Image[count]
  107. Local index:Int = 0
  108. Local posx:Int = x
  109. Local posy:Int = y
  110. While index < count
  111. If posx > (img.Width-width)
  112. Print posx
  113. posx = 0
  114. posy += height
  115. Endif
  116. frames[index] = New Image(img,posx,posy,width,height)
  117. index += 1
  118. posx += width
  119. Wend
  120. Return frames
  121. End Function
  122. Class sysFont
  123. Global font:Image[]
  124. Global width:Int
  125. Global height:Int
  126. Global firstChar:Int
  127. Function Set(image:Image[],first:Int)
  128. font = image
  129. width = image[0].Width
  130. height = image[0].Height
  131. End Function
  132. Function Draw(canvas:Canvas,text:String,x:Float,y:Float)
  133. For Local i:Int=0 Until text.Length
  134. Local ch:Int=text[i]-firstChar
  135. If ch>=0 And ch<font.Length
  136. canvas.DrawImage(font[ch],x+i*width,y)
  137. Endif
  138. Next
  139. End Function
  140. End Class
  141. Function SetFont( font:Image[],firstChar:Int=32 )
  142. sysFont.Set(font,firstChar)
  143. End
  144. Function GetFont:Image[]()
  145. Return sysFont.font
  146. End
  147. Function TextWidth:Float( text:string )
  148. If sysFont.font
  149. Return text.Length * sysFont.width
  150. Endif
  151. RuntimeError("No Font Available")
  152. Return 0
  153. End
  154. Function TextHeight:Float()
  155. If sysFont.font
  156. Return sysFont.height
  157. Endif
  158. RuntimeError("No Font Available")
  159. Return 0
  160. End
  161. Function FontHeight:Float()
  162. If sysFont.font
  163. Return sysFont.height
  164. Endif
  165. RuntimeError("No Font Available")
  166. Return 0
  167. End
  168. Function DrawText(canvas:Canvas,text:String,x:Float,y:Float )
  169. sysFont.Draw(canvas,text,x,y)
  170. End
  171. Class PVector2D
  172. Field x:Float
  173. Field y:Float
  174. Method New()
  175. End Method
  176. Method New(x:Float,y:Float)
  177. Set(x,y)
  178. End Method
  179. Method New(x1:Float,y1:Float,x2:Float,y2:Float)
  180. Set(x2-x1,y2-y1)
  181. End Method
  182. Method New(v:PVector2D)
  183. Set(v)
  184. End Method
  185. Method Set:PVector2D(v:PVector2D)
  186. x = v.x
  187. y = v.y
  188. Return Self
  189. End Method
  190. Method Set:PVector2D(x:Float,y:Float)
  191. Self.x = x
  192. Self.y = y
  193. Return Self
  194. End Method
  195. Method Add:PVector2D(x:Float,y:Float)
  196. Self.x += x
  197. Self.y += y
  198. Return Self
  199. End Method
  200. Method Add:PVector2D(v:PVector2D)
  201. Self.x += v.x
  202. Self.y += v.y
  203. Return Self
  204. End Method
  205. Method Add:PVector2D(value:Float)
  206. Self.x += value
  207. Self.y += value
  208. Return Self
  209. End Method
  210. Method Subtract:PVector2D(x:Float,y:Float)
  211. Self.x -= x
  212. Self.y -= y
  213. Return Self
  214. End Method
  215. Method Subtract:PVector2D(v:PVector2D)
  216. Self.x -= v.x
  217. Self.y -= v.y
  218. Return Self
  219. End Method
  220. Method Subtract:PVector2D(value:Float)
  221. Self.x -= value
  222. Self.y -= value
  223. Return Self
  224. End Method
  225. Method Multiply:PVector2D(x:Float,y:Float)
  226. Self.x *= x
  227. Self.y *= y
  228. Return Self
  229. End Method
  230. Method Multiply:PVector2D(v:PVector2D)
  231. Self.x *= v.x
  232. Self.y *= v.y
  233. Return Self
  234. End Method
  235. Method Multiply:PVector2D(value:Float)
  236. Self.x *= value
  237. Self.y *= value
  238. Return Self
  239. End Method
  240. Method Divide:PVector2D(x:Float,y:Float)
  241. Self.x /= x
  242. Self.y /= y
  243. Return Self
  244. End Method
  245. Method Divide:PVector2D(v:PVector2D)
  246. Self.x /= v.x
  247. Self.y /= v.y
  248. Return Self
  249. End Method
  250. Method Divide:PVector2D(value:Float)
  251. Self.x /= value
  252. Self.y /= value
  253. Return Self
  254. End Method
  255. Method DotProduct:Float(x:Float,y:Float)
  256. Return Self.x * x + Self.y * y
  257. End Method
  258. Method DotProduct:Float(v:PVector2D)
  259. Return Self.x * v.x + Self.y * v.y
  260. End Method
  261. Method PerpDotProduct:Float(x:Float,y:Float)
  262. Return Self.x * y - Self.y * x
  263. End Method
  264. Method PerpDotProduct:Float(v:PVector2D)
  265. Return Self.x * v.y - Self.y * v.x
  266. End Method
  267. Method MagnitudeSquare:Float()
  268. Return Self.x * Self.x + Self.y * Self.y
  269. End Method
  270. Method Magnitude:Float()
  271. Return Sqrt(MagnitudeSquare())
  272. End Method
  273. Method LeftNormal()
  274. Local n:Float = y
  275. y = -x
  276. x = n
  277. End Method
  278. Method RightNormal()
  279. Local n:Float = y
  280. y = x
  281. x = -n
  282. End Method
  283. Method Normalize:PVector2D()
  284. Divide(Magnitude())
  285. Return Self
  286. End Method
  287. Method GetAngle:Float()
  288. Return ATan2(y,x)
  289. End Method
  290. Method ToString:String()
  291. Return x+" "+y
  292. End Method
  293. Method Draw:Void(canvas:Canvas,x:Float,y:Float)
  294. canvas.DrawLine(x,y,x+x,y+y)
  295. End Method
  296. End Class
  297. Function Projection:PVector2D(this:PVector2D,into:PVector2D,destination:PVector2D=Null)
  298. If destination = Null destination = New PVector2D
  299. Return destination.Set(into).Normalize().Multiply(destination.DotProduct(this))
  300. End Function
  301. Function CrossProjection:PVector2D(this:PVector2D,into:PVector2D,destination:PVector2D=Null)
  302. If destination = Null destination = New PVector2D
  303. Return destination.Set(into.y,-into.x).Normalize().Multiply(destination.DotProduct(this))
  304. End Function