Class: Projectile

Inherits:
Object
  • Object
show all
Defined in:
lib/share/projectile.rb

Overview

Projectile class handles arrow logic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProjectile

Returns a new instance of Projectile.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/share/projectile.rb', line 9

def initialize
  @x = 0
  @y = 0
  @dx = 0
  @dy = 0
  @w = 16
  @h = 16
  @owner = nil
  @left_owner = false
  @flying = false
  @tick = 0
end

Instance Attribute Details

#dxObject

Returns the value of attribute dx.



7
8
9
# File 'lib/share/projectile.rb', line 7

def dx
  @dx
end

#dyObject

Returns the value of attribute dy.



7
8
9
# File 'lib/share/projectile.rb', line 7

def dy
  @dy
end

#hObject

Returns the value of attribute h.



7
8
9
# File 'lib/share/projectile.rb', line 7

def h
  @h
end

#owner_idObject

Returns the value of attribute owner_id.



7
8
9
# File 'lib/share/projectile.rb', line 7

def owner_id
  @owner_id
end

#rObject

Returns the value of attribute r.



7
8
9
# File 'lib/share/projectile.rb', line 7

def r
  @r
end

#wObject

Returns the value of attribute w.



7
8
9
# File 'lib/share/projectile.rb', line 7

def w
  @w
end

#xObject

Returns the value of attribute x.



7
8
9
# File 'lib/share/projectile.rb', line 7

def x
  @x
end

#yObject

Returns the value of attribute y.



7
8
9
# File 'lib/share/projectile.rb', line 7

def y
  @y
end

Instance Method Details

#calc_rotationObject

^ 3 / 7 /

v


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/share/projectile.rb', line 91

def calc_rotation
  @r = if @dy > -3 && @dy < 3
         if @dx.negative?
           4
         else
           0
         end
       elsif @dy.negative?
         if @dx > -3 && @dx < 3
           6
         elsif @dx.negative?
           5
         else
           7
         end
       elsif @dx > -3 && @dx < 3
         2
       elsif @dx.negative?
         3
       else
         1
       end
end

#check_hit(players) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/share/projectile.rb', line 56

def check_hit(players)
  owner_hit = false
  players.each do |player|
    next unless player.x + player.w > @x && player.x < @x + @w

    if player.y + player.h > @y && player.y < @y + @h
      if @owner.id == player.id
        owner_hit = true
        if @left_owner
          player.damage(@owner)
          hit
        end
      else
        player.damage(@owner)
        hit
      end
    end
  end
  @left_owner = true if owner_hit == false
end

#fire(x, y, dx, dy, owner) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/share/projectile.rb', line 22

def fire(x, y, dx, dy, owner)
  return if @flying

  @x = x
  @y = y
  @dx = dx
  @dy = dy
  calc_rotation
  @owner = owner
  @left_owner = false
  @flying = true
  $console.dbg "Projectile(x=#{x}, y=#{y}, dx=#{dx}, dy=#{dy})"
end

#hitObject



36
37
38
39
40
# File 'lib/share/projectile.rb', line 36

def hit
  @flying = false
  @x = 0
  @y = 0
end

#tick(players) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/share/projectile.rb', line 42

def tick(players)
  return unless @flying

  @tick += 1
  @x += @dx
  @y += @dy
  @dy += 1 if (@tick % 3).zero?
  calc_rotation
  check_hit(players)
  hit if  @y > WINDOW_SIZE_Y
  hit if  @x > WINDOW_SIZE_X
  hit if @x.negative?
end