Component = require("ecs.component") world = require("world") log = require("log") network_log = {} class NetworkedComponent extends Component @q: {} new: (name, properties) => assert(properties.id == nil, "networked component's id needs to be nil") assert(properties.name == nil, "network component's name needs to be nil") properties.id = world.level_sync.entid world.level_sync.entid += 1 nwc = @ update = (key, value) => if not world.hub error("Tried to update network on the client") return if not properties[key] error("Trying to set key not defined in properties:" .. key) properties[key] = value properties.last_update = am.eval_js("Date.now();") nwc.__class.q[nwc] = true -- Broadcast update to other peers through client --world.hub\broadcast("update", properties) --actually, we only want to update each network component *at most* once per frame. proxy = {} setmetatable(proxy, { __index: properties, __newindex: update }) super(name, proxy) @net_properties = properties join: (e, cid) => @@node\action("Send Updates", ()-> @@update_dirty! ) @net_properties.id = e.id @net_properties.name = cid log.info("Added networked componenet name " .. @name .. " to entity id " .. e.id, {"net","server","ecs"}) -- Send initial create message through client --if world.hub -- Actually, entity creation is hard, just sync each entity individually. --world.hub\broadcast("create", properties) super(e) pack: () => assert(@entity, "Tried to pack on a NetworkedComponent without an Entity") log.info("Packing data from proxy:" .. tostring(@proxy), {"net","ecs"}) return am.to_json({ id: @entity.id data: @net_properties time: am.current_time! }) unpack: () -> assert(@entity, "Tried to unpack on a NEtworkedComponent without an Entity") @update_dirty: () => for component, _ in pairs(@q) world.hub\broadcast("update", component.net_properties) @q = {} -- Each different kind of entity needs to have it's own create and listen network -- hooks, it's way too complicated and heavy to push every entity with every componenet -- over the network. assert(NetworkedComponent.q, "No queue found!") NetworkedComponent