Fixed tests. Need to check for damage from AI code
This commit is contained in:
+46
-27
@@ -1,17 +1,27 @@
|
||||
# Nathan Hinton 22 Dec 2025
|
||||
# Planet file
|
||||
# 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 for the Game module
|
||||
# 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
|
||||
attr_reader :owner, :planet_id
|
||||
# The owner of the planet, if any.
|
||||
attr_reader :owner
|
||||
|
||||
# Creates a new planet.
|
||||
# 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.
|
||||
#
|
||||
# @param width [Integer] The width of the game
|
||||
# @param height [Integer] The height of the game
|
||||
# @param depth [Integer] The depth of the game
|
||||
def initialize(width, height, depth)
|
||||
@owner = nil
|
||||
@buildings = []
|
||||
@@ -19,8 +29,11 @@ class Planet
|
||||
@planet_id = SecureRandom.hex
|
||||
end
|
||||
|
||||
# Returns if the planet can be claimed. Currently requires that there be no
|
||||
# buildings on the planet.
|
||||
# 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
|
||||
@@ -30,45 +43,51 @@ class Planet
|
||||
return false
|
||||
end
|
||||
|
||||
# Function that allows a player to claim the planet and to become the owner.
|
||||
# Claims the planet and sets the player as the owner.
|
||||
#
|
||||
# @param player [Player] The player claiming the planet.
|
||||
#
|
||||
def claim(player)
|
||||
@owner = player
|
||||
end
|
||||
|
||||
# Function called to update the planet.
|
||||
# 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
|
||||
|
||||
|
||||
# Function to return info to the client about a planet
|
||||
# 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
|
||||
return {
|
||||
{
|
||||
'id' => @planet_id,
|
||||
'position' => @position
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
##########################
|
||||
##### UTIL FUNCTIONS #####
|
||||
##########################
|
||||
|
||||
|
||||
# Save a player to a file
|
||||
# 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|
|
||||
# Load resources for the player:
|
||||
fi.write(Marshal.dump(self))
|
||||
end
|
||||
return self.planet_id
|
||||
self.planet_id
|
||||
end
|
||||
|
||||
# Load a player from a file
|
||||
# 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|
|
||||
# Load resources for the player:
|
||||
Marshal.load(fi.read)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user