working on reimplimenting server
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
# Nathan Hinton 19 Dec 2025
|
||||
|
||||
|
||||
require './space_object'
|
||||
|
||||
|
||||
# This class is for buildings that are built on a planet. Currently it
|
||||
# inherrits the SpaceObject class however it should probably inherrit a class
|
||||
# that is for planets.
|
||||
class Building<SpaceObject
|
||||
attr_reader :name, :type
|
||||
|
||||
# Create a new building object. You can pass in a name and a type.
|
||||
#
|
||||
# @param player [Player] The player who owns this building
|
||||
# @param name [String] The name of the building
|
||||
# @param type [String] The type of the building
|
||||
def initialize(player, planet, name: 'default name', type: 'Central Post')
|
||||
super()
|
||||
# Valiate type:
|
||||
if not get_config['buildings'].include? type
|
||||
raise BuildingError, "Invalid building type"
|
||||
end
|
||||
@player = player
|
||||
@planet = planet
|
||||
@name = name
|
||||
@type = type
|
||||
end
|
||||
|
||||
# Override the superclass tick method. This will add the resources that ar
|
||||
# produced to the player and the planet that the building belongs to.
|
||||
def tick
|
||||
get_config['buildings'][@type]['produces'].each do |resource, value|
|
||||
# Send some to the player (Global resoures)
|
||||
if ['metal', 'credits', 'crystals'].include? resource
|
||||
@player.resources[resource] += value
|
||||
else
|
||||
@planet.resources[resource] += value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Error class for the buildings
|
||||
class BuildingError < StandardError
|
||||
end
|
||||
Reference in New Issue
Block a user