private void SetupPlayers(GameContext game, Presets preset, HexGridBehaviour grid) { PlayerListData pld = GameObject.FindObjectOfType <PlayerListData>(); if (pld != null) { IEnumerator <Vector3> basePos = CoordinatesForBases(grid); foreach (var player in pld._model) { GameEntity pEntity = game.CreateEntity(); pEntity.AddTeam(player.Color); pEntity.isLocalPlayer = (player.Type == PlayerTypeEnum.Human); pEntity.isAIPlayer = (player.Type == PlayerTypeEnum.AI); basePos.MoveNext(); GameEntity baseEntity = preset.CreateBlueprint(Presets.EntitasPresetEnum.BASE); HexCellBehaviour home = grid.GetCell(basePos.Current); baseEntity.ReplaceLocation(home, home.GetComponent <EntitasLink>().id); baseEntity.ReplaceTeam(player.Color); baseEntity.ReplaceStartPosition(home.transform.position.x, home.transform.position.y, home.transform.position.z); } } else { Debug.Log("No player setup found; I'm assuming this scene was started in Unity Editor for debugging purposes"); GameEntity pEntity = game.CreateEntity(); pEntity.AddTeam(0); pEntity.isLocalPlayer = true; pEntity.isAIPlayer = false; } }
private TreeStatusEnum BuildBarracks(GameEntity playa) { int team = playa.team.value; GameEntity home = _allPlayers.BaseForTeam(team); if (home == null) { return(TreeStatusEnum.FAILURE); //no home base => we are dead } List <HexCellBehaviour> hood = _grid.GetWithinRange(3, home.location.cell.cubeCoordinates); HexCellBehaviour buildLoc = null; while (hood.Count > 0) { //chose random cell int chosen = UnityEngine.Random.Range(0, hood.Count); buildLoc = hood[chosen]; int cellid = buildLoc.GetComponent <EntitasLink>().id; //cell is empty? if (_game.GetEntitiesWithLocation(cellid).Count == 0) { } else { hood.RemoveAt(chosen); } } return(TreeStatusEnum.FAILURE); }
protected override void Execute(List <GameEntity> entities) { foreach (var unit in entities) { if (!unit.hasNavigationTarget) { if (unit.hasNavigationPath) { unit.RemoveNavigationPath(); } unit.isNavigationBlocked = false; } else { HexCellBehaviour fromCell = unit.location.cell; GameEntity toCellEntity = _game.GetEntityWithID(unit.navigationTarget.targetCellID); HexCellBehaviour toCell = toCellEntity.gameObject.value.GetComponent <HexCellBehaviour>(); Stack <HexCellBehaviour> p = _grid.FindPath(fromCell, toCell); p.Pop(); //remove first cell = current cell unit.ReplaceNavigationPath(p); } } }
public void ReplaceLocation(HexCellBehaviour newCell, int newCellid) { var index = GameComponentsLookup.Location; var component = CreateComponent <LocationComponent>(index); component.cell = newCell; component.cellid = newCellid; ReplaceComponent(index, component); }
public void AddLeaveCell(HexCellBehaviour newCell, int newCellid) { var index = GameComponentsLookup.LeaveCell; var component = CreateComponent <LeaveCell>(index); component.cell = newCell; component.cellid = newCellid; AddComponent(index, component); }
private Vector3 CalcVector3(WorldCoordinatesComponent coord, HexCellBehaviour to) { Vector3 res = new Vector3( to.transform.position.x - coord.x, to.transform.position.y - coord.y, to.transform.position.z - coord.z ).normalized; return(res); }
private void CreateCell(int x, int y, int i) { Vector3 position; position.x = (x + y * cellDistance - y / 2) * (HexMetrics.innerRadius * 2f); position.y = y * -cellDistance; position.z = 0f; HexCellBehaviour cell = cells[i] = Instantiate(cellPrefab); cell.transform.SetParent(transform, false); cell.transform.localPosition = position; }
protected override void Execute(List <GameEntity> entities) { foreach (var unit in entities) { HexCellBehaviour blockedCell = unit.navigationPath.path.Pop(); GameEntity cell = _game.GetEntityWithID(unit.navigationTarget.targetCellID); HexCellBehaviour targetCell = cell.gameObject.value.GetComponent <HexCellBehaviour>(); if (blockedCell == targetCell) //can't get closer { unit.RemoveNavigationTarget(); } else { //force calculation of new path with current traverse costs: unit.ReplaceNavigationTarget(cell.iD.value); unit.isNavigationBlocked = false; } } }
private void TurnGridToEntities(GameContext game, HexGridBehaviour grid) { for (var c = grid.GetCellEnumerator(); c.MoveNext();) { HexCellBehaviour cell = c.Current; GameEntity ge = game.CreateEntity(); Vector3 world = cell.transform.position; Vector3 cube = cell.cubeCoordinates; Quaternion rot = cell.transform.rotation; ge.AddHexCell(world.x, world.y, world.z, cube.x, cube.y, cube.z); ge.AddGameObject(cell.gameObject); ge.AddWorldCoordinates(world.x, world.y, world.z, rot.x, rot.y, rot.z, rot.w); //now that the entity has been created with a unique ID, //put this ID on the unity GameObject for easy reference: EntitasLink el = cell.gameObject.AddComponent <EntitasLink>(); el.id = ge.iD.value; } }
private GameEntity CreateCommon(EntitasInit unityObject) { GameEntity ge = _game.CreateEntity(); ge.AddGameObject(unityObject.gameObject); Vector3 pos = unityObject.transform.position; Quaternion rot = unityObject.transform.rotation; ge.AddWorldCoordinates(pos.x, pos.y, pos.z, rot.x, rot.y, rot.z, rot.w); HexCellBehaviour cell = _grid.GetCell(_grid.axial_to_cube(_grid.pixel_to_axial(unityObject.transform.position))); if (cell != null) { int id = cell.GetComponent <EntitasLink>().id; ge.AddLocation(cell, id); } HexSelectable selectable = unityObject.GetComponent <HexSelectable>(); if (selectable != null) { ge.isSelectable = true; } TeamColor team = unityObject.GetComponent <TeamColor>(); if (team != null) { ge.AddTeam(team.teamNr); } //now that the entity has been created with a unique ID, //put this ID on the unity GameObject for easy reference: EntitasLink el = unityObject.gameObject.AddComponent <EntitasLink>(); el.id = ge.iD.value; return(ge); }
public void Execute() { foreach (var unit in _navigatingUnits) { HexCellBehaviour curCell = unit.location.cell; Stack <HexCellBehaviour> path = unit.navigationPath.path; if (unit.isNavigationBlocked) { continue; //skip blocked units } if (path.Count < 1) { unit.RemoveNavigationTarget(); unit.RemoveNavigationPath(); continue; } //four possibilities: //1. we need to rotate before moving towards the next cell //2. we're moving to the next cell but still in our "own" cell //3. we're already in the next cell but still moving to the center //4. we've arrived at center of the cell (and this is possibly the last move) //the code checks above in the opposite order: float todist = CalcSqrDistance(unit.worldCoordinates, path.Peek()); if (todist < CLOSE_ENOUGH) { //4. we've arrived at center of the cell path.Pop(); if (path.Count > 0) { CheckForObstructions(unit, path); unit.ReplaceNavigationPath(path); } else { unit.RemoveNavigationTarget(); } continue; } float fromdist = CalcSqrDistance(unit.worldCoordinates, curCell); if (todist < fromdist) { //3. we're closer to next cell but still moving to center if (unit.hasLocation) { unit.ReplaceLeaveCell(unit.location.cell, unit.location.cellid); } unit.ReplaceLocation(path.Peek(), path.Peek().GetComponent <EntitasLink>().id); //no break, we need to add movement vector } Vector3 dir = CalcVector3(unit.worldCoordinates, path.Peek()); float roty = CalcRotationY(unit.worldCoordinates, dir, unit.navigable.turnRate); dir *= unit.navigable.moveRate * Time.deltaTime; if (Mathf.Abs(roty) < CLOSE_ENOUGH) { //2.we're pointed at next cell but still in our own unit.ReplaceMove(dir.x, dir.z, roty); continue; } //1. not oriented towards destination cell; unit should only rotate and not move unit.ReplaceMove(0, 0, roty); } }
private float CalcSqrDistance(WorldCoordinatesComponent coord, HexCellBehaviour to) { return((new Vector3(coord.x, coord.y, coord.z) - to.transform.position).sqrMagnitude); }