At the heart of the power system are four core components: batteries, devices, plugs and female plugs. Each plays a distinct role in simulating a modular, interactive electrical network.
Battery
componetns act as energy sources, storing charge and providing power to connected devices. Each battery tracks its current charge, voltage, and can simulate both charging and discharging. The battery’s OnUpdate method manages the flow of energy, iterating through all connected plugs and updating the charge based on the power consumption of attached devices.
Devices are anything that consumes (or sometimes generates) power—like lamps or generators. Every device inherits from a common DeviceBase
class, which exposes properties like IsActive
, IsPowered
, and PowerConsumption
. Devices react to changes in their power state via the OnPowerChange()
method, allowing for custom behaviors (such as turning on/off a light or playing a sound) when power is toggled.
Plugs serve as the physical connectors between batteries and devices. The FemalePlugDetector
component handles plug-in and unplug events, manages references to connected batteries or devices, and ensures that only valid connections are made (e.g., you can’t plug a battery into another battery). When a plug is inserted, the system updates all relevant references and triggers the appropriate state changes in the battery, device, and the plug(s). The Plug
component acts as a middle man between the devices and batteries to enable them to be plugged in.
This modular approach makes it easy to expand the system with new device types or power sources, and provides a solid foundation for simulating realistic electrical interactions in-game.
Thanks tytimp4_