Class: Termfront::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/termfront/player.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, angle:, weapons:) ⇒ Player

Returns a new instance of Player.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/termfront/player.rb', line 10

def initialize(x:, y:, angle:, weapons:)
  @x = x
  @y = y
  @angle = angle
  @weapons = weapons
  @weapon_idx = 0
  @last_fire = 0.0
  @shield = Config::SHIELD_MAX
  @health = Config::HEALTH_MAX
  @dead = false
  @game_time = 0.0
  @last_damage = -Config::SHIELD_DELAY
  @damage_flash = 0
  @fire_flash = 0
  @beep_count = 0
  @last_beep = 0.0
  @regen_active = false
  @swap_pressed = false
  @pickup_pressed = false
  @drops = []
end

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



5
6
7
# File 'lib/termfront/player.rb', line 5

def angle
  @angle
end

#beep_countObject

Returns the value of attribute beep_count.



5
6
7
# File 'lib/termfront/player.rb', line 5

def beep_count
  @beep_count
end

#damage_flashObject

Returns the value of attribute damage_flash.



5
6
7
# File 'lib/termfront/player.rb', line 5

def damage_flash
  @damage_flash
end

#deadObject

Returns the value of attribute dead.



5
6
7
# File 'lib/termfront/player.rb', line 5

def dead
  @dead
end

#dropsObject

Returns the value of attribute drops.



5
6
7
# File 'lib/termfront/player.rb', line 5

def drops
  @drops
end

#fire_flashObject

Returns the value of attribute fire_flash.



5
6
7
# File 'lib/termfront/player.rb', line 5

def fire_flash
  @fire_flash
end

#game_timeObject

Returns the value of attribute game_time.



5
6
7
# File 'lib/termfront/player.rb', line 5

def game_time
  @game_time
end

#healthObject

Returns the value of attribute health.



5
6
7
# File 'lib/termfront/player.rb', line 5

def health
  @health
end

#last_beepObject

Returns the value of attribute last_beep.



5
6
7
# File 'lib/termfront/player.rb', line 5

def last_beep
  @last_beep
end

#last_damageObject

Returns the value of attribute last_damage.



5
6
7
# File 'lib/termfront/player.rb', line 5

def last_damage
  @last_damage
end

#last_fireObject

Returns the value of attribute last_fire.



5
6
7
# File 'lib/termfront/player.rb', line 5

def last_fire
  @last_fire
end

#pickup_pressedObject

Returns the value of attribute pickup_pressed.



5
6
7
# File 'lib/termfront/player.rb', line 5

def pickup_pressed
  @pickup_pressed
end

#regen_activeObject

Returns the value of attribute regen_active.



5
6
7
# File 'lib/termfront/player.rb', line 5

def regen_active
  @regen_active
end

#shieldObject

Returns the value of attribute shield.



5
6
7
# File 'lib/termfront/player.rb', line 5

def shield
  @shield
end

#swap_pressedObject

Returns the value of attribute swap_pressed.



5
6
7
# File 'lib/termfront/player.rb', line 5

def swap_pressed
  @swap_pressed
end

#weapon_idxObject

Returns the value of attribute weapon_idx.



5
6
7
# File 'lib/termfront/player.rb', line 5

def weapon_idx
  @weapon_idx
end

#weaponsObject

Returns the value of attribute weapons.



5
6
7
# File 'lib/termfront/player.rb', line 5

def weapons
  @weapons
end

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/termfront/player.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/termfront/player.rb', line 5

def y
  @y
end

Instance Method Details

#apply_damage(amount) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/termfront/player.rb', line 40

def apply_damage(amount)
  @last_damage = @game_time
  @damage_flash = 3

  if @shield > 0
    overflow = amount - @shield
    @shield = [(@shield - amount), 0].max
    @health = [@health - [overflow, 0].max, 0].max if @shield == 0
  else
    @health = [@health - amount, 0].max
  end

  @dead = true if @health <= 0
end

#current_weaponObject



32
33
34
# File 'lib/termfront/player.rb', line 32

def current_weapon
  @weapons[@weapon_idx]
end

#process_fire(enemies, map) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/termfront/player.rb', line 115

def process_fire(enemies, map)
  dx = Math.cos(@angle)
  dy = Math.sin(@angle)

  weapon = current_weapon

  best = nil
  best_d = 1e30
  enemies.each do |e|
    next unless e.alive

    ex = e.x - @x
    ey = e.y - @y
    dot = ex * dx + ey * dy
    next if dot < 0.1

    perp = (ex * (-dy) + ey * dx).abs
    next if perp > weapon.hit_width

    if dot < best_d && map.line_of_sight?(@x, @y, e.x, e.y)
      best = e
      best_d = dot
    end
  end
  return unless best

  best.take_damage(1)
  return if best.alive

  @drops << DropItem::Weapon.new(x: best.x, y: best.y, type: best.drop_type, ammo: best.drop_ammo)
end

#swap_weaponObject



36
37
38
# File 'lib/termfront/player.rb', line 36

def swap_weapon
  @weapon_idx = 1 - @weapon_idx
end

#try_pickupObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/termfront/player.rb', line 92

def try_pickup
  nearest = nil
  best_d2 = Config::PICKUP_RADIUS**2
  @drops.each do |d|
    d2 = (d.x - @x)**2 + (d.y - @y)**2
    if d2 < best_d2
      nearest = d
      best_d2 = d2
    end
  end
  return unless nearest

  cur = current_weapon
  if cur.type_id == nearest.type
    max = cur.max_ammo
    cur.ammo = [cur.ammo + nearest.ammo, max].min if max
  else
    @drops << DropItem::Weapon.new(x: @x, y: @y, type: cur.type_id, ammo: cur.ammo)
    @weapons[@weapon_idx] = Weapon::Base.build(nearest.type, nearest.ammo)
  end
  @drops.delete(nearest)
end

#update_shield(dt, stdout, audio: nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/termfront/player.rb', line 55

def update_shield(dt, stdout, audio: nil)
  regen_now = @shield < Config::SHIELD_MAX && (@game_time - @last_damage) >= Config::SHIELD_DELAY
  if regen_now
    unless @regen_active
      @regen_active = true
      if audio
        audio.play_loop_se(:shield_regen)
      else
        stdout.syswrite("\a")
      end
    end
    @shield = [@shield + Config::SHIELD_REGEN * dt, Config::SHIELD_MAX].min
  else
    audio&.stop_loop_se(:shield_regen) if @regen_active
    @regen_active = false
  end
  @damage_flash -= 1 if @damage_flash > 0

  if @shield >= Config::SHIELD_MAX && @health < Config::HEALTH_MAX
    @health = [@health + Config::SHIELD_REGEN * dt, Config::HEALTH_MAX].min
  end

  return unless @shield == 0 && @health > 0

  @beep_count = 3 if @beep_count <= 0
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  return unless (now - @last_beep) >= Config::BEEP_INTERVAL

  if audio
    audio.play_se(:shield_alarm)
  else
    stdout.syswrite("\a")
  end
  @last_beep = now
  @beep_count -= 1
end