Class: RubyGame::Monster

Inherits:
MovingObject show all
Defined in:
lib/ruby_game/monster.rb

Defined Under Namespace

Classes: Action

Constant Summary collapse

@@monsters =
{}

Instance Attribute Summary

Attributes inherited from StaticObject

#x, #y

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MovingObject

#move_down, #move_left, #move_right, #move_up, #touch?

Methods inherited from StaticObject

#draw, #init_image

Constructor Details

#initialize(x = 0, y = 0, image_name = 'ghost1') ⇒ Monster

Returns a new instance of Monster.



7
8
9
10
# File 'lib/ruby_game/monster.rb', line 7

def initialize(x = 0, y = 0, image_name = 'ghost1')
  super
  @actions = []
end

Class Method Details

.build(name, number = 1) ⇒ Object



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

def self.build(name, number = 1)
  Array.new(number) {@@monsters[name].clone}
end

.define(name, &block) ⇒ Object



27
28
29
30
31
# File 'lib/ruby_game/monster.rb', line 27

def self.define(name, &block)
  monster = Monster.new
  monster.instance_eval(&block)
  @@monsters[name] = monster
end

Instance Method Details

#action(direction, options = {}) ⇒ Object



41
42
43
44
# File 'lib/ruby_game/monster.rb', line 41

def action(direction, options = {})
  opts = {velocity: @velocity, repeat: 1}.merge options
  @actions += Array.new(opts[:repeat]) {Action.new(direction, opts[:velocity])}
end

#execute(player) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/ruby_game/monster.rb', line 46

def execute(player)
  @actions_enum ||= @actions.cycle
  action = @actions_enum.next
  if action.direction == :forward
    self.send(action.direction, player, action.velocity)
  else
    self.send(action.direction, action.velocity)
  end
end

#forward(player, velocity = @velocity) ⇒ Object



22
23
24
25
# File 'lib/ruby_game/monster.rb', line 22

def forward(player, velocity = @velocity)
  @x += (player.x <=> @x) * velocity
  @y += (player.y <=> @y) * velocity
end

#image_name(image_name) ⇒ Object



33
34
35
# File 'lib/ruby_game/monster.rb', line 33

def image_name(image_name)
  @image_name = image_name
end

#init_limits(max_width, max_height, border_with, border_top_with) ⇒ Object



16
17
18
19
20
# File 'lib/ruby_game/monster.rb', line 16

def init_limits(max_width, max_height, border_with, border_top_with)
   super
   @x = rand border_top_with..(max_width - 2*border_top_with)
   @y = rand border_top_with..(max_height - 2*border_top_with)
end

#velocity(velocity) ⇒ Object



37
38
39
# File 'lib/ruby_game/monster.rb', line 37

def velocity(velocity)
  @velocity = velocity
end