Module: Core

Included in:
GUI::Inventory, GUI::ItemInfo, States::AlchemyMenu, States::EquipMenu, States::IngameMenu, States::MagicMenu, States::OptionMenu, States::StartMenu
Defined in:
lib/core.rb,
lib/load.rb,
lib/save.rb,
lib/song.rb,
lib/layer.rb,
lib/config.rb,
lib/cursor.rb,
lib/sample.rb,
lib/sprite.rb,
lib/tileset.rb,
lib/animation.rb,
lib/particles.rb,
lib/translate.rb,
lib/game_window.rb,
lib/tools/worldmap_editor.rb

Overview

TODO floats for position and speeds

Defined Under Namespace

Modules: GUI, Game, Parse, States, Tools, Trans Classes: Animation, CollisionLayer, Config, Cursor, Frame, GameWindow, Layer, Particle, ParticleEmitter, Particles, Sample, Song, Tileset

Constant Summary collapse

LIBRARY_PATH =

Source locations

File.dirname(__FILE__)
HOME_PATH =

For storing saves and updates in case the source folder is write protected

"#{ENV["HOME"]}/.essytas/"
SAVE_DIR =
"#{HOME_PATH}saves/"
VERSION =
0.1
RELEASE =
false
MAP_Z =

– Game

1
MAPOBJECT_Z =
50
CURSOR_Z =
1000000
GUI_Z =
9999
PARTICLE_Z =
5000
FOG_Z =
6000
DEFAULT_FONT =
"arial"
DEFAULT_CONFIG =
"default.cfg"
@@fonts =
{}
@@cache =
{}

Class Method Summary collapse

Class Method Details

.anim(anim) ⇒ Object



12
13
14
# File 'lib/animation.rb', line 12

def self.anim(anim)
  return animations[anim].clone
end

.animationsObject



4
5
6
# File 'lib/animation.rb', line 4

def self.animations
  return @animations
end

.animations=(hsh) ⇒ Object



8
9
10
# File 'lib/animation.rb', line 8

def self.animations=(hsh)
  @animations = hsh
end

.between?(a, b, c) ⇒ Boolean

a between b and c

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/core.rb', line 66

def self.between?(a, b, c)
  if a > b and a < c
    return true
  end
  return false
end

.configObject



113
114
115
# File 'lib/core.rb', line 113

def self.config
  return @config.hash
end

.exit(int) ⇒ Object

Graceful exit



137
138
139
140
141
# File 'lib/core.rb', line 137

def self.exit(int)
  @config.save
  puts("INFO: Shutting down")
  Kernel.exit(int)
end

.font(name, size) ⇒ Object

Returns a cached font instance



101
102
103
104
105
106
107
# File 'lib/core.rb', line 101

def self.font(name, size)
  if @@fonts["#{name}-#{size}".to_sym]
    return @@fonts["#{name}-#{size}".to_sym]
  else
    @@fonts["#{name}-#{size}".to_sym] = Gosu::Font.new(Core.window, name, size)
  end
end

.inside?(x, y, startx, starty, endx, endy) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'lib/core.rb', line 54

def self.inside?(x, y, startx, starty, endx, endy)
  if x >= startx && x < endx && y >= starty && y < endy
    return true
  end
  return false
end

.load_game(name = "autosave") ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/load.rb', line 4

def self.load_game(name="autosave")
  f = File.open("#{Core::SAVE_DIR}#{name}.esf")
  inflate = Zlib::Inflate.new
  str = Base64.decode64(inflate.inflate(f.read))
  inflate.close
  f.close
  map_obj = []
  map_misc = []
  lines = str.lines.to_a
  puts Core::States.constants.inspect
  state = Core::States.const_get("#{lines.shift.sub("\n", "")}".to_sym)
  lines.each do |line|
    line.sub!("\n", "")
    puts line.inspect
  end
  party = nil
  Core.window.advance(state.new(Core.window, party))
end

