/// <summary> /// Sets the new target building /// </summary> /// <param name="building"> Target building </param> void setTargetBuilding(BuildingEntity building) { building.OnDeath += (a, b) => { if (targetBuilding != null) { targetBuilding.Delete(); } hasTarget = attacking = false; }; targetBuilding = building; }
/// <summary> /// Update enemy logic /// </summary> public override void Update(GameTime gameTime) { // update animations updateAnimations(gameTime); float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; // if the enemy doesn't have a target and isn't entering, // find a new target building if (!isEntering && !hasTarget) { aquireNewTargetBuilding(); } float speed = delta * walkSpeed; // follow target path if the enemy isn't attacking if (!attacking && path.Count > 1) { // get the position of our current target var target = world.Grid[path[0]].Position + properties.TargetNodeOffset; // set our enemy walking currentAnimation = "Walk"; shadowAnimation = "ShadowWalk"; if (Vector2.Distance(target, position) <= speed) { // we aren't entering anymore isEntering = false; path.RemoveAt(0); // new node var node = world.Grid[path[0]]; // new target position var newTarget = node.Position + properties.TargetNodeOffset; if (node.Occupant != null && node.Occupant.BuildingType == Building.Barricade) { var tmpBuilding = targetBuilding; targetBuilding = node.Occupant; attacking = true; targetBuilding.OnDeath += delegate { if (targetBuilding != null) { targetBuilding.Delete(); } targetBuilding = tmpBuilding; attacking = false; hasTarget = true; setTargetPosition(newTarget); }; } else { // set the new target position setTargetPosition(newTarget); } // reached the destination, start attacking if (path.Count == 1) { attacking = true; // set our idle animation currentAnimation = "Idle"; shadowAnimation = "ShadowIdle"; // set our idle animation frame to the frame // prescribed by the building attack node setIdleFrame(destinationFrame); } } } else if (attacking) { lastAttacked -= gameTime.ElapsedGameTime; if (lastAttacked <= TimeSpan.Zero) { lastAttacked = attackInterval; // attack the target building, we've reached our attack // interval attackTargetBuilding(); } // make sure we're idle velocity = Vector2.Zero; } // move the enemy based on our velocity position += velocity * speed; // update enemy bullet logic checkBulletCollision(); }