Finished testing and docs for now. Time for new features!
This commit is contained in:
+52
-8
@@ -4,6 +4,7 @@
|
||||
require 'logger'
|
||||
|
||||
require './planet'
|
||||
require './star'
|
||||
require './position'
|
||||
require './universe_array'
|
||||
|
||||
@@ -60,6 +61,7 @@ class Universe
|
||||
|
||||
# Setup internal lists:
|
||||
@player_list = []
|
||||
@planet_list = []
|
||||
@ship_list = []
|
||||
|
||||
# initizlize RNG
|
||||
@@ -73,6 +75,8 @@ class Universe
|
||||
end # Initialize
|
||||
|
||||
|
||||
# Create the universe. This will setup all of the planets and a blank slate
|
||||
# for the universe.
|
||||
def create_universe()
|
||||
# Set center of universe
|
||||
@center = Position.new(@universe_size + 2, @universe_size)
|
||||
@@ -96,12 +100,17 @@ class Universe
|
||||
|
||||
# Skip if it is the center
|
||||
if position == @center
|
||||
@universe_map[position] = Star.new()
|
||||
next
|
||||
end
|
||||
|
||||
# Get what the planet should be:
|
||||
spot = nil
|
||||
if @random.rand < @create_chances[:planet]
|
||||
spot = Planet.new(@random)
|
||||
@planet_list.push(spot)
|
||||
#else
|
||||
# spot = ''
|
||||
end
|
||||
@@logger.debug "Creating something at (#{position})"
|
||||
@universe_map[position] = spot
|
||||
@@ -119,7 +128,6 @@ class Universe
|
||||
# @return [Integer] The distance between points in the universe.
|
||||
def distance(position_a, position_b)
|
||||
result = 0 # We can just add the abs diff of each direction.
|
||||
require 'byebug';debugger if position_a.nil?
|
||||
dist_x = (position_b.x_pos - position_a.x_pos).abs
|
||||
dist_y = (position_b.y_pos - position_a.y_pos).abs
|
||||
|
||||
@@ -176,9 +184,9 @@ class Universe
|
||||
# Print what we are:
|
||||
value = @universe_map[position]
|
||||
nextline += ' '
|
||||
if position == @center
|
||||
if value.is_a?(Star)
|
||||
print 'O'
|
||||
elsif value.class == Planet
|
||||
elsif value.is_a?(Planet)
|
||||
print 'P'
|
||||
else
|
||||
print ' '
|
||||
@@ -262,6 +270,7 @@ class Universe
|
||||
# @param player [Player] The player who owns the ship
|
||||
# @param position [Position] The position that the ship should be created at
|
||||
def create_ship(player, position)
|
||||
return false unless validate_player(player)
|
||||
end
|
||||
|
||||
# Move a player's ship to a given position
|
||||
@@ -270,6 +279,15 @@ class Universe
|
||||
# @param ship [Ship] The ship that should be moved
|
||||
# @param position [Position] The position the ship should be moved to.
|
||||
def move_ship(player, ship, position)
|
||||
return false unless validate_player(player)
|
||||
# Ensure the ship exists before moving it.
|
||||
end
|
||||
|
||||
# Remove a ship from the game
|
||||
# @param player [Player] The player who owns the ship to be removed
|
||||
# @param ship [Ship] The ship to be removed
|
||||
def remove_ship(player, ship)
|
||||
return false unless validate_player(player)
|
||||
end
|
||||
|
||||
|
||||
@@ -301,14 +319,24 @@ class Universe
|
||||
#
|
||||
# @param player [Player] The player who should be added to the universe
|
||||
def add_player(player)
|
||||
@player_list.push(player)
|
||||
# Give the player a planet
|
||||
free_planets = []
|
||||
planets.each do |planet|
|
||||
free_planets.push(planet) if planet.owner.nil?
|
||||
end
|
||||
|
||||
return 'Added player to universe'
|
||||
if free_planets == []
|
||||
@@logger.debug 'Failed to find an unoccopuied planet for new player!'
|
||||
return false
|
||||
end
|
||||
home_planet = free_planets.sample(random: @random)
|
||||
# Can;t figure out how to test this...
|
||||
# if !home_planet.change_owner(player)
|
||||
# raise Exception.new('Failed to allocate a new player a home planet!')
|
||||
# end
|
||||
home_planet.change_owner(player)
|
||||
@player_list.push(player)
|
||||
@@logger.debug "Added player '#{player.name}' with home planet #{home_planet}"
|
||||
return true
|
||||
end
|
||||
|
||||
# Remove a player from the list in this universe
|
||||
@@ -316,15 +344,30 @@ class Universe
|
||||
# @param player [Player] The player that should be removed from the player list.
|
||||
def remove_player(player)
|
||||
if @player_list.delete(player).nil?
|
||||
return 'Failed to remove player! Does not exist in this universe!'
|
||||
@@logger.debug 'Failed to remove player! Does not exist in this universe! Returning true anyway though!'
|
||||
end
|
||||
return 'Removed player from universe'
|
||||
@@logger.debug "Removed player '#{player.name}' from universe"
|
||||
return true
|
||||
end
|
||||
|
||||
# Validates if a player exists and is in a good state in the server
|
||||
#
|
||||
# @param player [Player] The player to validate
|
||||
def validate_player(player)
|
||||
# check if in player list:
|
||||
unless @player_list.include?(player)
|
||||
@@logger.warn "Player '#{player}' Tried to perform an action but is an invalid player!"
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
# Returns a list of ships owned by the player
|
||||
#
|
||||
# @param player [Player] The player who wants to get their ships.
|
||||
# @return [Array<Ship>]
|
||||
def get_player_ships(player)
|
||||
return [] unless validate_player(player)
|
||||
result = []
|
||||
@ship_list.each do |ship|
|
||||
if ship.owner == player
|
||||
@@ -338,6 +381,7 @@ class Universe
|
||||
#
|
||||
# @param player [Player] The player who wants to get their planets.
|
||||
def get_player_planets(player)
|
||||
return [] unless validate_player(player)
|
||||
result = []
|
||||
@planet_list.each do |planet|
|
||||
if planet.owner == player
|
||||
|
||||
Reference in New Issue
Block a user