Class: Core::Game::MapObject

Inherits:
Object
  • Object
show all
Defined in:
lib/game/map/map_object.rb

Direct Known Subclasses

MapAnimation, MapNPC

Constant Summary collapse

DELIM =
"\x00"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, props) ⇒ MapObject

Returns a new instance of MapObject.



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
# 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

Instance Attribute Details

#dxObject (readonly)

Returns the value of attribute dx.



6
7
8
# File 'lib/game/map/map_object.rb', line 6

def dx
  @dx
end

#dyObject (readonly)

Returns the value of attribute dy.



6
7
8
# File 'lib/game/map/map_object.rb', line 6

def dy
  @dy
end

#followObject

Returns the value of attribute follow.



7
8
9
# File 'lib/game/map/map_object.rb', line 7

def follow
  @follow
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/game/map/map_object.rb', line 6

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



6
7
8
# File 'lib/game/map/map_object.rb', line 6

def properties
  @properties
end

#throughObject

Returns the value of attribute through.



7
8
9
# File 'lib/game/map/map_object.rb', line 7

def through
  @through
end

#txObject (readonly)

Returns the value of attribute tx.



6
7
8
# File 'lib/game/map/map_object.rb', line 6

def tx
  @tx
end

#tyObject (readonly)

Returns the value of attribute ty.



6
7
8
# File 'lib/game/map/map_object.rb', line 6

def ty
  @ty
end

#xObject (readonly)

Returns the value of attribute x.



6
7
8
# File 'lib/game/map/map_object.rb', line 6

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



6
7
8
# File 'lib/game/map/map_object.rb', line 6

def y
  @y
end

#zObject (readonly)

Returns the value of attribute z.



6
7
8
# File 'lib/game/map/map_object.rb', line 6

def z
  @z
end

Instance Method Details

#dead?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/game/map/map_object.rb', line 171

def dead?
  return @dead
end

#directionObject



180
181
182
183
184
185
186
187
188
189
190
# 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?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/game/map/map_object.rb', line 38

def do_setup?
  return @setup
end

#draw(xoff, yoff) ⇒ Object



167
168
169
# File 'lib/game/map/map_object.rb', line 167

def draw(xoff, yoff)
  @graphics[@index].draw(@x+xoff, @y-16+yoff, @z)
end

#setupObject



42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



198
199
200
201
202
203
# 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_saveObject



192
193
194
195
196
# 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) ⇒ Object



175
176
177
178
# File 'lib/game/map/map_object.rb', line 175

def trigger(other)
  @trigger.call(other) if @trigger
  @behaviour.on_trigger(other) if @behaviour
end

#updateObject



52
53
54
# File 'lib/game/map/map_object.rb', line 52

def update
  update_move(Core.window.state.map.current)
end

#update_move(map) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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_triggerObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# 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