Files
space_game_server/game/universe_array.rb
T

48 lines
1.5 KiB
Ruby

# Nathan Hinton 1 Oct 2025
# Create a custom hash class for the universe.
require 'logger'
require './position'
# This will contain the universe hash inside it. It will allow for array type
# indexing as well as point type indexing
class UniverseArray < Array
@@logger = Logger.new('logs/universe.log')
# Custom accessor. This allows us to access either with a position object
# (preferred as it does validation) or direct access.
#
# @param index [Integer, Position] If it is an integer nothing special. If a
# Position it will be intepreted for us into the regular indexint.
# @param rest [Object] Actually, I have no idea why this is here. (copy paste)
# @return Whatever is at that position. Can be anything.
def [](index, *rest)
#require 'byebug';debugger
if index.is_a?(Position)
if self[index.y_pos].nil?
@@logger.debug "attempted access outside of known universe!"
return nil
end
return self[index.y_pos][index.x_pos]
else
super
end
end
# Custom writer. This allows us to access either with a position object
# (preferred as it does validation) or direct access.
#
# @param index [Integer, Position] If it is an integer nothing special. If a
# Position it will be intepreted for us into the regular indexint.
# @param rest [Object] This (if intercepted) has the value we should assign
def []=(index, *rest)
if index.is_a?(Position)
return self[index.y_pos][index.x_pos] = rest[0]
else
super
end
end
end