项目作者: Dvergar

项目描述 :
Entity Component System with network support (for haxe)
高级语言: Haxe
项目地址: git://github.com/Dvergar/OSIS.git
创建时间: 2014-10-06T21:54:32Z
项目社区:https://github.com/Dvergar/OSIS

开源协议:

下载


OSIS Logo

API here : http://dvergar.github.io/osis/ (latest, 0.8.0)
Available on haxelib.

Entity Component System architecture with networking support (for haxe).

  • Simple API
  • Simple source
  • Networking is optional, none of the network API is polluating the ECS
  • API support for shared code betwen client & server
  • Automatic & fine-grained serialization of components via PODStream
  • Avoids magic !

(Iteration from old ECS-Networking-Haxe)

Check out the sample for a quick overview of the architecture. The library assumes you are in a client/server architecture where the server is authoritative.

ECS ?

Entity is an aggregation of data-only components
Logics are in systems
Systems act on components

Networking

  • Components updates are always done from server to clients (not the other way) and only at your demand.
  • Messages are the only types of network events that can go both ways.
  • In a game w/ this framework, communications should sum up to the client sending a list of pressed keys and/or events and the server dispatching everything else.

Notes

  • Based on my network library Anette TCP (no websockets)
  • 64 components max, 64 entity sets max
  • Will soon make it network-library agnostic via an API as i want to have websockets w/ hxWebSockets soon. And udprotean for UDP later.

Usage

Setup

  1. var em:EntityManager = new EntityManager();

If you’re going networked you can get the network entity manager via.

  1. var net = em.listen("127.0.0.1", 32000);

on the server and

  1. var net = em.connect("127.0.0.1", 32000);

on the client.

EntitySet

An entity set will allow to manipulate entities having the specified components; here CPosition and CMonster.

  1. var entitySet = em.getEntitySet([CPosition, CMonster]);

You will then be able to iterate through entitySet.adds, entitySet.changes, entitySet.removes and entitySet.entities. entitySet.applyChanges() will update the entity set to the last changes.

System

A system is a really simple structure that will give you a loop where you can iterate over your entity sets and an init where you will initialize your entity sets.

Example :

  1. class MovementSystem extends System
  2. {
  3. var entitySet:EntitySet;
  4. public override function init()
  5. entitySet = em.getEntitySet([CPosition, CMonster]);
  6. public override function loop()
  7. {
  8. entitySet.applyChanges();
  9. for(entity in entitySet.entities)
  10. {
  11. var pos = entity.get(CPosition);
  12. pos.x += 0.1;
  13. net.markChanged(entity, pos, entitySet);
  14. }
  15. }
  16. }

net.markChanged here will notify the change to all the clients.

Network event

The content of an event is called a Message and will be shared among client & server, here is an example :

  1. class MessageHello implements Message
  2. {
  3. @String public var txt:String;
  4. public function new() {}
  5. }

The receiver registers to the event

  1. net.addEvent(MessageHello, function(msg:MessageHello, connection:Connection) {
  2. trace(msg.txt);
  3. });

And the sender sends the message

  1. var msg = new MessageHello();
  2. msg.txt = "coucou";
  3. net.sendEvent(msg);

Templates

A template is a set of components defining a specific entity

  1. public function player()
  2. {
  3. var entity = em.createEntity();
  4. var pos = em.addComponent(entity, new CPosition());
  5. return entity;
  6. }

Adds a template like this
em.addTemplate("player", player);
and call the template like that em.createEntity("player");

Plugging everything together

  1. class Server
  2. {
  3. var em:EntityManager = new EntityManager();
  4. var net:NetEntityManager;
  5. public function new()
  6. {
  7. net = em.listen("127.0.0.1", 32000);
  8. net.onConnection = onConnection;
  9. net.onDisconnection = onDisconnection;
  10. net.addEvent(MessageHello, onMessage);
  11. em.addSystem(new MovementSystem());
  12. while(true)
  13. {
  14. em.fixedUpdate(function()
  15. {
  16. em.processAllSystems();
  17. });
  18. }
  19. }
  20. function onMessage(msg:MessageHello, connection:Connection)
  21. {
  22. trace("Message: " + msg.txt);
  23. }
  24. function onConnection(connection:Connection)
  25. {
  26. var player = net.createEntity("player");
  27. net.bindEntity(connection, player);
  28. net.sendWorldStateTo(connection);
  29. var msg = new MessageHello();
  30. msg.txt = "youhou";
  31. net.sendEvent(msg);
  32. net.addComponent(player, new CHealer());
  33. }
  34. function onDisconnection(conn:Connection)
  35. {
  36. var boundEntity = net.getBoundEntity(conn);
  37. net.destroyEntity(boundEntity);
  38. }
  39. static function main() {new Server();}
  40. }