/// Should be connected via a HeavyGameEventListener /// Processes the initial listened to HeavyGameEvent public virtual void OnFilterReceiveAction(HeavyGameEventData data) { if (data.SourceCell.Selectable == this.parentUnit) { this.receiveAction(data); } }
/// Sets up the animations for the given action protected virtual void receiveAction(HeavyGameEventData data) { /// Will only do the setup if no animations are running from this UnitModel if (this.runningAnimation) { return; } float maxAnimationTime = -1.0f; for (int i = 0; i < this.unitActions.Length; i++) { /// Only appropriate UnitAnimations should be activated if (this.unitActions[i].TriggerActionType == data.ActionType) { maxAnimationTime = Math.Max(maxAnimationTime, this.unitActions[i].UnitAnimation.GetAnimationDuration(data)); UnitAnimation animation = Instantiate(this.unitActions[i].UnitAnimation) as UnitAnimation; animation.transform.SetParent(this.transform); animation.transform.localPosition = Vector3.zero; animation.SetupAnimation(data); this.presentAnimations.Add(animation); } } /// Only plays an animation if the time is > 0 if (maxAnimationTime > 0.0f) { StartCoroutine(this.runAnimation(maxAnimationTime)); } }
/// Connected to HeavyGameEventListener /// listens to attack events /// Should have a low Priority public void OnUnitAttackedEvent(HeavyGameEventData data) { if (data.ActionType == SelectableActionType.Attack && data.TargetCell?.Selectable != null && data.TargetCell.Selectable as Unit == this.unit) { this.updateHealth(); } }
public void EndMove() { HeavyGameEventData data = new HeavyGameEventData(); data.SourceCell = ParentCell; data.TargetCell = targetCell; onMoveEvent.Raise(data); moving = false; targetCell = null; this.onFinishRevolve(); }
public void OnPlanetMoveEvent(HeavyGameEventData _data) { if (_data.TargetCell.Selectable == null) { Selectable movingPlanet = _data.SourceCell.Selectable; _data.SourceCell.Selectable = null; movingPlanet.SetParentCell(_data.TargetCell); } else { if (_data.TargetCell.Selectable.selectableType == SelectableType.Unit) { Planet movingPlanet = (Planet)_data.SourceCell.Selectable; Unit sittingUnit = (Unit)_data.TargetCell.Selectable; GridCell newParentCell = null; int numberOfTries = 0; while (newParentCell == null && numberOfTries < movingPlanet.grid.GetGridSize().slices - 1) { int randSlice = UnityEngine.Random.Range(0, movingPlanet.grid.GetGridSize().slices - 1); GridCell potentialCell = movingPlanet.grid.GetGridCell(movingPlanet.grid.GetGridSize().layers - 2, randSlice); if (potentialCell.Selectable == null) { newParentCell = potentialCell; } numberOfTries++; if (numberOfTries == movingPlanet.grid.GetGridSize().slices) { //TODO: Do something with this throw new System.Exception("No free cells for move"); } } sittingUnit.SetParentCell(newParentCell); sittingUnit.transform.position = newParentCell.transform.position; _data.SourceCell.Selectable = null; movingPlanet.SetParentCell(_data.TargetCell); } else { throw new System.Exception("A planet has collided with something it shouldn't have."); } } }
public void OnUnitAttack(HeavyGameEventData data) { Unit attackingUnit = (Unit)data.SourceCell.Selectable; Unit defendingUnit = (Unit)data.TargetCell.Selectable; int damage = 0; //Close Range if (data.SourceCell.GetNeighbors().Contains(data.TargetCell)) { //TODO: Verify formula damage = attackingUnit.GetAttack().closeAttack - defendingUnit.GetDefense().closeDefense; } //Long Range else { //TODO: Verify formula damage = attackingUnit.GetAttack().longAttack - defendingUnit.GetDefense().longDefense; } defendingUnit.TakeDamage(damage); }
public override void SetupAnimation(HeavyGameEventData data) { this.rb = this.GetComponent <Rigidbody>(); this.transform.LookAt(data.SourceCell.transform.position); this.rb.velocity = Vector3.Normalize(data.TargetCell.transform.position - data.SourceCell.transform.position) * this.attackSpeed; }
/// Called on set up public virtual void SetupAnimation(HeavyGameEventData data) { }
/// Returns the duration of the animation /// Some animations should have dynamic animation times, which is why /// this might be overriden in sub classes public virtual float GetAnimationDuration(HeavyGameEventData data) { return(this.defaultAnimationTime); }
public void OnUnitMoveEvent(HeavyGameEventData _data) { Unit movingUnit = null; bool comingFromPlanet = false; //If the source cell has a selectable and the selectable is a unit, proceed as normal if (_data.SourceCell.Selectable != null && _data.SourceCell.Selectable.selectableType == SelectableType.Unit) { movingUnit = (Unit)_data.SourceCell.Selectable; comingFromPlanet = false; } //As of now, the only other possibility is that it's a unit exiting the gravity well of a planet else { movingUnit = (Unit)_data.targetSelectable; comingFromPlanet = true; } //We only want to set the source cell's selectable to null if it's not a planet, because the planet //will still be there when all is said and done. We set the planet gravity well's source cell to null elsewhere if (!comingFromPlanet) { _data.SourceCell.Selectable = null; } GridCell finalTarget = _data.TargetCell; //If moving to edge of planetary system if (!_data.TargetCell.parentGrid.isSolarSystem && _data.TargetCell.layer == _data.TargetCell.parentGrid.GetGridSize().layers - 1) { //Get current cardinal neighbor cells of planet in solar system (GridCell north, GridCell south, GridCell east, GridCell west)neighbors = GetDirectionalGridCells(_data.TargetCell.parentGrid.parentPlanet.ParentCell.GetNeighbors(), _data.TargetCell.parentGrid.parentPlanet.ParentCell.transform.position); //Use cardinal direction conversion to set final target Vector3 diffFromSource = (movingUnit.transform.position - _data.SourceCell.parentGrid.transform.position); if (Mathf.Abs(diffFromSource.z) >= Mathf.Abs(diffFromSource.x)) { if (diffFromSource.z >= 0) { finalTarget = neighbors.north; } else { finalTarget = neighbors.south; } } else { if (diffFromSource.x >= 0) { finalTarget = neighbors.east; } else { finalTarget = neighbors.west; } } if (finalTarget == null || (finalTarget.Selectable != null && finalTarget.Selectable.selectableType == SelectableType.Unit)) { finalTarget = _data.TargetCell.parentGrid.parentPlanet.ParentCell.GetNeighbors()[1]; } movingUnit.transform.position = _data.TargetCell.parentGrid.parentPlanet.ParentCell.transform.position; movingUnit.SetForceMove(_data.TargetCell.parentGrid.parentPlanet.ParentCell, finalTarget); return; } //Else, if we're moving onto a planet cell //(this is an else if because the previous circumstance should never be able to happen at the same time as this) else if (_data.TargetCell.Selectable != null && _data.TargetCell.Selectable.selectableType == SelectableType.Planet) { //Get current cardinal neighbor cells of planet in solar system (GridCell north, GridCell south, GridCell east, GridCell west)landings = GetLandingGridCells(((Planet)_data.TargetCell.Selectable).grid); //Find difference from sourceNode to targetnode Vector3 diffFromSource = (movingUnit.transform.position - _data.TargetCell.transform.position); if (Mathf.Abs(diffFromSource.z) >= Mathf.Abs(diffFromSource.x)) { if (diffFromSource.z >= 0) { finalTarget = FindLandingCell(landings.north); } else { finalTarget = FindLandingCell(landings.south); } } else { if (diffFromSource.x >= 0) { finalTarget = FindLandingCell(landings.east); } else { finalTarget = FindLandingCell(landings.west); } } } movingUnit.transform.position = finalTarget.transform.position; movingUnit.SetParentCell(finalTarget); }