Class: Quake::Game::ItemPickups

Inherits:
Object
  • Object
show all
Defined in:
lib/quake/game/item_pickups.rb

Overview

Handles item pickup logic: checks proximity between player and item entities, applies effects to PlayerState, marks items as picked up.

Constant Summary collapse

PICKUP_RADIUS =

units (roughly player bbox width)

32.0
ITEMS =

Pickup definitions: classname -> { type:, … }

{
  # Health
  "item_health" => { type: :health, amount: 25 },

  # Armor
  "item_armor1" => { type: :armor, points: 100, armor_type: 1 }, # green
  "item_armor2" => { type: :armor, points: 150, armor_type: 2 }, # yellow
  "item_armorInv" => { type: :armor, points: 200, armor_type: 3 }, # red

  # Ammo
  "item_shells" => { type: :ammo, ammo_type: :shells, amount: 20 },
  "item_spikes" => { type: :ammo, ammo_type: :nails, amount: 25 },
  "item_rockets" => { type: :ammo, ammo_type: :rockets, amount: 5 },
  "item_cells" => { type: :ammo, ammo_type: :cells, amount: 6 },

  # Weapons (give weapon + starting ammo)
  "weapon_supershotgun" => {
    type: :weapon, weapon: :super_shotgun,
    ammo_type: :shells, ammo_amount: 5
  },
  "weapon_nailgun" => {
    type: :weapon, weapon: :nailgun,
    ammo_type: :nails, ammo_amount: 30
  },
  "weapon_supernailgun" => {
    type: :weapon, weapon: :super_nailgun,
    ammo_type: :nails, ammo_amount: 30
  },
  "weapon_grenadelauncher" => {
    type: :weapon, weapon: :grenade_launcher,
    ammo_type: :rockets, ammo_amount: 5
  },
  "weapon_rocketlauncher" => {
    type: :weapon, weapon: :rocket_launcher,
    ammo_type: :rockets, ammo_amount: 5
  },
  "weapon_lightning" => {
    type: :weapon, weapon: :lightning_gun,
    ammo_type: :cells, ammo_amount: 15
  },

  # Powerups
  "item_artifact_super_damage" => { type: :powerup, powerup: :quad },
  "item_artifact_invulnerability" => { type: :powerup, powerup: :pentagram },
  "item_artifact_envirosuit" => { type: :powerup, powerup: :biosuit },
  "item_artifact_invisibility" => { type: :powerup, powerup: :ring }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(entities) ⇒ ItemPickups

Returns a new instance of ItemPickups.



61
62
63
64
# File 'lib/quake/game/item_pickups.rb', line 61

def initialize(entities)
  @item_entities = entities.select { |e| ITEMS.key?(e.classname) }
  @picked_up = Set.new # entity object_ids that have been collected
end

Instance Method Details

#check_pickups(player_pos, player_state) ⇒ Object

Check for pickups near player position. Returns array of pickup event hashes (for sound/effect triggering).



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
# File 'lib/quake/game/item_pickups.rb', line 68

def check_pickups(player_pos, player_state)
  events = []

  @item_entities.each do |ent|
    next if @picked_up.include?(ent.object_id)

    dx = player_pos.x - ent.position.x
    dy = player_pos.y - ent.position.y
    dz = player_pos.z - ent.position.z
    dist_sq = dx * dx + dy * dy + dz * dz

    next if dist_sq > PICKUP_RADIUS * PICKUP_RADIUS

    defn = ITEMS[ent.classname]
    next unless defn

    picked = try_pickup(player_state, defn, ent)
    if picked
      @picked_up.add(ent.object_id)
      events << { classname: ent.classname, type: defn[:type], entity: ent }
    end
  end

  events
end

#picked_up?(entity) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/quake/game/item_pickups.rb', line 94

def picked_up?(entity)
  @picked_up.include?(entity.object_id)
end