public void CreateShape(uint id, Config.ShapeConfig entityConfig) { switch (entityConfig.shape) { case Config.Shape.Circle: if (Instance.ShouldDisplayGraphics) { _particlePool.CreateParticle(id, entityConfig.initialPos, entityConfig.size, entityConfig.color); } break; default: Debug.LogError("Unhandled shape " + entityConfig.shape); break; } if (ShouldDisplayGraphics && ShouldDisplayEntityIDs) { var newText = Instantiate(text); newText.transform.SetParent(text.transform.parent, false); newText.transform.localScale = Vector3.one; newText.transform.position = entityConfig.initialPos; newText.transform.SetAsLastSibling(); Text t = newText.GetComponent <Text>(); t.text = id.ToString(); t.color = Color.white; allTexts[id] = t; } }
public void CreateShape(uint id, Config.ShapeConfig entityConfig) { if (_gameObjectsForDisplay.ContainsKey(id)) { DestroyShape(id); } GameObject instance; switch (entityConfig.shape) { case Config.Shape.Circle: instance = Instantiate(_circlePrefab); break; case Config.Shape.Square: instance = Instantiate(_squarePrefab); break; default: Debug.LogError("Unhandled shape " + entityConfig.shape); instance = new GameObject(); break; } instance.transform.localScale *= entityConfig.size; instance.GetComponent <SpriteRenderer>().color = Color.HSVToRGB(id / 10.0f, 1, 1); // assumes there's just 10 shapes at the same time _gameObjectsForDisplay[id] = instance; }
public void UpdateSystem() { if (world.Positions.Count > 0) { return; } for (uint i = 0; i < ecs.Config.allShapesToSpawn.Count; i++) { Config.ShapeConfig shape = ecs.Config.allShapesToSpawn[(int)i]; ecs.CreateShape(i, ecs.Config.allShapesToSpawn[(int)i]); ecs.UpdateShapePosition(i, shape.initialPos); world.Positions.Add(new PositionComponent(i, shape.initialPos)); world.Sizes.Add(new SizeComponent(i, shape.size)); world.Speeds.Add(new SpeedComponent(i, shape.initialSpeed)); world.Static.Add(isModulo4(i + 1) ? new StaticComponent(i, true) : new StaticComponent(i, false)); world.CollisionWithEdges.Add(new CollisionWithEdgesComponent(i, false)); world.CollisionsWithEntities.Add(new CollisionWithEntityComponent(i, false)); world.Colliders.Add(new ColliderComponent(i, true)); world.Colors.Add(new ColorComponent(i, Color.white)); world.PastPositions.Add(new PastPositionComponent(i)); world.PastSpeeds.Add(new PastSpeedComponent(i)); world.PastColors.Add(new PastColorComponent(i)); world.PastSizes.Add(new PastSizeComponent(i)); world.PastColliders.Add(new PastColliderComponent(i)); } }
public void CreateShape(uint id, Config.ShapeConfig entityConfig) { GameObject instance = Instantiate(_circlePrefab); instance.transform.localScale *= entityConfig.size; _gameObjectsForDisplay[id] = instance; }
public static void SpawnEntity(uint entityId, Vector2 speed, Config.ShapeConfig entityConfig) { ECSManager.Instance.CreateShape(entityId, entityConfig); ShapeComponent shapeData = new ShapeComponent(); shapeData.pos = entityConfig.initialPos; shapeData.speed = speed; shapeData.shape = entityConfig.shape; shapeData.size = entityConfig.size; ComponentsManager.Instance.SetComponent <ShapeComponent>(entityId, shapeData); ComponentsManager.Instance.SetComponent <EntityComponent>(entityId, new EntityComponent(entityId)); }
public void SpawnPlayerEntity(uint playerId, ShapeComponent shapeData) { var entityConfig = new Config.ShapeConfig(); entityConfig.shape = Config.Shape.Circle; entityConfig.size = shapeData.size; entityConfig.initialPos = shapeData.pos; SpawnEntity(playerId, new Vector2(), entityConfig); PlayerComponent playerComponent = new PlayerComponent() { playerId = playerId }; ComponentsManager.Instance.SetComponent <PlayerComponent>(playerId, playerComponent); }
private static void SpawnEntity(uint entityId, Vector2 speed, Config.ShapeConfig entityConfig, bool isStatic) { ECSManager.Instance.CreateShape(entityId, entityConfig); ComponentsManager.Instance.SetComponent <ColliderComponent>(entityId, new ColliderComponent()); ComponentsManager.Instance.SetComponent <PositionComponent>(entityId, new PositionComponent(entityConfig.initialPos)); ComponentsManager.Instance.SetComponent <SizeComponent>(entityId, new SizeComponent(entityConfig.size)); ComponentsManager.Instance.SetComponent <EntityComponent>(entityId, new EntityComponent(entityId)); if (!isStatic) { SpeedComponent speedData = new SpeedComponent { speed = speed }; ComponentsManager.Instance.SetComponent <SpeedComponent>(entityId, speedData); } }
public void UpdateSystem() { bool spawnFound = ComponentsManager.Instance.TryGetComponent(new EntityComponent(0), out SpawnInfo spawn); if (!spawnFound || !spawn.spawnDone) { ComponentsManager.Instance.ClearComponents <CollisionEventComponent>(); Vector2 screenBorderPos = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height)); uint currentID = 0; int staticCounter = 0; for (int i = 0; i < ECSManager.Instance.Config.numberOfShapesToSpawn; i++) { var randomPos = new Vector2(UnityEngine.Random.Range(-screenBorderPos.x, screenBorderPos.x), UnityEngine.Random.Range(-screenBorderPos.y, screenBorderPos.y)); var entity = new Config.ShapeConfig() { initialPos = randomPos, size = UnityEngine.Random.Range(ECSManager.Instance.Config.MinSize, ECSManager.Instance.Config.MaxSize), color = Color.red, }; bool isStatic = false; if (staticCounter == 4) { staticCounter = 0; isStatic = true; entity.color = NiceColors.NiceRed; ComponentsManager.Instance.SetComponent <ColorComponent>(currentID, new ColorComponent(entity.color)); } else { staticCounter += 1; } SpawnEntity(currentID, new Vector2(UnityEngine.Random.Range(-5.0f, 5.0f), UnityEngine.Random.Range(-5.0f, 5.0f)), entity, isStatic); currentID += 1; } ECSManager.Instance.InitDisplay(); var spawnInfo = new SpawnInfo { spawnDone = true }; ComponentsManager.Instance.SetComponent <SpawnInfo>(new EntityComponent(), spawnInfo); } }
public void UpdateSystem() { if (ECSManager.Instance.NetworkManager.isServer) { //Only server spawns shapes from config bool spawnFound = ComponentsManager.Instance.TryGetComponent(new EntityComponent(0), out SpawnInfo spawnInfo); if (!spawnFound || !spawnInfo.spawnDone) { uint currentID = 1; float minSpeed = 2.0f; float maxSpeed = 5.0f; foreach (var entity in ECSManager.Instance.Config.allShapesToSpawn) { float xDir = UnityEngine.Random.value > 0.5 ? 1 : -1; float yDir = UnityEngine.Random.value > 0.5 ? 1 : -1; SpawnEntity(currentID++, new Vector2(xDir * UnityEngine.Random.Range(minSpeed, maxSpeed), yDir * UnityEngine.Random.Range(minSpeed, maxSpeed)), entity); } if (!spawnFound) { spawnInfo = new SpawnInfo(true); } ComponentsManager.Instance.SetComponent <SpawnInfo>(new EntityComponent(0), spawnInfo); } for (int i = 0; i < spawnInfo.playersToSpawn.Count; i++) { uint playerId = spawnInfo.playersToSpawn[i]; bool shapeSpawned = ComponentsManager.Instance.TryGetComponent(playerId, out ShapeComponent shapeComponent); if (shapeSpawned) { SpawnPlayerEntity(playerId, shapeComponent); spawnInfo.playersToSpawn.RemoveAt(i); i--; } } ComponentsManager.Instance.SetComponent <SpawnInfo>(new EntityComponent(0), spawnInfo); } else if (ECSManager.Instance.NetworkManager.isClient) { uint clientId = (uint)ECSManager.Instance.NetworkManager.LocalClientId; bool playerSpawned = ComponentsManager.Instance.TryGetComponent(clientId, out PlayerComponent playerComponent); if (!playerSpawned) { bool shapeSpawned = ComponentsManager.Instance.TryGetComponent(clientId, out ShapeComponent shapeComponent); if (shapeSpawned) { SpawnPlayerEntity(clientId, shapeComponent); } } bool spawnFound = ComponentsManager.Instance.TryGetComponent(new EntityComponent(0), out SpawnInfo spawnInfo); if (spawnFound) { foreach (var msgReplication in spawnInfo.replicatedEntitiesToSpawn) { var entityConfig = new Config.ShapeConfig(); entityConfig.shape = msgReplication.shape; entityConfig.size = msgReplication.size; entityConfig.initialPos = msgReplication.pos; // spawn other players as player entities in other to handle collisions during extrapolation if (msgReplication.shape == Config.Shape.Circle && !(msgReplication.entityId == clientId)) { ShapeComponent shapeComponent; if (!ComponentsManager.Instance.TryGetComponent(msgReplication.entityId, out shapeComponent)) { shapeComponent = new ShapeComponent(); shapeComponent.pos = entityConfig.initialPos; shapeComponent.speed = msgReplication.speed; shapeComponent.shape = entityConfig.shape; shapeComponent.size = entityConfig.size; ComponentsManager.Instance.SetComponent <ShapeComponent>(msgReplication.entityId, shapeComponent); } SpawnPlayerEntity(msgReplication.entityId, shapeComponent); } else { SpawnEntity(msgReplication.entityId, msgReplication.speed, entityConfig); } } spawnInfo.replicatedEntitiesToSpawn.Clear(); ComponentsManager.Instance.SetComponent <SpawnInfo>(new EntityComponent(0), spawnInfo); } } }