Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.51 KB | None | 0 0
  1. module Editor
  2.    
  3.     # This is the main Window class where all the magic happens
  4.     class Window < Gosu::Window
  5.         attr_accessor :states, :entities, :texts, :font, :images, :state, :previous_state
  6.         include Observable
  7.  
  8.         def initialize(width, height, fullscreen = false)
  9.             super(width, height, fullscreen)
  10.             $system = System.new
  11.             self.caption = "RRPG - Editor"
  12.             @entities = []
  13.             @images = { }
  14.             @images[:cursor] = Gosu::Image.new('editor/cursor.png')
  15.             @font = {} # this is necessary
  16.             # @font[:sm] = Gosu::Font.new(($font_height * 0.8).to_i, name: "Noteworthy")
  17.             # @font[:md] = Gosu::Font.new($font_height, name: "Noteworthy")
  18.             @font[:sm] = Gosu::Font.new(($font_height * 0.8).to_i)
  19.             @font[:md] = Gosu::Font.new($font_height)
  20.             @font[:lg] = Gosu::Font.new(($font_height * 1.5).to_i)
  21.             # @states = {}
  22.             # @states = create_states('./states/editor.yml')
  23.  
  24.             @states= {
  25.                 start_menu: State.new(
  26.                     self,
  27.                     entities: [
  28.                         Entity::UI::Menu.new(
  29.                             32,64,20,
  30.                             menu_entries: [
  31.                                 Entity::UI::MenuEntry.new('New Game', proc { System.state_update(:new_game) }),
  32.                                 Entity::UI::MenuEntry.new('Load Game', proc { System.state_update(:start_menu) }),
  33.                                 Entity::UI::MenuEntry.new('Map Editor',
  34.                                     proc {
  35.                                         state = create_state(
  36.                                             State.new(self, entities: [
  37.                                                 Entity::Map.new('./data/tilesets/default.png',
  38.                                                     [
  39.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  40.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  41.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  42.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  43.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  44.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  45.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  46.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  47.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  48.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  49.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  50.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  51.                                                         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  52.                                                     ]),
  53.                                                     Entity::UI::EditorTilePicker.new(16, 16,1000)
  54.                                                 ]
  55.                                             ),
  56.                                         )
  57.                                         @state = state
  58.                                     }),
  59.                                 Entity::UI::MenuEntry.new('Exit', proc { System.exit_game })
  60.                             ]
  61.                         )
  62.                     ]
  63.                 ),
  64.                 new_game: State.new(
  65.                     self,
  66.                     entities: [
  67.                         Entity::Map.new('./data/tilesets/default.png',
  68.                             [
  69.                                 [0,3,3,3,3,4,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  70.                                 [0,1,1,1,4,4,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  71.                                 [0,1,2,2,1,4,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  72.                                 [0,1,2,2,1,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  73.                                 [0,1,1,1,1,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  74.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  75.                                 [0,0,0,2,2,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  76.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  77.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  78.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  79.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  80.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  81.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  82.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  83.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  84.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  85.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  86.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  87.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  88.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  89.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  90.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  91.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  92.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  93.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  94.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  95.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  96.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  97.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  98.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  99.                                 [0,0,0,2,0,0,1,2,3,4,2,3,1,2,3,4,1,2,3,5,],
  100.                             ],
  101.                             actors: [
  102.                                 {
  103.                                     name: "Gustaf",
  104.                                     x: 32,
  105.                                     y: 32
  106.                                 },
  107.                                 {
  108.                                     name: "Peter",
  109.                                     x: 96,
  110.                                     y: 32
  111.                                 }
  112.                             ]
  113.                         )
  114.                     ] # end entities
  115.                 )
  116.             }
  117.  
  118.             @state = @states[:start_menu]
  119.             @previous_state = @state
  120.         end
  121.  
  122.         def create_state(state)
  123.             @states[state] = state
  124.         end
  125.  
  126.         # def create_states(file)
  127.         #     states_yaml = YAML.load_file(file)
  128.         #     states_yaml.each do |state, entities|
  129.  
  130.         #         self.states[state] = State.new(self, state, entities['entities'])
  131.  
  132.         #     end
  133.         # end
  134.  
  135.         def button_up(id)
  136.             @state.button_up(id)
  137.         end
  138.  
  139.         def button_down(id)
  140.             @state.button_down(id)
  141.  
  142.             # case id
  143.             #     when Gosu::KbQ
  144.             #         p "closing editor"
  145.             #         exit(0)
  146.             # end
  147.         end
  148.  
  149.         def state_update(args)
  150.             if args.include? :next_state
  151.                 puts "Load State"
  152.                 @previous_state = @state
  153.                 @state = args[:next_state]
  154.             elsif args.include? :go_back
  155.                 @state = @previous_state
  156.             else
  157.                 abort "Couldn't change the State. Aborting.".colorize(:red)
  158.             end
  159.         end
  160.  
  161.         def update
  162.             @state.update
  163.         end
  164.  
  165.         def draw
  166.             self.draw_quad(
  167.                 #1
  168.                 0,0,Gosu::Color.argb(0x333333ff),
  169.                 #2
  170.                 self.width,0,Gosu::Color.argb(0x333333ff),
  171.                 #3
  172.                 0,self.height,Gosu::Color.argb(0xff00ffff),
  173.                 #4
  174.                 self.width,self.height,Gosu::Color.argb(0xff00ffff),
  175.  
  176.                 z = 0,
  177.                 mode = :default
  178.             )
  179.  
  180.             @state.draw
  181.         end
  182.     end
  183. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement