Class: Doom::Game::PlayerState
- Inherits:
-
Object
- Object
- Doom::Game::PlayerState
- Defined in:
- lib/doom/game/player_state.rb
Overview
Tracks player state for HUD display and weapon rendering
Constant Summary collapse
- WEAPON_FIST =
Weapons
0- WEAPON_PISTOL =
1- WEAPON_SHOTGUN =
2- WEAPON_CHAINGUN =
3- WEAPON_ROCKET =
4- WEAPON_PLASMA =
5- WEAPON_BFG =
6- WEAPON_CHAINSAW =
7- WEAPON_NAMES =
Weapon symbols for graphics lookup
{ WEAPON_FIST => :fist, WEAPON_PISTOL => :pistol, WEAPON_SHOTGUN => :shotgun, WEAPON_CHAINGUN => :chaingun, WEAPON_ROCKET => :rocket, WEAPON_PLASMA => :plasma, WEAPON_BFG => :bfg, WEAPON_CHAINSAW => :chainsaw }.freeze
- ATTACK_DURATIONS =
Attack durations in tics (at 35fps), matching DOOM's weapon state sequences
{ WEAPON_FIST => 14, # Punch windup + swing WEAPON_PISTOL => 16, # S_PISTOL: 6+4+5+1 tics WEAPON_SHOTGUN => 40, # Pump action cycle WEAPON_CHAINGUN => 8, # Rapid fire (2 shots per cycle) WEAPON_ROCKET => 20, # Rocket launch + recovery WEAPON_PLASMA => 8, # Fast energy weapon WEAPON_BFG => 60, # Long charge + fire WEAPON_CHAINSAW => 6 # Fast melee }.freeze
- VIEWHEIGHT =
Smooth step-up/down (matching Chocolate Doom's P_CalcHeight / P_ZMovement)
41.0- VIEWHEIGHT_HALF =
VIEWHEIGHT / 2.0
- DELTA_ACCEL =
deltaviewheight += FRACUNIT/4 per tic
0.25
- MAXBOB =
View bob (camera bounce when walking, matching Chocolate Doom's P_CalcHeight + P_XYMovement + P_Thrust from p_user.c / p_mobj.c)
16.0- STOPSPEED =
Maximum bob amplitude (0x100000 in fixed-point = 16 map units)
0.0625
- BOB_DECAY_RATE =
Continuous-time equivalents of DOOM's per-tic constants (35 fps tic rate):
FRICTION = 0xE800/0x10000 = 0.90625 per tic decay_rate = -ln(0.90625) * 35 = 3.44/sec walk thrust = forwardmove(25) * 2048 / 65536 = 0.78 map units/tic = 27.3/sec terminal velocity = 27.3 / 3.44 = 7.56 -> bob = 7.56^2/4 = 14.3 (89% of MAXBOB) 3.44- BOB_THRUST =
Friction as continuous decay rate (1/sec)
26.0- BOB_FREQUENCY =
Walk thrust (map units/sec), gives terminal ~7.5
11.0- BOB_MOM_SCALE =
Compute view bob from actual movement momentum. Matches Chocolate Doom P_CalcHeight:
bob = (momx*momx + momy*momy) >> 2, capped at MAXBOB viewz += finesine[angle] * bob/2Momentum is in units/sec; DOOM's bob uses units/tic (divide by 35).
1.0 / (35.0 * 35.0 * 4.0)
Instance Attribute Summary collapse
-
#ammo_bullets ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#ammo_cells ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#ammo_rockets ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#ammo_shells ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#armor ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#attack_frame ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#attack_tics ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#attacking ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#bob_amount ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#bob_angle ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#damage_count ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#dead ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#death_tic ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#deltaviewheight ⇒ Object
readonly
Bob cycle frequency (rad/sec): FINEANGLES/20 * 35 / 8192 * 2*PI.
-
#god_mode ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#has_weapons ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#health ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#infinite_ammo ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#is_moving ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#keys ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#max_armor ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#max_bullets ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#max_cells ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#max_health ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#max_rockets ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#max_shells ⇒ Object
Red flash intensity (0-8), decays each tic.
-
#view_bob_offset ⇒ Object
readonly
Bob cycle frequency (rad/sec): FINEANGLES/20 * 35 / 8192 * 2*PI.
-
#viewheight ⇒ Object
readonly
Bob cycle frequency (rad/sec): FINEANGLES/20 * 35 / 8192 * 2*PI.
-
#weapon ⇒ Object
Red flash intensity (0-8), decays each tic.
Instance Method Summary collapse
-
#apply_fall_impact(momz) ⇒ Object
Called on landing after a fall.
- #can_attack? ⇒ Boolean
- #current_ammo ⇒ Object
- #die ⇒ Object
-
#initialize ⇒ PlayerState
constructor
A new instance of PlayerState.
-
#notify_step(step_amount) ⇒ Object
Called when player moves onto a different floor height.
- #reset ⇒ Object
-
#set_movement_momentum(momx, momy) ⇒ Object
Set movement momentum directly (called from GosuWindow with actual movement momentum, which already has thrust + friction applied).
- #start_attack ⇒ Object
- #switch_weapon(weapon_num) ⇒ Object
-
#take_damage(amount) ⇒ Object
Apply damage (from environment or enemies).
- #update_attack ⇒ Object
- #update_bob(delta_time) ⇒ Object
-
#update_damage_count ⇒ Object
Decay damage flash each tic.
- #update_view_bob(delta_time) ⇒ Object
-
#update_viewheight ⇒ Object
Gradually restore viewheight to VIEWHEIGHT (called each tic).
- #weapon_bob_x ⇒ Object
- #weapon_bob_y ⇒ Object
- #weapon_name ⇒ Object
Constructor Details
#initialize ⇒ PlayerState
Returns a new instance of PlayerState.
62 63 64 |
# File 'lib/doom/game/player_state.rb', line 62 def initialize reset end |
Instance Attribute Details
#ammo_bullets ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def ammo_bullets @ammo_bullets end |
#ammo_cells ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def ammo_cells @ammo_cells end |
#ammo_rockets ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def ammo_rockets @ammo_rockets end |
#ammo_shells ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def ammo_shells @ammo_shells end |
#armor ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def armor @armor end |
#attack_frame ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def attack_frame @attack_frame end |
#attack_tics ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def attack_tics @attack_tics end |
#attacking ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def attacking @attacking end |
#bob_amount ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def bob_amount @bob_amount end |
#bob_angle ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def bob_angle @bob_angle end |
#damage_count ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def damage_count @damage_count end |
#dead ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def dead @dead end |
#death_tic ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def death_tic @death_tic end |
#deltaviewheight ⇒ Object (readonly)
Bob cycle frequency (rad/sec): FINEANGLES/20 * 35 / 8192 * 2*PI
60 61 62 |
# File 'lib/doom/game/player_state.rb', line 60 def deltaviewheight @deltaviewheight end |
#god_mode ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def god_mode @god_mode end |
#has_weapons ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def has_weapons @has_weapons end |
#health ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def health @health end |
#infinite_ammo ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def infinite_ammo @infinite_ammo end |
#is_moving ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def is_moving @is_moving end |
#keys ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def keys @keys end |
#max_armor ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def max_armor @max_armor end |
#max_bullets ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def max_bullets @max_bullets end |
#max_cells ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def max_cells @max_cells end |
#max_health ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def max_health @max_health end |
#max_rockets ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def max_rockets @max_rockets end |
#max_shells ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def max_shells @max_shells end |
#view_bob_offset ⇒ Object (readonly)
Bob cycle frequency (rad/sec): FINEANGLES/20 * 35 / 8192 * 2*PI
60 61 62 |
# File 'lib/doom/game/player_state.rb', line 60 def view_bob_offset @view_bob_offset end |
#viewheight ⇒ Object (readonly)
Bob cycle frequency (rad/sec): FINEANGLES/20 * 35 / 8192 * 2*PI
60 61 62 |
# File 'lib/doom/game/player_state.rb', line 60 def viewheight @viewheight end |
#weapon ⇒ Object
Red flash intensity (0-8), decays each tic
40 41 42 |
# File 'lib/doom/game/player_state.rb', line 40 def weapon @weapon end |
Instance Method Details
#apply_fall_impact(momz) ⇒ Object
Called on landing after a fall. Matches Chocolate Doom P_ZMovement: deltaviewheight = momz >> 3, producing a squat that update_viewheight then recovers via DELTA_ACCEL.
230 231 232 |
# File 'lib/doom/game/player_state.rb', line 230 def apply_fall_impact(momz) @deltaviewheight = momz / 8.0 end |
#can_attack? ⇒ Boolean
151 152 153 154 155 156 157 |
# File 'lib/doom/game/player_state.rb', line 151 def can_attack? return true if @weapon == WEAPON_FIST || @weapon == WEAPON_CHAINSAW return true if @infinite_ammo ammo = current_ammo ammo && ammo > 0 end |
#current_ammo ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/doom/game/player_state.rb', line 136 def current_ammo case @weapon when WEAPON_PISTOL, WEAPON_CHAINGUN @ammo_bullets when WEAPON_SHOTGUN @ammo_shells when WEAPON_ROCKET @ammo_rockets when WEAPON_PLASMA, WEAPON_BFG @ammo_cells else nil # Fist/chainsaw don't use ammo end end |
#die ⇒ Object
331 332 333 334 335 336 |
# File 'lib/doom/game/player_state.rb', line 331 def die @dead = true @death_tic = 0 @attacking = false @deltaviewheight = -VIEWHEIGHT / 8.0 # View drops to ground end |
#notify_step(step_amount) ⇒ Object
Called when player moves onto a different floor height. Matches Chocolate Doom P_ZMovement: reduce viewheight by the step amount so the camera doesn't snap, then let P_CalcHeight recover it smoothly.
220 221 222 223 224 225 |
# File 'lib/doom/game/player_state.rb', line 220 def notify_step(step_amount) return if step_amount == 0 @viewheight -= step_amount @deltaviewheight = (VIEWHEIGHT - @viewheight) / 8.0 end |
#reset ⇒ Object
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/doom/game/player_state.rb', line 66 def reset @health = 100 @armor = 0 @max_health = 100 @max_armor = 200 # Ammo @ammo_bullets = 50 @ammo_shells = 0 @ammo_rockets = 0 @ammo_cells = 0 @max_bullets = 200 @max_shells = 50 @max_rockets = 50 @max_cells = 300 # Start with fist and pistol @weapon = WEAPON_PISTOL @has_weapons = [true, true, false, false, false, false, false, false] # No keys @keys = { blue_card: false, yellow_card: false, red_card: false, blue_skull: false, yellow_skull: false, red_skull: false } # Attack state @attacking = false @attack_frame = 0 @attack_tics = 0 # Death state @dead = false @death_tic = 0 @damage_count = 0 # Cheats @god_mode = false @infinite_ammo = false # Weapon bob @bob_angle = 0.0 @bob_amount = 0.0 @is_moving = false # Smooth step height (P_CalcHeight viewheight/deltaviewheight) @viewheight = VIEWHEIGHT @deltaviewheight = 0.0 # View bob (camera bounce) - simulated momentum for P_CalcHeight @view_bob_offset = 0.0 @momx = 0.0 # Simulated X momentum (map units/sec, not actual movement) @momy = 0.0 # Simulated Y momentum # Held at zero and never read by game logic, but part of the snapshot # wire format and state fingerprint (PLAYER_STATE_FLOATS), so removing # them would need a snapshot version bump. Left in place deliberately. @thrust_x = 0.0 @thrust_y = 0.0 @view_bob_angle = 0.0 end |
#set_movement_momentum(momx, momy) ⇒ Object
Set movement momentum directly (called from GosuWindow with actual movement momentum, which already has thrust + friction applied).
259 260 261 262 |
# File 'lib/doom/game/player_state.rb', line 259 def set_movement_momentum(momx, momy) @momx = momx @momy = momy end |
#start_attack ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/doom/game/player_state.rb', line 159 def start_attack return unless can_attack? return if @attacking @attacking = true @attack_frame = 0 @attack_tics = 0 # Consume ammo (skipped with infinite ammo) return if @infinite_ammo case @weapon when WEAPON_PISTOL @ammo_bullets -= 1 if @ammo_bullets > 0 when WEAPON_SHOTGUN @ammo_shells -= 1 if @ammo_shells > 0 when WEAPON_CHAINGUN @ammo_bullets -= 1 if @ammo_bullets > 0 when WEAPON_ROCKET @ammo_rockets -= 1 if @ammo_rockets > 0 when WEAPON_PLASMA @ammo_cells -= 1 if @ammo_cells > 0 when WEAPON_BFG @ammo_cells -= 40 if @ammo_cells >= 40 end end |
#switch_weapon(weapon_num) ⇒ Object
293 294 295 296 297 298 299 |
# File 'lib/doom/game/player_state.rb', line 293 def switch_weapon(weapon_num) return unless weapon_num >= 0 && weapon_num < 8 return unless @has_weapons[weapon_num] return if @attacking @weapon = weapon_num end |
#take_damage(amount) ⇒ Object
Apply damage (from environment or enemies). Armor absorbs some.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/doom/game/player_state.rb', line 302 def take_damage(amount) return if @dead return if @god_mode absorbed = 0 if @armor > 0 absorbed = amount / 3 # Green armor absorbs 1/3 absorbed = @armor if absorbed > @armor @armor -= absorbed end actual = amount - absorbed @health -= actual # Red flash proportional to damage (capped at palette 8) @damage_count = [(@damage_count + (actual / 2.0)).ceil, 8].min return unless @health <= 0 @health = 0 @damage_count = 8 die end |
#update_attack ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/doom/game/player_state.rb', line 186 def update_attack return unless @attacking @attack_tics += 1 # Calculate which frame we're on based on tics duration = ATTACK_DURATIONS[@weapon] || 8 frame_count = @weapon == WEAPON_FIST ? 3 : 4 tics_per_frame = duration / frame_count @attack_frame = (@attack_tics / tics_per_frame).to_i # Attack finished? return unless @attack_tics >= duration @attacking = false @attack_frame = 0 @attack_tics = 0 end |
#update_bob(delta_time) ⇒ Object
206 207 208 209 210 211 212 213 214 215 |
# File 'lib/doom/game/player_state.rb', line 206 def update_bob(delta_time) if @is_moving # Increase bob while moving @bob_angle += delta_time * 10.0 @bob_amount = [@bob_amount + (delta_time * 16.0), 6.0].min else # Decay bob when stopped @bob_amount = [@bob_amount - (delta_time * 12.0), 0.0].max end end |
#update_damage_count ⇒ Object
Decay damage flash each tic
327 328 329 |
# File 'lib/doom/game/player_state.rb', line 327 def update_damage_count @damage_count -= 1 if @damage_count > 0 end |
#update_view_bob(delta_time) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/doom/game/player_state.rb', line 271 def update_view_bob(delta_time) dt = delta_time.clamp(0.001, 0.05) # P_CalcHeight: bob = (momx_per_tic^2 + momy_per_tic^2) / 4, capped at MAXBOB bob = ((@momx * @momx) + (@momy * @momy)) * BOB_MOM_SCALE bob = MAXBOB if bob > MAXBOB # Advance bob sine wave (FINEANGLES/20 per tic = ~11 rad/sec) @view_bob_angle += BOB_FREQUENCY * dt # viewz offset: sin(angle) * bob/2 @view_bob_offset = Math.sin(@view_bob_angle) * bob / 2.0 end |
#update_viewheight ⇒ Object
Gradually restore viewheight to VIEWHEIGHT (called each tic). Matches Chocolate Doom P_CalcHeight viewheight recovery loop. For step-up: viewheight < 41, delta > 0, accelerates upward. For step-down: viewheight > 41, delta < 0, decelerates then recovers.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/doom/game/player_state.rb', line 238 def update_viewheight @viewheight += @deltaviewheight if @viewheight > VIEWHEIGHT && @deltaviewheight >= 0 @viewheight = VIEWHEIGHT @deltaviewheight = 0.0 end if @viewheight < VIEWHEIGHT_HALF @viewheight = VIEWHEIGHT_HALF @deltaviewheight = 1.0 if @deltaviewheight <= 0 end return unless @deltaviewheight != 0 @deltaviewheight += DELTA_ACCEL @deltaviewheight = 0.0 if @deltaviewheight.abs < 0.01 && (@viewheight - VIEWHEIGHT).abs < 0.5 end |
#weapon_bob_x ⇒ Object
285 286 287 |
# File 'lib/doom/game/player_state.rb', line 285 def weapon_bob_x Math.cos(@bob_angle) * @bob_amount end |
#weapon_bob_y ⇒ Object
289 290 291 |
# File 'lib/doom/game/player_state.rb', line 289 def weapon_bob_y Math.sin(@bob_angle * 2) * @bob_amount * 0.5 end |
#weapon_name ⇒ Object
132 133 134 |
# File 'lib/doom/game/player_state.rb', line 132 def weapon_name WEAPON_NAMES[@weapon] end |