working more
This commit is contained in:
+22
-13
@@ -1,7 +1,12 @@
|
||||
# Nathan Hinton 1 Oct 2025
|
||||
|
||||
|
||||
require 'logger'
|
||||
|
||||
require './planet'
|
||||
require './position'
|
||||
require './universe_array'
|
||||
|
||||
|
||||
# Universe class. Holds a universe and provides the api for it. Universes are
|
||||
# created on a 2d grid. I would like it to be hexagonal movement type so that
|
||||
@@ -42,12 +47,15 @@ require './planet'
|
||||
# @param name [String] The name of the game
|
||||
# @param universe_size [Integer] Controlls the size of the generated universe. This is a radius from the center point.
|
||||
class Universe
|
||||
def initialize(name, universe_size: 4, create_chances: {planet: 1}, seed: nil, verbose: true, debug: false)
|
||||
# Setup logger for this class:
|
||||
sh 'mkdir -p logs'
|
||||
@@logger = Logger.new('logs/universe.log')
|
||||
|
||||
def initialize(name, universe_size: 4, create_chances: {planet: 1}, seed: nil)
|
||||
|
||||
@name = name
|
||||
@universe_size = universe_size
|
||||
@create_chances = create_chances
|
||||
@verbose = verbose
|
||||
@debug = debug
|
||||
|
||||
# Setup internal lists:
|
||||
@player_list = []
|
||||
@@ -63,11 +71,11 @@ class Universe
|
||||
end
|
||||
|
||||
# Set center of universe
|
||||
@center = Position.new([@universe_size + 2, @universe_size ])
|
||||
@center = Position.new(@universe_size + 2, @universe_size)
|
||||
|
||||
puts "Generating Universe '#{@name}' with seed #{@seed}" if @verbose
|
||||
@@logger.info "Generating Universe '#{@name}' with seed #{@seed}"
|
||||
puts "Center is #{@center}"
|
||||
@universe_map = []
|
||||
@universe_map = UniverseArray.new()
|
||||
# Generate the universe. First generate the x values:
|
||||
(0 .. (@universe_size * 2)).each do |y_pos|
|
||||
@universe_map[y_pos] = []
|
||||
@@ -75,7 +83,7 @@ class Universe
|
||||
# Make the grid work right, x is actually skipping every other.
|
||||
x_pos *= 2
|
||||
x_pos += 1 if y_pos.odd?
|
||||
position = Position.new([x_pos, y_pos])
|
||||
position = Position.new(x_pos, y_pos)
|
||||
|
||||
# Skip if the node is too far
|
||||
if distance(@center, position) >= @universe_size
|
||||
@@ -87,7 +95,7 @@ class Universe
|
||||
if @random.rand < @create_chances[:planet]
|
||||
spot = Planet.new(@random)
|
||||
end
|
||||
puts "Creating something at (#{position.x_pos} #{position.y_pos})" if @debug
|
||||
@@logger.debug "Creating something at (#{position.x_pos} #{position.y_pos})"
|
||||
@universe_map[position.y_pos][position.x_pos] = spot
|
||||
end # Create y points
|
||||
end # Create x points
|
||||
@@ -114,7 +122,7 @@ class Universe
|
||||
if dist_x >= dist_y
|
||||
res = (dist_x + dist_y) / 2
|
||||
end
|
||||
puts "Distance: #{point_a} is #{point_b} #{res} units away" if @debug
|
||||
@@logger.debug "Distance: #{position_a} and #{position_b} are #{res} units away"
|
||||
return res
|
||||
end # Function distance
|
||||
|
||||
@@ -140,18 +148,19 @@ class Universe
|
||||
x_pos *= 2
|
||||
x_pos += 1 if y_pos.odd?
|
||||
|
||||
position = Position.new(x_pos, y_pos)
|
||||
# Print empty space if we are outside the universe (For items)
|
||||
if distance(@center, [x_pos, y_pos]) >= @universe_size
|
||||
if distance(@center, position) >= @universe_size
|
||||
print ' '
|
||||
next
|
||||
end
|
||||
|
||||
# Print what we are:
|
||||
position = @universe_map[y_pos][x_pos]
|
||||
value = @universe_map[position.y_pos][position.x_pos]
|
||||
nextline += ' '
|
||||
if [x_pos, y_pos] == @center
|
||||
print 'O'
|
||||
elsif position.class == Planet
|
||||
elsif value.class == Planet
|
||||
print 'P'
|
||||
else
|
||||
print ' '
|
||||
@@ -161,7 +170,7 @@ class Universe
|
||||
if y_pos < @universe_size
|
||||
nextline += '/ \\'
|
||||
end
|
||||
if distance(@center, [x_pos + 2, y_pos]) < @universe_size
|
||||
if distance(@center, Position.new(x_pos + 2, y_pos)) < @universe_size
|
||||
print '---'
|
||||
if y_pos >= @universe_size
|
||||
nextline += '\\ /'
|
||||
|
||||
Reference in New Issue
Block a user