Class: GamesAndRpgParadise::EnemyTank

Inherits:
Tank
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(team) ⇒ EnemyTank

#

initialize

#


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 16

def initialize(team)
  super()
  @tank_west  = ::Gosu::Image.new('media/red_tank_west.png')
  @tank_east  = ::Gosu::Image.new('media/red_tank_east.png')
  @tank_north = ::Gosu::Image.new('media/red_tank_north.png')
  @tank_south = ::Gosu::Image.new('media/red_tank_south.png')
  @x = [20, 372, 724].sample
  @y = 10
  @head_west, @head_east, @head_north, @head_south = false, false, false, true
  @moving = false
  @first_move = true
  @cannon = Cannon.new(self)
  @game_start = Time.now
  @team = team 
  @teammates = @team.enemy_team
  @player = @team.player
  reset
end

Instance Attribute Details

#aliveObject

Returns the value of attribute alive.



11
12
13
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 11

def alive
  @alive
end

#cannonObject (readonly)

Returns the value of attribute cannon.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 9

def cannon
  @cannon
end

#head_eastObject (readonly)

Returns the value of attribute head_east.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 9

def head_east
  @head_east
end

#head_northObject (readonly)

Returns the value of attribute head_north.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 9

def head_north
  @head_north
end

#head_southObject (readonly)

Returns the value of attribute head_south.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 9

def head_south
  @head_south
end

#head_westObject (readonly)

Returns the value of attribute head_west.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 9

def head_west
  @head_west
end

#xObject (readonly)

Returns the value of attribute x.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 9

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 9

def y
  @y
end

Instance Method Details

#cannon_timer(range) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 74

def cannon_timer(range)
  unless @cannon_active 
    @get_time = Time.now
    @game_duration = (@get_time - @game_start).to_i
    @num = rand(range)
    @r = @game_duration % @num if @game_duration > 1 # fire once within num seconds, but not right at the start
  end
  if @r == 0 && @cannon_fired == false
    @cannon_active = true
    @cannon.fire
    @cannon_fired = true
  end
  if Time.now - @get_time > @num
    @cannon_active = false
    @cannon_fired = false
  end
end

#drawObject

draw



117
118
119
120
121
122
123
124
125
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 117

def draw       
  case true
  when @head_west; @tank_west.draw(@x, @y, 1)
  when @head_east; @tank_east.draw(@x, @y, 1)
  when @head_north; @tank_north.draw(@x, @y, 1)
  when @head_south; @tank_south.draw(@x, @y, 1)
  end 
  @cannon.draw
end

#moveObject

#

move

#


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 49

def move  
  unless @moving      
    @time_stamp = Time.now 
    if @first_move # move away from the generation site.     
      @duration = 1.5
      @option = 3
      @first_move = false
    else           
      @duration = rand * 1.5
      @option = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3].sample 
    end       
  end   
  if Time.now - @time_stamp <= @duration       
    case @option
    when 1; move_west
    when 2; move_east
    when 3; move_south
    when 4; move_north
    end
    @moving = true
  else
    @moving = false
  end
end

#resetObject

#

reset

#


38
39
40
41
42
43
44
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 38

def reset
  super()
  @r = nil
  @cannon_active = false
  @cannon_fired = false
  @alive = true
end

#sense_brickObject



99
100
101
102
103
104
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 99

def sense_brick
  unless @player.bricks.empty?
    nearest_brick = nearest_obj(@player.bricks)
  end
  sense_collide(nearest_brick)
end

#sense_teammateObject



92
93
94
95
96
97
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 92

def sense_teammate
  if @teammates.length >= 2 # need at least 2 teammates 
    nearest_tank = nearest_obj(@teammates)
    sense_collide(nearest_tank)
  end
end

#updateObject

update



107
108
109
110
111
112
113
114
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/enemy_tank.rb', line 107

def update
  move
  @cannon.update
  cannon_timer(3..5)
  sense_collide(@player)
  sense_teammate
  sense_brick
end