Class: Tank

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

Overview

class Tank

Direct Known Subclasses

Enemy, Player

Instance Attribute Summary collapse

Attributes inherited from GameObject

#collider, #x, #y

Instance Method Summary collapse

Methods inherited from GameObject

#collide?, #collision_detect, #done?, #draw, #hitted, #solid?

Constructor Details

#initialize(x, y, direction) ⇒ Tank

Returns a new instance of Tank.



8
9
10
11
12
13
14
15
# File 'lib/entities/tank.rb', line 8

def initialize(x, y, direction)
  super(x, y, 2, 2, true)
  @direction = direction
  @missile = nil
  @shoot_timer = 0
  @level = 0
  @speed = 1
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



6
7
8
# File 'lib/entities/tank.rb', line 6

def direction
  @direction
end

Instance Method Details

#can_shoot?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/entities/tank.rb', line 43

def can_shoot?
  @missile.nil? && @shoot_timer == 0
end

#canoonXObject



47
48
49
50
51
# File 'lib/entities/tank.rb', line 47

def canoonX
  return @x + 14 if @direction == :right
  return @x - 12 if @direction == :left
  return @x + 3 if @direction == :up || @direction == :down
end

#canoonYObject



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

def canoonY
  return @y + 14 if @direction == :down
  return @y - 12 if @direction == :up
  @y
end

#check_missileObject



59
60
61
62
63
64
65
# File 'lib/entities/tank.rb', line 59

def check_missile
  return if @missile.nil?
  if @missile.done?
    @missile = nil
    @shoot_timer = 20 - @level * 3
  end
end

#moveObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/entities/tank.rb', line 17

def move
  @x -= 0.5 * @speed if @direction == :left
  @x += 0.5 * @speed if @direction == :right
  @y += 0.5 * @speed if @direction == :down
  @y -= 0.5 * @speed if @direction == :up
  @x = 0 if @x < 0
  @y = 0 if @y < 0
  @x = 240 if @x > 240
  @y = 208 if @y > 208
  @collider.update(@x, @y)
end

#return_backObject



29
30
31
32
33
34
35
# File 'lib/entities/tank.rb', line 29

def return_back
  @x += 0.5 * @speed if @direction == :left
  @x -= 0.5 * @speed if @direction == :right
  @y -= 0.5 * @speed if @direction == :down
  @y += 0.5 * @speed if @direction == :up
  @collider.update(@x, @y)
end

#shootObject



37
38
39
40
41
# File 'lib/entities/tank.rb', line 37

def shoot
  @missile = Bullet.new(self, true) if can_shoot? && @level == 4
  @missile = Bullet.new(self) if can_shoot?
  @missile
end

#updateObject



67
68
69
# File 'lib/entities/tank.rb', line 67

def update
  @shoot_timer -= 1 if @shoot_timer > 0
end