Class: Doom::Game::Player
- Inherits:
-
Object
- Object
- Doom::Game::Player
- Defined in:
- lib/doom/game/player.rb
Overview
A player as a first-class entity.
Before this existed the "player" was scattered across three objects: position and angle lived in Renderer, horizontal momentum lived in GosuWindow, and floor height lived in PlayerPhysics -- which made more than one of them impossible. Everything that identifies a player now lives here; Renderer is reduced to a camera that gets pointed at a pose.
Angles are radians internally (Renderer's old setters took degrees).
Instance Attribute Summary collapse
-
#angle ⇒ Object
Returns the value of attribute angle.
-
#cos_angle ⇒ Object
readonly
Returns the value of attribute cos_angle.
-
#frags ⇒ Object
Deathmatch score.
-
#id ⇒ Object
Returns the value of attribute id.
-
#momx ⇒ Object
Returns the value of attribute momx.
-
#momy ⇒ Object
Returns the value of attribute momy.
-
#prev_angle ⇒ Object
readonly
Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.
-
#prev_x ⇒ Object
readonly
Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.
-
#prev_y ⇒ Object
readonly
Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.
-
#prev_z ⇒ Object
readonly
Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.
-
#sin_angle ⇒ Object
readonly
Returns the value of attribute sin_angle.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#use_held ⇒ Object
Edge-trigger latch for the use key, per player: holding the key must not spam doors, and each player needs its own latch.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Instance Method Summary collapse
- #angle_degrees=(degrees) ⇒ Object
- #dead? ⇒ Boolean
-
#initialize(id: 0, state: PlayerState.new) ⇒ Player
constructor
A new instance of Player.
- #move_to(x, y) ⇒ Object
-
#place(x, y, z, angle_degrees) ⇒ Object
Place the player outright (spawn, teleport, respawn) and collapse the interpolation history so the camera doesn't sweep across the map.
-
#snapshot! ⇒ Object
Called at the start of each tic, before the simulation moves the player.
- #turn(degrees) ⇒ Object
-
#view_pose(alpha) ⇒ Object
Pose to render at,
alphaof the way from the previous tic to this one.
Constructor Details
#initialize(id: 0, state: PlayerState.new) ⇒ Player
Returns a new instance of Player.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/doom/game/player.rb', line 28 def initialize(id: 0, state: PlayerState.new) @id = id @state = state @x = 0.0 @y = 0.0 @z = 41.0 # eye height @momx = 0.0 @momy = 0.0 @use_held = false @frags = 0 self.angle = 0.0 snapshot! end |
Instance Attribute Details
#angle ⇒ Object
Returns the value of attribute angle.
15 16 17 |
# File 'lib/doom/game/player.rb', line 15 def angle @angle end |
#cos_angle ⇒ Object (readonly)
Returns the value of attribute cos_angle.
22 23 24 |
# File 'lib/doom/game/player.rb', line 22 def cos_angle @cos_angle end |
#frags ⇒ Object
Deathmatch score. It lives on the player rather than in a side table so it survives respawning, which throws away the whole PlayerState.
21 22 23 |
# File 'lib/doom/game/player.rb', line 21 def frags @frags end |
#id ⇒ Object
Returns the value of attribute id.
15 16 17 |
# File 'lib/doom/game/player.rb', line 15 def id @id end |
#momx ⇒ Object
Returns the value of attribute momx.
15 16 17 |
# File 'lib/doom/game/player.rb', line 15 def momx @momx end |
#momy ⇒ Object
Returns the value of attribute momy.
15 16 17 |
# File 'lib/doom/game/player.rb', line 15 def momy @momy end |
#prev_angle ⇒ Object (readonly)
Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.
26 27 28 |
# File 'lib/doom/game/player.rb', line 26 def prev_angle @prev_angle end |
#prev_x ⇒ Object (readonly)
Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.
26 27 28 |
# File 'lib/doom/game/player.rb', line 26 def prev_x @prev_x end |
#prev_y ⇒ Object (readonly)
Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.
26 27 28 |
# File 'lib/doom/game/player.rb', line 26 def prev_y @prev_y end |
#prev_z ⇒ Object (readonly)
Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.
26 27 28 |
# File 'lib/doom/game/player.rb', line 26 def prev_z @prev_z end |
#sin_angle ⇒ Object (readonly)
Returns the value of attribute sin_angle.
22 23 24 |
# File 'lib/doom/game/player.rb', line 22 def sin_angle @sin_angle end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
22 23 24 |
# File 'lib/doom/game/player.rb', line 22 def state @state end |
#use_held ⇒ Object
Edge-trigger latch for the use key, per player: holding the key must not spam doors, and each player needs its own latch.
18 19 20 |
# File 'lib/doom/game/player.rb', line 18 def use_held @use_held end |
#x ⇒ Object
Returns the value of attribute x.
15 16 17 |
# File 'lib/doom/game/player.rb', line 15 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
15 16 17 |
# File 'lib/doom/game/player.rb', line 15 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
15 16 17 |
# File 'lib/doom/game/player.rb', line 15 def z @z end |
Instance Method Details
#angle_degrees=(degrees) ⇒ Object
50 51 52 |
# File 'lib/doom/game/player.rb', line 50 def angle_degrees=(degrees) self.angle = degrees * Math::PI / 180.0 end |
#dead? ⇒ Boolean
100 101 102 |
# File 'lib/doom/game/player.rb', line 100 def dead? @state.dead end |
#move_to(x, y) ⇒ Object
58 59 60 61 |
# File 'lib/doom/game/player.rb', line 58 def move_to(x, y) @x = x.to_f @y = y.to_f end |
#place(x, y, z, angle_degrees) ⇒ Object
Place the player outright (spawn, teleport, respawn) and collapse the interpolation history so the camera doesn't sweep across the map.
65 66 67 68 69 70 71 72 73 |
# File 'lib/doom/game/player.rb', line 65 def place(x, y, z, angle_degrees) @x = x.to_f @y = y.to_f @z = z.to_f self.angle_degrees = angle_degrees @momx = 0.0 @momy = 0.0 snapshot! end |
#snapshot! ⇒ Object
Called at the start of each tic, before the simulation moves the player.
76 77 78 79 80 81 |
# File 'lib/doom/game/player.rb', line 76 def snapshot! @prev_x = @x @prev_y = @y @prev_z = @z @prev_angle = @angle end |
#turn(degrees) ⇒ Object
54 55 56 |
# File 'lib/doom/game/player.rb', line 54 def turn(degrees) self.angle = @angle + (degrees * Math::PI / 180.0) end |
#view_pose(alpha) ⇒ Object
Pose to render at, alpha of the way from the previous tic to this one.
Angle is interpolated along the shortest arc so wrapping past +/-PI
doesn't spin the view the long way round.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/doom/game/player.rb', line 86 def view_pose(alpha) a = alpha.clamp(0.0, 1.0) delta = @angle - @prev_angle delta -= 2 * Math::PI while delta > Math::PI delta += 2 * Math::PI while delta < -Math::PI [ @prev_x + ((@x - @prev_x) * a), @prev_y + ((@y - @prev_y) * a), @prev_z + ((@z - @prev_z) * a), @prev_angle + (delta * a) ] end |