Class: GamesParadise::Tank

Inherits:
Object
  • Object
show all
Defined in:
lib/games_paradise/gui/gosu/battle_city/tank.rb

Direct Known Subclasses

EnemyTank, Player

Instance Method Summary collapse

Constructor Details

#initializeTank

#

initialize

#


12
13
14
# File 'lib/games_paradise/gui/gosu/battle_city/tank.rb', line 12

def initialize
  reset
end

Instance Method Details

#move_eastObject

#

move_east

#


41
42
43
44
45
46
47
48
49
# File 'lib/games_paradise/gui/gosu/battle_city/tank.rb', line 41

def move_east     
  @head_west, @head_east, @head_north, @head_south = false, true, false, false
  @x += 1.3
  @x -= 5 if @x + 56 > 800     
  if @collide_obj_left
    @x -= 5
    @collide_obj_right, @collide_obj_left, @collide_obj_bottom, @collide_obj_top = [false] * 4
  end
end

#move_northObject

#

move_north

#


54
55
56
57
58
59
60
61
62
# File 'lib/games_paradise/gui/gosu/battle_city/tank.rb', line 54

def move_north     
  @head_west, @head_east, @head_north, @head_south = false, false, true, false
  @y -= 1.3
  @y += 5 if @y < 0 
  if @collide_obj_bottom
    @y += 5
    @collide_obj_right, @collide_obj_left, @collide_obj_bottom, @collide_obj_top = [false] * 4
  end
end

#move_southObject

move_south



65
66
67
68
69
70
71
72
73
# File 'lib/games_paradise/gui/gosu/battle_city/tank.rb', line 65

def move_south     
  @head_west, @head_east, @head_north, @head_south = false, false, false, true
  @y += 1.3
  @y -= 5 if @y + 56 > 710 
  if @collide_obj_top
    @y -= 5
    @collide_obj_right, @collide_obj_left, @collide_obj_bottom, @collide_obj_top = [false] * 4
  end
end

#move_westObject

#

move_west

#


28
29
30
31
32
33
34
35
36
# File 'lib/games_paradise/gui/gosu/battle_city/tank.rb', line 28

def move_west    
  @head_west, @head_east, @head_north, @head_south = true, false, false, false
  @x -= 1.3
  @x += 5 if @x < 0
  if @collide_obj_right
    @x += 5
    @collide_obj_right, @collide_obj_left, @collide_obj_bottom, @collide_obj_top = [false] * 4
  end
end

#nearest_obj(objects) ⇒ Object

nearest_obj



76
77
78
79
80
81
82
83
84
# File 'lib/games_paradise/gui/gosu/battle_city/tank.rb', line 76

def nearest_obj(objects)
  distances = []
  centre_loc = 20 if objects[0].class == Brick # centre location of brick
  centre_loc = 28 if objects[0].class.superclass == Tank
  objects.each { |obj| distances << ::Gosu.distance(obj.x + centre_loc, obj.y + centre_loc, @x + 28, @y + 28) }
  distances.delete(0) # not including self in team situation
  nearest_obj_index = distances.index(distances.min)
  objects[nearest_obj_index]  
end

#resetObject

#

reset

#


19
20
21
22
23
# File 'lib/games_paradise/gui/gosu/battle_city/tank.rb', line 19

def reset
  @collide_obj_left  = false
  @collide_obj_top   = false
  @collide_obj_right = false
end

#sense_collide(obj) ⇒ Object

sense_collide



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/games_paradise/gui/gosu/battle_city/tank.rb', line 87

def sense_collide(obj)
  unless obj.nil?
    ox = obj.x
    oy = obj.y
    ow = oh = 40 if obj.class == Brick # obj width and height
    ow = oh = 56 if obj.class.superclass == Tank
    if oy + oh > @y && oy < @y + 56 && ox < @x && ox + ow > @x
      @collide_obj_right = true 
      @x += 1 # remove from intersection instantly 
    end

    if oy + oh > @y && oy < @y + 56 && ox < @x + 56 && ox + ow > @x + 56
      @collide_obj_left = true
      @x -= 1
    end

    if ox + ow > @x && ox < @x + 56 && oy < @y && oy + oh > @y
      @collide_obj_bottom = true
      @y += 1
    end

    if ox + ow > @x && ox < @x + 56 && oy < @y + 56 && oy + oh > @y + 56
      @collide_obj_top = true
      @y -= 1
    end 
  end
end