Class: Brawl::Arena

Inherits:
Object
  • Object
show all
Defined in:
lib/brawl/arena.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Arena

Returns a new instance of Arena.



7
8
9
10
11
12
13
14
# File 'lib/brawl/arena.rb', line 7

def initialize(params={})
  @size     = params[:size]
  @objects  = []
  
  set_clock(params[:clock])

  add_walls
end

Instance Attribute Details

#clockObject (readonly)

Returns the value of attribute clock.



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

def clock
  @clock
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

Instance Method Details

#add_object(object) ⇒ Object



33
34
35
# File 'lib/brawl/arena.rb', line 33

def add_object(object)
  add_objects([object]).first
end

#add_objects(objects_array) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/brawl/arena.rb', line 37

def add_objects(objects_array)
  objects_array.collect do |object|
    if get_object(id: object.id) || ping(object.location)
      false
    else
      !!(@objects << object)
    end
  end
end

#end_gameObject



99
100
101
# File 'lib/brawl/arena.rb', line 99

def end_game
  @clock.stop
end

#forward_damage(params) ⇒ Object



84
85
86
87
88
# File 'lib/brawl/arena.rb', line 84

def forward_damage(params)
  target = @objects.select{|object| object.id == params[:target]}.first
  target.damage(params[:damage])
  end_game if victory?
end

#get_all_objectsObject



70
71
72
# File 'lib/brawl/arena.rb', line 70

def get_all_objects
  @objects.collect {|object| object.properties}
end

#get_object(property_hash) ⇒ Object

allows searching by object properties like location or id



62
63
64
65
66
67
68
# File 'lib/brawl/arena.rb', line 62

def get_object(property_hash)
  property, value = property_hash.first
  @objects.each do |object|
    return object.properties if object.properties[property] == value
  end
  nil
end

#in_bounds?(location) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/brawl/arena.rb', line 74

def in_bounds?(location)
  return false if location[:x] >= @size[:width]  || location[:x] <  0
  return false if location[:y] >= @size[:length] || location[:y] <  0
  true
end

#lengthObject



16
17
18
# File 'lib/brawl/arena.rb', line 16

def length
  @size[:length]
end

#move_object(object_location_hash) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/brawl/arena.rb', line 51

def move_object(object_location_hash)
  object    = object_location_hash.keys.first
  location  = object_location_hash.values.first

  return if ping(location)
  return unless in_bounds?(location)

  !!(object.location = location)
end

#ping(location) ⇒ Object



80
81
82
# File 'lib/brawl/arena.rb', line 80

def ping(location)
  get_object(location: location)
end

#remove_object(object) ⇒ Object



47
48
49
# File 'lib/brawl/arena.rb', line 47

def remove_object(object)
  @objects.delete(object)
end

#set_clock(clock = nil) ⇒ Object



28
29
30
31
# File 'lib/brawl/arena.rb', line 28

def set_clock(clock=nil)
  @clock = clock || Clock.new
  @clock.start
end

#ticksObject



24
25
26
# File 'lib/brawl/arena.rb', line 24

def ticks
  @clock.ticks
end

#victory?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
# File 'lib/brawl/arena.rb', line 90

def victory?
  @objects.one? do |object|
    object.class != Wall && object.health > 0
  end ||
  @objects.none? do |object|
    object.class != Wall && object.health > 0
  end
end

#widthObject



20
21
22
# File 'lib/brawl/arena.rb', line 20

def width
  @size[:width]
end