Guest User

Untitled

a guest
Feb 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. class Edit
  2. def initialize name
  3. @name = name
  4. @screen = Rubygame::Screen.set_mode([800,600],0, [Rubygame::HWSURFACE, Rubygame::DOUBLEBUF])
  5. @image = []
  6. @map
  7. @mouse = Mouse_input.new
  8. #@sprites = Rubygame::Sprites::Group.new()
  9. #Rubygame::Sprites::UpdateGroup.extend_object(@sprites)
  10. #@sprites.push(@mouse)
  11. end
  12. def new_file file
  13. save(file , @name)
  14. @map = read(@name)
  15. image
  16. edit_mode
  17. end
  18. def load
  19. @map = read(@name)
  20. image
  21. edit_mode
  22. end
  23. def read filename
  24. yaml_string = File.read filename; YAML:: load yaml_string
  25. end
  26. def save object , filename
  27. File.open filename , 'w' do |f|
  28. f.write(object.to_yaml)
  29. end
  30. end
  31. def image
  32. #load image files according to the strings it read in @map['file']
  33. @map['file'].each do |file|
  34. @image << Rubygame::Image.load("data/maps/#{file}")
  35. end
  36. map
  37. end
  38. def map
  39. x = y = time = 0
  40. @map['points'].each do |render|
  41. if render != nil
  42. @image[render].blit(@screen,[x,y])
  43. else
  44. #Code doesn't work for some reason.
  45. #Rubygame::Draw.filled_box( @screen, [50,0], [50,50], [0,0,0] )
  46. end
  47. x += 50
  48. time += 1
  49. if time == 16
  50. y += 50
  51. x = 0
  52. time = 0
  53. end
  54. end
  55. end
  56. def change event
  57. #Compare x and y until the value are greater and then map the last points.
  58. #Purpose is to determine the relative locations of the mouse in which squares
  59. max = 0
  60. #Start at negative 1 because @map['points'] contains array element and array start with 0
  61. square = -1
  62. while (1)
  63. #Deal with x position; square increase by 1
  64. if event.pos[0] >= max
  65. max +=50
  66. square += 1
  67. else
  68. max = 0
  69. break
  70. end
  71. end
  72. #So the square will appear where the mouse is..relatively
  73. square -= 16
  74. while (1)
  75. #Deal with y position; square increase by 16
  76. if event.pos[1] >= max
  77. max +=50
  78. square += 16
  79. else
  80. break
  81. end
  82. end
  83. if @map['points'][square] == nil
  84. @map['points'][square] = 0
  85. else
  86. @map['points'][square] += 1
  87. if @map['points'][square] == 7
  88. @map['points'][square] = 0
  89. end
  90. end
  91. map
  92. end
  93. def edit_mode
  94. q = Rubygame::Queue.instance()
  95. while 1
  96. @screen.flip
  97. q.get.each do |event|
  98. case event
  99. when Rubygame::MouseMotionEvent
  100. @mouse.pos(event)
  101. when Rubygame::MouseDownEvent
  102. case event.button
  103. when Rubygame::MOUSE_LEFT
  104. change(event)
  105. end
  106. when Rubygame::KeyDownEvent
  107. case event.key
  108. when Rubygame::K_ESCAPE
  109. save(@map , @name)
  110. exit
  111. end
  112. when Rubygame::QuitEvent
  113. exit
  114. end
  115. end
  116. #@sprites.undraw(@screen,@image)
  117. @mouse.update
  118. #@sprites.draw(@screen)
  119. end
  120. end
  121. end
Add Comment
Please, Sign In to add comment