class Core::Game::MapObject

Constants

DELIM

Attributes

dx[R]
dy[R]
follow[RW]
name[R]
properties[R]
through[RW]
tx[R]
ty[R]
x[R]
y[R]
z[R]

Public Class Methods

new(x, y, props) click to toggle source
# File lib/game/map/map_object.rb, line 11
def initialize(x, y, props)
  file = props[:file]
  if file
    @graphics = Core.tiles("chars/#{file}", -4, -4)
  else
    @graphics = nil
    file = "unnamed"
  end
  @properties = props
  @follow = false
  @index = 0
  @x, @y = x, y
  @tx, @ty = x/32, y/32 # tile based position, used for collision
  @z = Core::MAPOBJECT_Z
  @z += props[:z] if props[:z]
  @speed = 0
  @speed += props[:speed] if props[:speed]
  @dx = @dy = 0 # distance to move, should be multiple of @speed
  @dead = false
  @steps = 0
  @trigger = nil
  @name = "mapobject-#{file.downcase}-#{rand(1000)}"
  @through = false
  @setup = true
  @stepped = false
end

Public Instance Methods

dead?() click to toggle source
# File lib/game/map/map_object.rb, line 171
def dead?
  return @dead
end
direction() click to toggle source
# File lib/game/map/map_object.rb, line 180
def direction
  if Core.between?(@index, -1, 4)
    return 0 # down
  elsif Core.between?(@index, 3, 8)
    return 1 # left
  elsif Core.between?(@index, 7, 12)
    return 2 # right
  elsif Core.between?(@index, 11, 16)
    return 3 # up
  end
end
do_setup?() click to toggle source
# File lib/game/map/map_object.rb, line 38
def do_setup?
  return @setup
end
draw(xoff, yoff) click to toggle source
# File lib/game/map/map_object.rb, line 167
def draw(xoff, yoff)
  @graphics[@index].draw(@x+xoff, @y-16+yoff, @z)
end
setup() click to toggle source
# File lib/game/map/map_object.rb, line 42
def setup
  if !@setup
    return false
  end
  if @setup
    @setup = false
  end
  return true
end
teleport(tx, ty) click to toggle source
# File lib/game/map/map_object.rb, line 198
def teleport(tx, ty)
  @tx = tx
  @ty = ty
  @x = @tx * 32
  @y = @ty * 32
end
to_save() click to toggle source
# File lib/game/map/map_object.rb, line 192
def to_save
  str = "#{@name}#{DELIM}#{@properties.to_s}#{DELIM}#{@tx}|#{@ty}#{DELIM}"
  str += "#{@x}|#{@y}#{DELIM}#{@speed}#{DELIM}#{@steps}#{DELIM}#{@through}#{DELIM}#{@stepped}#{DELIM}"
  return str
end
trigger(other) click to toggle source
# File lib/game/map/map_object.rb, line 175
def trigger(other)
  @trigger.call(other) if @trigger
  @behaviour.on_trigger(other) if @behaviour
end
update() click to toggle source
# File lib/game/map/map_object.rb, line 52
def update
  update_move(Core.window.state.map.current)
end
update_move(map) click to toggle source
# File lib/game/map/map_object.rb, line 56
def update_move(map)
  moved = false
  player = self.class == Player
  tx = @tx
  ty = @ty
  if @dx > 0
    if @stepped or @through or map.passable?((@tx*32)+@dx, @ty*32, player)
      @x += @speed
      @dx -= @speed
      @tx += 1 if !@stepped
      @stepped = true
      moved = true
      if @tx > Core.window.state.map.current.width
        Core.window.state.map.move_right
        @tx = 0
        @x = @speed
      end
    else
      @dx = 0
    end
    @index = 8
  elsif @dx < 0
    if @stepped or @through or map.passable?((@tx*32)+@dx, @ty*32, player)
      @x -= @speed
      @dx += @speed
      @tx -= 1 if !@stepped
      @stepped = true
      moved = true
      if @tx < 0
        Core.window.state.map.move_left
        @tx = Core.window.state.map.current.width
        @x = (Core.window.state.map.current.width*32) - @speed
      end
    else
      @dx = 0
    end
    @index = 4
  end
  if @dy > 0
    if @stepped or @through or map.passable?(@tx*32, (@ty*32)+@dy, player)
      @y += @speed
      @dy -= @speed
      @ty += 1 if !@stepped
      @stepped = true
      moved = true
    else
      @dy = 0
    end
    @index = 0
  elsif @dy < 0
    if @stepped or @through or map.passable?(@tx*32, (@ty*32)+@dy, player)
      @y -= @speed
      @dy += @speed
      @ty -= 1 if !@stepped
      @stepped = true
      moved = true
    else
      @dy = 0
    end
    @index = 12
  end
  if @dx == 0 and @dy == 0 and moved
    @steps += 1
    @stepped = false
  end
  if moved
    d = @dx
    if d == 0
      d = @dy
    end
    if d < 0
      d *= -1
    end
    if Core.between?(d, 8, 25)
      if @steps.even?
        @index += 1
      else
        @index += 3
      end
    end
  end
  if tx != @tx or ty != @ty
    Core.window.state.map.current.module.event(Events::MotionEvent.new(self, [tx, ty]))
  end
  return moved
end
update_trigger() click to toggle source
# File lib/game/map/map_object.rb, line 143
def update_trigger
  # look for something on current tile
  obj = Core.window.state.find_object(@tx*32, @ty*32)
  if !obj or obj == self
    # look for something in the direction we're looking
    x = y = 0
    case direction
    when 0
      y = 32
    when 1
      x = -32
    when 2
      x = 32
    else
      y = -32
    end
    obj = Core.window.state.find_object((@tx*32)+x, (@ty*32)+y)
  end
  if obj
    obj.trigger(self)
    Core.window.state.map.current.module.event(Events::TriggerEvent.new(self, obj))
  end
end