Class: Tank

Inherits:
GameObject show all
Defined in:
lib/entities/tank.rb

Constant Summary collapse

SHOOT_DELAY =
500

Instance Attribute Summary collapse

Attributes inherited from GameObject

#components, #location, #x, #y

Instance Method Summary collapse

Methods inherited from GameObject

#collide, #draw, #effect?, #mark_for_removal, #move, #removable?, #update

Constructor Details

#initialize(object_pool, input) ⇒ Tank

Returns a new instance of Tank.



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

def initialize(object_pool, input)
  x, y = object_pool.map.spawn_point
  super(object_pool, x, y)
  @input = input
  @input.control(self)
  @physics = TankPhysics.new(self, object_pool)
  @sounds = TankSounds.new(self, object_pool)
  @health = TankHealth.new(self, object_pool)
  @graphics = TankGraphics.new(self)
  @direction = rand(0..7) * 45
  @gun_angle = rand(0..360)
  reset_modifiers
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



3
4
5
# File 'lib/entities/tank.rb', line 3

def direction
  @direction
end

#fire_rate_modifierObject

Returns the value of attribute fire_rate_modifier.



3
4
5
# File 'lib/entities/tank.rb', line 3

def fire_rate_modifier
  @fire_rate_modifier
end

#graphicsObject

Returns the value of attribute graphics.



3
4
5
# File 'lib/entities/tank.rb', line 3

def graphics
  @graphics
end

#gun_angleObject

Returns the value of attribute gun_angle.



3
4
5
# File 'lib/entities/tank.rb', line 3

def gun_angle
  @gun_angle
end

#healthObject

Returns the value of attribute health.



3
4
5
# File 'lib/entities/tank.rb', line 3

def health
  @health
end

#inputObject

Returns the value of attribute input.



3
4
5
# File 'lib/entities/tank.rb', line 3

def input
  @input
end

#physicsObject

Returns the value of attribute physics.



3
4
5
# File 'lib/entities/tank.rb', line 3

def physics
  @physics
end

#soundsObject

Returns the value of attribute sounds.



3
4
5
# File 'lib/entities/tank.rb', line 3

def sounds
  @sounds
end

#speed_modifierObject

Returns the value of attribute speed_modifier.



3
4
5
# File 'lib/entities/tank.rb', line 3

def speed_modifier
  @speed_modifier
end

#throttle_downObject

Returns the value of attribute throttle_down.



3
4
5
# File 'lib/entities/tank.rb', line 3

def throttle_down
  @throttle_down
end

Instance Method Details

#boxObject



21
22
23
# File 'lib/entities/tank.rb', line 21

def box
  @physics.box
end

#can_shoot?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/entities/tank.rb', line 50

def can_shoot?
  Gosu.milliseconds - (@last_shot || 0) >
    (SHOOT_DELAY / @fire_rate_modifier)
end

#on_collision(object) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/entities/tank.rb', line 25

def on_collision(object)
  return unless object
  # Avoid recursion
  if object.class == Tank
    # Inform AI about hit
    object.input.on_collision(object)
  else
    # Call only on non-tanks to avoid recursion
    object.on_collision(self)
  end
  # Bullets should not slow Tanks down
  if object.class != Bullet
    @sounds.collide if @physics.speed > 1
  end
end

#reset_modifiersObject



55
56
57
58
# File 'lib/entities/tank.rb', line 55

def reset_modifiers
  @fire_rate_modifier = 1
  @speed_modifier = 1
end

#shoot(target_x, target_y) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/entities/tank.rb', line 41

def shoot(target_x, target_y)
  if can_shoot?
    @last_shot = Gosu.milliseconds
    Bullet.new(object_pool, @x, @y, target_x, target_y).fire(
      self, 1500)
    input.stats.add_shot
  end
end

#to_sObject



60
61
62
# File 'lib/entities/tank.rb', line 60

def to_s
  "Tank [#{@health.health}@#{@x}:#{@y}@#{@physics.speed.round(2)}px/tick]#{@input.stats}"
end