.mouse_inside?(x, y, ex, ey) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/core.rb', line 61

def self.mouse_inside?(x, y, ex, ey)
  return inside?(Core.window.mouse_x, Core.window.mouse_y, x, y, ex, ey)
end

.multiline(text, width, font) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/core.rb', line 73

def self.multiline(text, width, font)
  ret = []
  lines = text.split("\n")
  lines.each { |line|
    ary = line.split(" ")
    str = ""
    ary.each { |word|
      if font.text_width(str + word) < width - 18
        str += "#{word} "
      else
        str.rstrip!
        if str != ""
          ret.push(str)
        end
        str = "#{word} "
      end
      if word == ary.last
        str.rstrip!
        ret.push(str)
      end
    }
  }
  return ret
end

.particlesObject



5
6
7
# File 'lib/particles.rb', line 5

def self.particles
  return @particles
end

.particles=(hsh) ⇒ Object



8
9
10
# File 'lib/particles.rb', line 8

def self.particles=(hsh)
  @particles = hsh
end

.save_game(name = "autosave") ⇒ Object



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
# File 'lib/save.rb', line 7

def self.save_game(name="autosave")
  state = Core.window.state
  if state.class == States::GameState
    map = state.map
    party = state.party
  else
    map = Core.window.saved.map
    party = Core.window.saved.party
  end
  map_obj = map.objects
  map_misc = map.misc
  map_file = map.current.properties[:file]
  f = File.open("#{Core::SAVE_DIR}#{name}.esf", "wb")
  str = "#{state.class.to_s.split("::").last}\n"
  str += "#{map_file}\n"
  str += "#{party}"
  str += "START_MAP_OBJ\n"
  map_obj.each do |obj|
    next if obj.dead?
    str += "#{obj.to_save}\n"
  end
  str += "END_MAP_OBJ\n"
  # TODO is this actually used?
  str += "START_MAP_MISC\n"
  map_misc.each do |misc|
    str += "#{misc.class}\n"
    #str += "#{misc.to_save}\n"
  end
  str += "END_MAP_MISC\n"
  deflate = Zlib::Deflate.new(Zlib::BEST_COMPRESSION)
  # take that, script kiddies!
  comp = deflate.deflate(Base64.encode64(str), Zlib::FINISH)
  deflate.close
  f.puts(comp)
  f.close
end

.silently(&block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/core.rb', line 36

def self.silently(&block)
  lvl = $VERBOSE
  $VERBOSE = nil
  result = block.call
  $VERBOSE = lvl
  return result
end

.sprite(file, tile = false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sprite.rb', line 7

def self.sprite(file, tile=false)
  if @@cache[file]
    return @@cache[file]
  end
  begin
    raise if !File.exist?("#{Core::LIBRARY_PATH}/graphics/#{file}.png")
    img = Gosu::Image.new(window, "#{Core::LIBRARY_PATH}/graphics/#{file}.png", tile)
    @@cache.store(file, img)
    return img
  rescue RuntimeError
    warn("ERROR: Failed to open graphic #{file}")
    file = "missing"
    retry
  end
end

.tiles(file, tx, ty, tile = false) ⇒ Object

TODO cache



24
25
26
27
28
29
30
31
32
33
# File 'lib/sprite.rb', line 24

def self.tiles(file, tx, ty, tile=false)
  begin
    ary = Gosu::Image.load_tiles(window, "#{Core::LIBRARY_PATH}/graphics/#{file}.png", tx, ty, tile)
    return ary
  rescue RuntimeError
    warn("ERROR: Failed to open graphic #{file}")
    file = "missing"
    retry
  end
end

.windowObject

Returns the global Gosu::Window instance



46
47
48
# File 'lib/core.rb', line 46

def self.window
  return @window
end

.window=(w) ⇒ Object

Globally sets the Gosu::Window instance



50
51
52
# File 'lib/core.rb', line 50

def self.window=(w)
  @window = w
end