private void onRightBtnClick(SidedObjectEntity entity) { if (entity is UnitBase) { if (this.selectedParty.contains((UnitBase)entity)) { this.selectedParty.remove((UnitBase)entity); } else { this.selectedBuilding.clearSelected(); this.selectedParty.tryAdd((UnitBase)entity); } } else if (entity is BuildingBase) { if (entity == this.selectedBuilding) { this.selectedBuilding.clearSelected(); } else { this.selectedParty.clearSelected(); this.selectedBuilding.setSelected((BuildingBase)entity); } } this.actionButtons.updateSideButtons(); }
protected override void preformTask() { if (this.trainingQueue.Count != 0) { bool teamHasRoom = this.getTeam().getTroopCount() <= this.getTeam().getMaxTroopCount(); if (teamHasRoom) { this.trainingProgress += Time.deltaTime; } UnitBase nextInQueue = this.trainingQueue[0].getPrefab().GetComponent <UnitBase>(); if (this.trainingProgress >= nextInQueue.getData().getProductionTime() && teamHasRoom) { Vector2 v = Random.insideUnitCircle * 2f; float i = 1.5f; v.x += (v.x < 0 ? -i : i); v.y += (v.x < 0 ? -i : i); Vector3 pos = this.transform.position + new Vector3(v.x, 0, v.y); RegisteredObject regObj = this.trainingQueue[0]; this.trainingQueue.RemoveAt(0); SidedObjectEntity obj = (SidedObjectEntity)this.map.spawnEntity(regObj, pos, Quaternion.Euler(0, Random.Range(0, 359), 0)); obj.setTeam(this.getTeam()); this.trainingProgress = 0; } } }
public virtual SidedObjectEntity attack(SidedObjectEntity target) { if (Time.time >= (this.lastAttack + Constants.AI_MELEE_ATTACK_RATE) && this.inRangeToAttack(target)) { this.preformAttack(target); this.lastAttack = Time.time; } return(target); }
public override void readFromNbt(NbtCompound tag) { base.readFromNbt(tag); this.damage = tag.getInt("damageDelt"); this.target = (LivingObject)this.map.findMapObjectFromGuid(tag.getGuid("target")); Guid guid = tag.getGuid("shooter"); this.shooter = (SidedObjectEntity)(guid != Guid.Empty ? this.map.findMapObjectFromGuid(guid) : null); }
/// <summary> /// Returns true if the action buttons should be disabled. /// </summary> public bool shouldDisable(SidedObjectEntity thisEntity) { if (this.shouldDisableFunction == null) { return(false); } else { return(this.shouldDisableFunction.Invoke(thisEntity)); } }
/// <summary> /// Attacks the target if they are in range, or continues to move if they are not in range. /// </summary> protected void func() { if (this.unit.attack.inRangeToAttack(this.target)) { this.target = this.unit.attack.attack(this.target); this.moveHelper.stop(); } else { this.moveHelper.setDestination(this.target); } }
protected virtual void preformAttack() { if (Util.isAlive(this.target) && Vector3.Distance(this.unit.getPos(), this.target.getPos()) <= Constants.AI_FIGHTING_FIND_RANGE) { // There is a target and it is close enough to "see". this.func(); } else { // Target is dead or out of range, find a new one. this.target = this.findTarget(); } }
private void onLeftBtnClick(SidedObjectEntity entity) { this.getSelected().clearSelected(); if (entity is UnitBase) { this.selectedParty.tryAdd((UnitBase)entity); } else if (entity is BuildingBase) { this.selectedBuilding.setSelected((BuildingBase)entity); } this.actionButtons.updateSideButtons(); }
public void callFunction <T>(List <T> list, SidedObjectEntity clickedObject) where T : SidedObjectEntity { List <SidedObjectEntity> candidates = this.getCandidates(list); if (this.entitySelecterFunctionDelayed == null) { // Call function on all. foreach (SidedObjectEntity entity in candidates) { this.mainFunctionDelayed.Invoke(entity, clickedObject); } } else { SidedObjectEntity e = this.entitySelecterFunctionDelayed.Invoke(candidates, clickedObject); this.mainFunctionDelayed.Invoke(e, clickedObject); } }
/// <summary> /// Calls the passed function on all of the passed SidedObjectEntities, or a specific /// one if this.entitySelecter is not null. /// </summary> public virtual void callFunction <T>(List <T> list) where T : SidedObjectEntity { List <SidedObjectEntity> candidates = this.getCandidates(list); if (this.entitySelecterFunction == null) { // Call function on all. foreach (SidedObjectEntity entity in candidates) { this.mainFunction.Invoke(entity); } } else { SidedObjectEntity e = this.entitySelecterFunction.Invoke(candidates); this.mainFunction.Invoke(e); } }
public override void onDamage(MapObject dealer) { if (dealer is Projectile) { SidedObjectEntity shooter = ((Projectile)dealer).getShooter(); if (this.unit.getTeam() != shooter.getTeam()) { this.target = shooter; } } else if (dealer is SidedObjectEntity) { SidedObjectEntity soe = (SidedObjectEntity)dealer; if (this.unit.getTeam() != soe.getTeam()) { this.target = soe; } } }
/// <summary> /// Returns the closest enemy object to the point, or null if there are none. /// </summary> protected T findEntityOfType <T>(Vector3 point, float maxDistance = -1, bool findEnemies = true) where T : SidedObjectEntity { SidedObjectEntity obj = null; float f = float.PositiveInfinity; Predicate <MapObject> predicate = (findEnemies ? this.unit.getTeam().predicateOtherTeam : this.unit.getTeam().predicateThisTeam); foreach (SidedObjectEntity s in this.unit.map.findMapObjects(predicate)) { if (s is T && !s.isDead()) { float dis = Vector3.Distance(point, s.transform.position); if ((dis < f) && (maxDistance == -1 || dis < maxDistance)) { obj = s; f = dis; } } } return((T)obj); }
/// <summary> /// Damages the passed object and returns it. Null will be returned if the object is destroyed. /// This method will also increase stats if needed. /// </summary> public SidedObjectEntity damageTarget(SidedObjectEntity obj) { int damage = this.unitStats.getAttack(); this.unitStats.damageDelt.increase(damage); if (obj.damage(this, damage)) { // obj was killed. if (obj is BuildingBase) { this.unitStats.buildingsDestroyed.increase(); } else if (obj is UnitBase) { this.unitStats.unitsKilled.increase(); } return(null); } else { return(obj); } }
/// <summary> /// Checks if this unit is in range to preform an attack on the passed target. /// </summary> public abstract bool inRangeToAttack(SidedObjectEntity target);
protected override void preformAttack(SidedObjectEntity target) { this.unit.damageTarget(target); }
/// <summary> /// Returns true if the target is in range. /// </summary> public override bool inRangeToAttack(SidedObjectEntity target) { float maxDistance = this.unit.getSizeRadius() + target.getSizeRadius() + 0.5f; return(Vector3.Distance(this.unit.getPos(), target.transform.position) <= maxDistance); }
private void handlePlayerInput() { bool leftBtnUp = Input.GetMouseButtonUp(0); bool rightBtnUp = Input.GetMouseButtonUp(1); this.selectionBox.updateRect(); if (leftBtnUp || rightBtnUp) { RaycastHit hit; if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, float.PositiveInfinity)) { SidedObjectEntity entity = hit.transform.gameObject.GetComponent <SidedObjectEntity>(); if (entity == null) { // Didn't click anything, move the party to the clicked point. if (leftBtnUp && hit.transform.CompareTag(Tags.ground)) { this.actionButtons.closePopupButtons(); this.selectedParty.moveAllTo(hit.point); } if (rightBtnUp) { this.actionButtons.closePopupButtons(); this.getSelected().clearSelected(); // Deselect all selected units. } } else { // Clicked an Entity if (this.actionButtons.delayedButtonRef != null) { ActionButtonRequireClick delayedButton = this.actionButtons.delayedButtonRef; // Check if this is a valid option to preform the action on. if (delayedButton.isValidForAction(this.team, entity)) { if (this.selectedBuilding.getBuilding() != null) { delayedButton.callFunction(this.selectedBuilding.getBuilding(), entity); } else { delayedButton.callFunction(this.selectedParty.getAllUnits(), entity); } } } else { if (entity.getTeam() == this.team) { // Clicked something on our team. if (leftBtnUp) { this.onLeftBtnClick(entity); } if (rightBtnUp) { this.onRightBtnClick(entity); } } } } // A click happened, so if something valid was clicked the delayed action was called. // Either way, we should cancel the action becuase it was resolved or the wrong thing was clicked. this.actionButtons.cancelDelayedAction(); } } }
// Overloads of parent class functions to provide the clicked argument. public void callFunction(BuildingBase building, SidedObjectEntity clicked) { this.mainFunctionDelayed.Invoke(building, clicked); }
public bool isValidForAction(Team team, SidedObjectEntity entity) { return(this.validForActionFunction(team, entity)); }
public void setProjectileInfo(SidedObjectEntity shooter, int damage, LivingObject target) { this.shooter = shooter; this.damage = damage; this.target = target; }
public override bool inRangeToAttack(SidedObjectEntity target) { return(Vector3.Distance(this.unit.getPos(), target.getPos()) <= Constants.AI_ARCHER_SHOOT_RANGE); }
public TaskAttackNearby(UnitBase unit) : base(unit) { this.target = this.findTarget(); }
protected override void preformAttack(SidedObjectEntity target) { Projectile arrow = (Projectile)this.unit.map.spawnEntity(Registry.projectileArrow, this.unit.transform.position + new Vector3(0, 1f, 0), Quaternion.identity); arrow.setProjectileInfo(this.unit, this.unit.unitStats.getAttack(), target); }
/// <summary> /// Attempts to preform an attack on the passed target. The attack will fail if /// <see cref="inRangeToAttack(SidedObjectEntity)"/> returns false or the /// attack cooldown has not finished. /// </summary> protected abstract void preformAttack(SidedObjectEntity target);
protected void placeBuilding(RegisteredObject obj, Vector3 pos) { SidedObjectEntity entity = (SidedObjectEntity)this.map.spawnEntity(obj, pos, Quaternion.identity); entity.setTeam(this.team); }
public static readonly ActionButton[] buttonList = new ActionButton[64]; // There doesn't seem to be any limit to the size. // Functions to use for this.setShouldDisableFunction() private static bool functionIsImmutable(SidedObjectEntity entity) { return(entity.isImmutable()); }