# Represents a celestial body in the game that can be owned by a player. # It tracks its position and can hold various buildings. require 'securerandom' # A planet object in the game that can be owned by a player and contains buildings. # # @example # planet = Planet.new(100, 100, 100) # class Planet # The owner of the planet, if any. attr_reader :owner # The unique identifier for the planet. attr_reader :planet_id # Creates a new planet with random position in the game space. # # @param width [Integer] The width of the game space. # @param height [Integer] The height of the game space. # @param depth [Integer] The depth of the game space. # @return [Planet] The new planet instance. # def initialize(width, height, depth) @owner = nil @buildings = [] @position = [rand(width), rand(height), rand(depth)] @planet_id = SecureRandom.hex end # Determines if the planet can be claimed by a player. # A planet is claimable if it has no owner or no buildings. # # @return [Boolean] true if the planet can be claimed, false otherwise. # def claimable? if @owner.nil? return true elsif @buildings == [] return true end return false end # Claims the planet and sets the player as the owner. # # @param player [Player] The player claiming the planet. # def claim(player) @owner = player end # Called to update the planet during the game tick. # # @param delta_time [Float] The time elapsed since the last update. # def update(delta_time) end # Returns information about the planet to be sent to the client. # # @return [Hash] A hash containing the planet's ID and position. # def info_update { 'id' => @planet_id, 'position' => @position } end # Saves the planet to a file for persistence. # # @return [String] The planet ID that was saved. # def save_to_file File.open("game_data/#{self.planet_id}_planet_.save", 'wb') do |fi| fi.write(Marshal.dump(self)) end self.planet_id end # Loads a planet from a save file. # # @param fname [String] The filename of the save file. # @return [Planet] The loaded planet instance. # def self.load_from_save(fname) File.open("game_data/#{fname}_planet_.save", 'rb') do |fi| Marshal.load(fi.read) end end end