TheoAllen

Iseng2 RGSS3 (8) Poligon

Jun 3rd, 2013
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.21 KB | None | 0 0
  1. class Bitmap
  2.  
  3. def draw_line(x1,y1,x2,y2,color = Color.new(255,255,255),color_set_skip = false)
  4. jarak_x = (x2-x1)
  5. jarak_y = (y2-y1)
  6. if jarak_y == 0 || jarak_x == 0
  7. if jarak_y == 0
  8. draw_horz(x1,y1,jarak_x,color)
  9. for j in 0..jarak_x
  10. yield [x1,y1]
  11. x1 += 1
  12. end if block_given?
  13. elsif jarak_x.abs == 0
  14. draw_vert(x1,y1,jarak_y,color)
  15. for k in 0..jarak_y
  16. yield [x1,y1]
  17. y1 += 1
  18. end if block_given?
  19. end
  20. return
  21. end
  22. maximum = [jarak_x.abs,jarak_y.abs].max
  23. rasio_x = jarak_x / maximum.to_f
  24. rasio_y = jarak_y / maximum.to_f
  25. real_x = x1.to_f
  26. real_y = y1.to_f
  27. for i in 0..maximum
  28. if get_pixel(x1,y1).same?(color) || color_set_skip ? !get_pixel(x1,y1).empty? : false
  29. x1 = real_x.round
  30. y1 = real_y.round
  31. real_x += rasio_x
  32. real_y += rasio_y
  33. next
  34. end
  35. Graphics.update
  36. set_pixel(x1,y1,color)
  37. real_x += rasio_x
  38. real_y += rasio_y
  39. yield [x1,y1] if block_given?
  40. x1 = real_x.round
  41. y1 = real_y.round
  42. end
  43. end
  44.  
  45. def gradient_line(x1,y1,x2,y2,color1 = Color.new(255,255,255),color2 = color1)
  46. jarak_x = (x2-x1)
  47. jarak_y = (y2-y1)
  48. radius = Math.sqrt((jarak_x**2) + (jarak_y**2))
  49. red_diff = (color2.red - color1.red) / radius
  50. green_diff = (color2.green - color1.green) / radius
  51. blue_diff = (color2.blue - color1.blue) / radius
  52. alpha_diff = (color2.alpha - color1.alpha) / radius
  53. red = color1.red
  54. green = color1.green
  55. blue = color1.blue
  56. alpha = color1.alpha
  57. if jarak_y.abs == 0 || jarak_x.abs == 0
  58. gradient_fill_rect(x1,y1,1,jarak_y,color1,color2,true) if jarak_y.abs == 0
  59. gradient_fill_rect(x1,y1,jarak_x,1,color1,color2) if jarak_x.abs == 0
  60. yield [x1,y2] if block_given?
  61. return
  62. end
  63. maximum = [jarak_x.abs,jarak_y.abs].max
  64. rasio_x = jarak_x / maximum.to_f
  65. rasio_y = jarak_y / maximum.to_f
  66. real_x = x1.to_f
  67. real_y = y1.to_f
  68. for i in 0..maximum
  69. set_pixel(x1,y1,Color.new(red,green,blue,alpha))
  70. real_x += rasio_x
  71. real_y += rasio_y
  72. yield [x1,y1] if block_given?
  73. x1 = real_x.round
  74. y1 = real_y.round
  75. red += red_diff
  76. blue += blue_diff
  77. green += green_diff
  78. alpha += alpha_diff
  79. end
  80. end
  81.  
  82. def draw_horz(x,y,width,color = Color.new(255,255,255))
  83. if width < 0
  84. fill_rect(x+width+1,y,width.abs,1,color)
  85. return
  86. end
  87. fill_rect(x,y,width,1,color)
  88. end
  89.  
  90. def draw_vert(x,y,height,color = Color.new(255,255,255))
  91. if height < 0
  92. fill_rect(x,y+height+1,1,height.abs,color)
  93. return
  94. end
  95. fill_rect(x,y,1,height,color)
  96. end
  97.  
  98. def draw_ellipse(x,y=0,horz=1,vert=1,think=1,color=Color.new(255,255,255),step=0.1)
  99. return if think < 1
  100. if x.is_a?(Ellipse)
  101. elipse = x
  102. x = elipse.x
  103. y = elipse.y
  104. horz = elipse.horz
  105. vert = elipse.vert
  106. end
  107. ori_x = x
  108. ori_y = y
  109. x += horz
  110. degree = 0.0
  111. while degree <= 360
  112. set_pixel(x,y,color)
  113. x = Math.cos(Math.radian(degree)) * horz + ori_x
  114. y = Math.sin(Math.radian(degree)) * vert + ori_y
  115. degree = [degree+step,361].min
  116. end
  117. if think > 1
  118. draw_ellipse(ori_x+1,ori_y+1,horz,vert,think-1,color,step)
  119. draw_ellipse(ori_x-1,ori_y-1,horz,vert,think-1,color,step)
  120. draw_ellipse(ori_x+1,ori_y-1,horz,vert,think-1,color,step)
  121. draw_ellipse(ori_x-1,ori_y+1,horz,vert,think-1,color,step)
  122. end
  123. end
  124.  
  125. def fill_ellipse(x,y,horz,vert,color,step=1)
  126. degree = 0.0
  127. target_x = x+horz
  128. target_y = y
  129. while degree <= 360
  130. draw_line(x,y,target_x,target_y,color)
  131. target_x = Math.cos(Math.radian(degree)) * horz + x
  132. target_y = Math.sin(Math.radian(degree)) * vert + y
  133. degree = [degree+step,361].min
  134. end
  135. end
  136.  
  137. def gradient_fill_ellipse(x,y,horz,vert,color1,color2,step=0.1)
  138. degree = 0.0
  139. target_x = x+horz
  140. target_y = y
  141. while degree <= 360
  142. gradient_line(x,y,target_x,target_y,color1,color2)
  143. target_x = Math.cos(Math.radian(degree)) * horz + x
  144. target_y = Math.sin(Math.radian(degree)) * vert + y
  145. degree = [degree+step,361].min
  146. end
  147. end
  148.  
  149. def draw_shape_params(x,y,params,length,color1 = Color.new(255,255,255),fill=true,color2 = color1)
  150. return unless params.is_a?(Array)
  151. return unless params.size >= 3
  152. degree_minus = 360 / params.size
  153. degree = 270
  154. coordinate = []
  155. edge = []
  156. params.each do |i|
  157. x_des = x + Math.cos(Math.radian(degree)) * (length*(i.to_f/params.max))
  158. y_des = y + Math.sin(Math.radian(degree)) * (length*(i.to_f/params.max))
  159. draw_line(x,y,x_des,y_des,color2)
  160. degree += degree_minus
  161. coordinate.push(Coordinate.new(x_des,y_des))
  162. end
  163. for i in -1..coordinate.size-2
  164. c = coordinate
  165. draw_line(c[i].x,c[i].y,c[i+1].x,c[i+1].y) {|cor| edge.push(cor)}
  166. end
  167. return unless fill
  168. edge.each do |line|
  169. draw_line(x,y,line[0],line[1],color2,true)
  170. end
  171. for i in 0..1
  172. for baris in 0..length*2
  173. Graphics.update
  174. Input.update
  175. for kolom in 0..length*2
  176. pos_x = baris-length+x
  177. pos_y = kolom-length+y
  178. syarat = (get_pixel(pos_x+1,pos_y).same?(color1,color2) &&
  179. get_pixel(pos_x-1,pos_y).same?(color1,color2)) ||
  180. (get_pixel(pos_x,pos_y-1).same?(color1,color2) &&
  181. get_pixel(pos_x,pos_y+1).same?(color1,color2)) ||
  182. (get_pixel(pos_x+1,pos_y+1).same?(color1,color2) &&
  183. get_pixel(pos_x-1,pos_y-1).same?(color1,color2)) ||
  184. (get_pixel(pos_x-1,pos_y+1).same?(color1,color2) &&
  185. get_pixel(pos_x+1,pos_y-1).same?(color1,color2))
  186. if syarat
  187. set_pixel(pos_x,pos_y,color2)
  188. end
  189. end
  190. end
  191. end
  192. for i in -1..coordinate.size-2
  193. c = coordinate
  194. draw_line(c[i].x,c[i].y,c[i+1].x,c[i+1].y) #{|cor| edge.push(cor)}
  195. end
  196. end
  197.  
  198. def draw_test
  199. edge = []
  200. draw_line(10,10,30,300) {|cor| edge.push(cor)}
  201. edge.each do |line|
  202. draw_line(300,10,line[0],line[1])
  203. end
  204. end
  205.  
  206. end
  207.  
  208. class Coordinate
  209. attr_accessor :x,:y
  210.  
  211. def initialize(x,y)
  212. @x = x
  213. @y = y
  214. end
  215.  
  216. end
  217.  
  218. module Math
  219.  
  220. def self.radian(degree)
  221. return (degree.to_f/180) * Math::PI
  222. end
  223.  
  224. def self.degree(radian)
  225. return (radian.to_f/Math::PI) * 180
  226. end
  227.  
  228. end
  229.  
  230. #~ class Ellipse
  231. #~ attr_accessor :x, :y, :horz, :vert,
  232. #~ end
  233.  
  234. class Color
  235.  
  236. def same?(*args)
  237. return false unless args.all? {|c| c.is_a?(Color)}
  238. return args.any?{|color| self.red == color.red &&
  239. self.green == color.green && self.blue == color.blue &&
  240. self.alpha == color.alpha}
  241. end
  242.  
  243. def empty?
  244. return self.alpha <= 0
  245. end
  246.  
  247. end
  248.  
  249. class Radar < Sprite
  250. attr_accessor :line_color
  251.  
  252. def initialize(x,y,width,height,speed = 10,outline=false,viewport=nil)
  253. super(viewport)
  254. self.bitmap = Bitmap.new(width*2,height*2)
  255. self.x = x
  256. self.y = y
  257. self.ox = self.width/2
  258. self.oy = self.height/2
  259. @radar_w = width
  260. @radar_h = height
  261. @outline = outline
  262. @degree = 0.0
  263. @speed = speed
  264. @line_color = Color.new(255,255,255)
  265. end
  266.  
  267. def update
  268. self.bitmap.clear
  269. cx = self.width/2
  270. cy = self.height/2
  271. to_x = cx + Math.cos(Math.radian(@degree)) * @radar_w
  272. to_y = cy + Math.sin(Math.radian(@degree)) * @radar_h
  273. self.bitmap.draw_line(cx,cy,to_x,to_y,@line_color)
  274. @degree += @speed
  275. end
  276.  
  277. def dispose
  278. self.bitmap.dispose
  279. super
  280. end
  281.  
  282. end
  283.  
  284. def updates
  285. Graphics.update
  286. Input.update
  287. #~ $radar.update
  288. #~ if Input.repeat?(:LEFT)
  289. #~ $nilai -= 0.1
  290. #~ $sprite.bitmap.clear
  291. #~ $sprite.bitmap.fill_ellipse($tengah_x,$tengah_y,200,100,Color.new(255,255,255),$nilai)
  292. #~ elsif Input.repeat?(:RIGHT)
  293. #~ $nilai += 0.1
  294. #~ $sprite.bitmap.clear
  295. #~ $sprite.bitmap.fill_ellipse($tengah_x,$tengah_y,200,100,Color.new(255,255,255),$nilai)
  296. #~ end
  297.  
  298. end
  299. #~ $back = Sprite.new
  300. #~ $back.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  301. #~ $back.bitmap.fill_rect(0,0,Graphics.width,Graphics.height,Color.new(255,255,255))
  302. color2 = Color.new(0,0,0,255)
  303. color1 = Color.new(0,0,0,0)
  304. color3 = Color.new(255,255,255)
  305. $sprite = Sprite.new
  306. $tengah_x = Graphics.width/2
  307. $tengah_y = Graphics.height/2
  308. #~ $radar = Radar.new($tengah_x,$tengah_y,200,200)
  309. $nilai = 1
  310. $sprite.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  311. #~ $sprite.z += 9999
  312. #~ $sprite.bitmap.gradient_fill_ellipse($tengah_x,$tengah_y,Graphics.width/1.4,Graphics.height/1.4,color1,color2)
  313. #~ $sprite.bitmap.gradient_line(0,0,200,200,Color.new(255,255,255,0),Color.new(255,255,255,255))
  314. #~ $sprite.bitmap.draw_ellipse($tengah_x,$tengah_y,200,100,2,Color.new(255,255,255),0.1)
  315. #~ $sprite.bitmap.fill_ellipse($tengah_x,$tengah_y,200,100,Color.new(255,255,255),$nilai)
  316. params = []
  317. params += [100,50]*2
  318. $sprite.bitmap.draw_shape_params(100,150,params,50,color3,true,Color.new(0,255,0))
  319. params = []
  320. params += [100,50]*4
  321. $sprite.bitmap.draw_shape_params(200,150,params,50,color3,true,Color.new(0,255,0))
  322. params = []
  323. params += [100,50]*6
  324. $sprite.bitmap.draw_shape_params(300,150,params,50,color3,true,Color.new(0,255,0))
  325. params = []
  326. params += [100]*3
  327. $sprite.bitmap.draw_shape_params(100,300,params,50,color3,true,Color.new(0,255,0))
  328. params = []
  329. params += [100]*5
  330. $sprite.bitmap.draw_shape_params(200,300,params,50,color3,true,Color.new(0,255,0))
  331. params = []
  332. params += [100]*7
  333. $sprite.bitmap.draw_shape_params(300,300,params,50,color3,true,Color.new(0,255,0))
  334. params = []
  335. params += [100,45,69,78,59,70,34,87]
  336. $sprite.bitmap.draw_shape_params(50,50,params,50,color3,true,Color.new(0,255,0))
  337. #$sprite.bitmap.draw_line(100,100,20,20)
  338. #~ $sprite.bitmap.draw_test
  339. #~ $sprite.bitmap.fill_rect(50,200,300,300,Color.new(255,255,255,255))
  340. #~ $sprite.bitmap.fill_rect(200,50,300,300,Color.new(255,255,255,255))
  341. #~ $sprite.bitmap.draw_line(0,0,Graphics.width,Graphics.height)
  342. updates while true
  343.  
  344. class Scene_Base
  345. alias asd start
  346. def start
  347. asd
  348. $sprite.viewport = @viewport
  349. end
  350.  
  351. end
Advertisement
Add Comment
Please, Sign In to add comment