public override void HandleEvent(GameTime gameTime) { EventManager.FindMessagesByDestination(this, messages); FindDoubledMessages(); //for (int i = 0; i < 0; i++) if (messages.Count > 0) { int i = 0; if (!messages[i].Done) switch (messages[i].Type) { #region HandleEvent.Selected/Unselected case (int)EventManager.Events.Selected: selected = true; messages[i].Done = true; GUIEventManager.CreateMessage(new Message((int)GUIEventManager.Events.GuiUP, this, null, null)); break; case (int)EventManager.Events.Unselected: selected = false; messages[i].Done = true; GUIEventManager.CreateMessage(new Message((int)GUIEventManager.Events.GuiDOWN, this, null, null)); break; #endregion #region HandleEvent.Interaction case (int)EventManager.Events.Interaction: Console.WriteLine("Jednostka - interakcja z" + messages[i].Destination.ToString()); EventManager.CreateMessage(new Message((int)EventManager.Events.Interaction, this, messages[i].Payload, null)); messages[i].Done = true; break; #endregion #region HandleEvent.MoveUnit case (int)EventManager.Events.MoveUnit: //////nowa wersja//////// if (destinyPoints == null) { setWalk(); Laikos.PathFiding.Wspolrzedne wspBegin = new Laikos.PathFiding.Wspolrzedne((int)this.Position.X, (int)this.Position.Z); Laikos.PathFiding.Wspolrzedne wspEnd = new Laikos.PathFiding.Wspolrzedne((int)((Vector3)messages[i].Payload).X, (int)(((Vector3)messages[i].Payload).Z)); // Console.WriteLine("A: " + wspBegin.X + ", " + wspBegin.Y + ", " + Map.WalkMeshMap[wspBegin.X / Map.SKALA, wspBegin.Y / Map.SKALA] // + " B: " + wspEnd.X + ", " + wspEnd.Y + ", " + Map.WalkMeshMap[wspEnd.X / Map.SKALA, wspEnd.Y / Map.SKALA]); this.destinyPoints = this.pathFiding.obliczSciezke(wspBegin, wspEnd); this.destinyPointer = null; } /////////nowa wersja/////////// if (destinyPoints.Count > 0) { if ((destinyPoints != null) && (destinyPoints.Count > 0) && (destinyPointer == null)) { destinyPointer = destinyPoints.GetEnumerator(); destinyPointer.MoveNext(); Vector3 vecTmp = new Vector3(destinyPointer.Current.X, 0.0f, destinyPointer.Current.Y); direction = vecTmp - Position; } if ( (!Double.IsNaN(direction.X)) && (!Double.IsNaN(direction.Y)) && (!Double.IsNaN(direction.Z)) ) { direction.Normalize(); PositionOld = Position; Position.X += direction.X * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 50.0f; Position.Z += direction.Z * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 50.0f; ChangeDirection(); } if ((destinyPointer != null) && (Math.Abs(Position.X - destinyPointer.Current.X) < 0.5f) && (Math.Abs(Position.Z - destinyPointer.Current.Y) < 0.5f)) { // Next step walk. if (!destinyPointer.MoveNext()) { EndMove(messages[i]); setIdle(); // Console.WriteLine("done"); } else { Vector3 vecTmp = new Vector3(destinyPointer.Current.X, 0.0f, destinyPointer.Current.Y); direction = vecTmp - Position; } } } else { messages[i].Done = true; } foreach (Message _msg in messages) { if ( (!_msg.Done) && (_msg.Type == (int)EventManager.Events.FixCollisions) ) { _msg.Done = true; } } break; #endregion #region HandleEvent.MoveToBuild case (int)EventManager.Events.MoveToBuild: if (EventManager.MessageToOld(gameTime, messages[i], 4000)) { messages[i].Done = true; break; } if (budowniczy == true) { //////nowa wersja//////// if (destinyPoints == null) { /* Vector3 stay_here = new Vector3(((WhereToBuild)messages[i].Payload).position.X, ((WhereToBuild)messages[i].Payload).position.Y, ((WhereToBuild)messages[i].Payload).position.Z); Building temp = new Building(game, player, ((WhereToBuild)messages[i].Payload).building.type, Vector3.Zero, ((WhereToBuild)messages[i].Payload).building.type.Scale, false); if (MathUtils.RandomNumber(1, 2) == 1) //czy x czy Z { if (MathUtils.RandomNumber(1, 2) == 1) // czy + czy - stay_here.X += BoundingSphere.CreateFromBoundingBox(temp.boundingBox).Radius / 4 * 3; else stay_here.X -= BoundingSphere.CreateFromBoundingBox(temp.boundingBox).Radius / 4 * 3; stay_here.Z = MathUtils.RandomNumber((int)(stay_here.Z - BoundingSphere.CreateFromBoundingBox(temp.boundingBox).Radius / 4 * 3), (int)(stay_here.Z + BoundingSphere.CreateFromBoundingBox(temp.boundingBox).Radius / 4 * 3)); } else { if (MathUtils.RandomNumber(1, 2) == 1) // czy + czy - stay_here.Z += BoundingSphere.CreateFromBoundingBox(temp.boundingBox).Radius / 4 * 3; else stay_here.Z -= BoundingSphere.CreateFromBoundingBox(temp.boundingBox).Radius / 4 * 3; stay_here.X = MathUtils.RandomNumber((int)(stay_here.X - BoundingSphere.CreateFromBoundingBox(temp.boundingBox).Radius / 4 * 3), (int)(stay_here.X + BoundingSphere.CreateFromBoundingBox(temp.boundingBox).Radius / 4 * 3)); }*/ setWalk(); Laikos.PathFiding.Wspolrzedne wspBegin = new Laikos.PathFiding.Wspolrzedne((int)this.Position.X, (int)this.Position.Z); Laikos.PathFiding.Wspolrzedne wspEnd = new Laikos.PathFiding.Wspolrzedne((int)((WhereToBuild)messages[i].Payload).position.X, (int)(((WhereToBuild)messages[i].Payload).position.Z)); //Laikos.PathFiding.Wspolrzedne wspEnd = new Laikos.PathFiding.Wspolrzedne((int)stay_here.X, (int)stay_here.Z); this.destinyPoints = this.pathFiding.obliczSciezke(wspBegin, wspEnd); this.destinyPointer = null; } /////////nowa wersja/////////// if (destinyPoints.Count > 0) { if ((destinyPoints != null) && (destinyPoints.Count > 0) && (destinyPointer == null)) { destinyPointer = destinyPoints.GetEnumerator(); destinyPointer.MoveNext(); Vector3 vecTmp = new Vector3(destinyPointer.Current.X, 0.0f, destinyPointer.Current.Y); direction = vecTmp - Position; } if ( (!Double.IsNaN(direction.X)) && (!Double.IsNaN(direction.Y)) && (!Double.IsNaN(direction.Z)) ) { direction.Normalize(); PositionOld = Position; Position.X += direction.X * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 50.0f; Position.Z += direction.Z * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 50.0f; ChangeDirection(); } if ((destinyPointer != null) && (Math.Abs(Position.X - destinyPointer.Current.X) < 12.0f) && (Math.Abs(Position.Z - destinyPointer.Current.Y) < 12.0f)) { // Next step walk. if (!destinyPointer.MoveNext()) { destinyPoints = null; destinyPointer = null; direction.X = 0.0f; direction.Z = 0.0f; messages[i].Done = true; EventManager.CreateMessage(new Message((int)EventManager.Events.Build, this, this, messages[i].Payload)); timeSpan = TimeSpan.FromMilliseconds(((WhereToBuild)messages[i].Payload).building.buildtime); setAttack(); } else { Vector3 vecTmp = new Vector3(destinyPointer.Current.X, 0.0f, destinyPointer.Current.Y); direction = vecTmp - Position; } } } else { messages[i].Done = true; } } messages[i].timer = gameTime.TotalGameTime; break; #endregion #region HandleEvent.Build case (int)EventManager.Events.Build: if (EventManager.MessageToOld(gameTime, messages[i], 100)) { messages[i].Done = true; break; } if (budowniczy == true) { timeSpan -= gameTime.ElapsedGameTime; if (timeSpan < TimeSpan.Zero) { player.Build(((WhereToBuild)messages[i].Payload).building.type, ((WhereToBuild)messages[i].Payload).position); messages[i].Done = true; setIdle(); } } messages[i].timer = gameTime.TotalGameTime; break; #endregion #region HandleEvent.Gathering case (int)EventManager.Events.Gather: ///////BREAK MESSAGE if (EventManager.MessageToOld(gameTime, messages[i], 3000)) { messages[i].Done = true; break; } ///////////////////// if (budowniczy == true) { //////////MOVE if (destinyPoints == null) { setWalk(); Laikos.PathFiding.Wspolrzedne wspBegin = new Laikos.PathFiding.Wspolrzedne((int)this.Position.X, (int)this.Position.Z); Laikos.PathFiding.Wspolrzedne wspEnd = new Laikos.PathFiding.Wspolrzedne((int)((Building)messages[i].Sender).Position.X, (int)((Building)messages[i].Sender).Position.Z); this.destinyPoints = this.pathFiding.obliczSciezke(wspBegin, wspEnd); this.destinyPointer = null; } if (this.destinyPoints.Count > 0) { if ((destinyPoints != null) && (destinyPoints.Count > 0) && (destinyPointer == null)) { destinyPointer = destinyPoints.GetEnumerator(); destinyPointer.MoveNext(); Vector3 vecTmp = new Vector3(destinyPointer.Current.X, 0.0f, destinyPointer.Current.Y); direction = vecTmp - Position; } direction.Normalize(); PositionOld = Position; Position.X += direction.X * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 50.0f; Position.Z += direction.Z * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 50.0f; ChangeDirection(); if ((destinyPointer != null) && (Math.Abs(Position.X - destinyPointer.Current.X) < 0.5f) && (Math.Abs(Position.Z - destinyPointer.Current.Y) < 0.5f)) { //JESTES U CELU if (!destinyPointer.MoveNext()) { destinyPoints = null; destinyPointer = null; direction.X = 0.0f; direction.Z = 0.0f; setAttack(); //OBSŁUGA ZBIERANIA timeSpan -= gameTime.ElapsedGameTime; if (((Building)messages[i].Sender).Souls > 0) //czy budynek ma narobione dusze { if (this.Souls_owned < Souls_cap) //czy mamy miejsce { //if (timeSpan < TimeSpan.Zero) //jesli czas pakowania minal dodajemy dusze //{ if (Souls_cap - this.Souls_owned >= ((Building)messages[i].Sender).Souls) //ilosc miejsca wieksza niz ilosc dusz w budynku { this.Souls_owned += ((Building)messages[i].Sender).Souls; //dodajemy wszystkie dusze z budynku ((Building)messages[i].Sender).Souls = 0; } else //ilosc miejsca mniejsza niz ilosc dusz w budynku { ((Building)messages[i].Sender).Souls -= Souls_cap - this.Souls_owned; //dobieramy tyle ile sie da this.Souls_owned += Souls_cap - this.Souls_owned; } foreach (Building building in player.BuildingList) { if (building.type.Name.Contains("Pałac rady")) //odsylamy do skladowania { EventManager.CreateMessage(new Message((int)EventManager.Events.Store, messages[i].Sender, this, building)); messages[i].Done = true; break; } } //} } else //jak nie mamy miejsca to odnosimy { foreach (Building building in player.BuildingList) { if (building.type.Name.Contains("Pałac rady")) //odsylamy do skladowania { setWalk(); EventManager.CreateMessage(new Message((int)EventManager.Events.Store, this, this, building)); messages[i].Done = true; break; } } } } else //jak nie ma dusz to koniec zadania { messages[i].Done = true; break; } } else { Vector3 vecTmp = new Vector3(destinyPointer.Current.X, 0.0f, destinyPointer.Current.Y); direction = vecTmp - Position; } } setWalk(); } else { messages[i].Done = true; } } messages[i].timer = gameTime.TotalGameTime; break; #endregion #region HandleEvent.Storing case (int)EventManager.Events.Store: ///////BREAK MESSAGE if (EventManager.MessageToOld(gameTime, messages[i], 3000)) { messages[i].Done = true; break; } ///////////////////// if (budowniczy == true) { //////////MOVE if (destinyPoints == null) { setWalk(); Laikos.PathFiding.Wspolrzedne wspBegin = new Laikos.PathFiding.Wspolrzedne((int)this.Position.X, (int)this.Position.Z); Laikos.PathFiding.Wspolrzedne wspEnd = new Laikos.PathFiding.Wspolrzedne((int)((Building)messages[i].Payload).Position.X, (int)((Building)messages[i].Payload).Position.Z); this.destinyPoints = this.pathFiding.obliczSciezke(wspBegin, wspEnd); this.destinyPointer = null; } if (this.destinyPoints.Count > 0) { if ((destinyPoints != null) && (destinyPoints.Count > 0) && (destinyPointer == null)) { destinyPointer = destinyPoints.GetEnumerator(); destinyPointer.MoveNext(); Vector3 vecTmp = new Vector3(destinyPointer.Current.X, 0.0f, destinyPointer.Current.Y); direction = vecTmp - Position; } PositionOld = Position; Position.X += direction.X * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 50.0f; Position.Z += direction.Z * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 50.0f; // ChangeDirection(); if ((destinyPointer != null) && (Math.Abs(Position.X - destinyPointer.Current.X) < 0.5f) && (Math.Abs(Position.Z - destinyPointer.Current.Y) < 0.5f)) { // Next step walk. if (!destinyPointer.MoveNext()) { EndMove(messages[i]); player.Souls += this.Souls_owned; this.Souls_owned = 0; setWalk(); EventManager.CreateMessage(new Message((int)EventManager.Events.Gather, messages[i].Sender, this, null)); timeSpan = TimeSpan.FromMilliseconds(3000); //Console.WriteLine("done"); } else { Vector3 vecTmp = new Vector3(destinyPointer.Current.X, 0.0f, destinyPointer.Current.Y); direction = vecTmp - Position; } } setWalk(); } else { messages[i].Done = true; } } messages[i].timer = gameTime.TotalGameTime; break; #endregion #region Move To Attack case (int)EventManager.Events.MoveToAttack: if (range <= 0) { messages[i].Done = true; break; } GameObject targetMoveAttack = (GameObject)messages[i].Payload; if (destinyPoints == null) { setWalk(); Laikos.PathFiding.Wspolrzedne wspBegin = new Laikos.PathFiding.Wspolrzedne((int)this.Position.X, (int)this.Position.Z); Laikos.PathFiding.Wspolrzedne wspEnd = new Laikos.PathFiding.Wspolrzedne((int)(targetMoveAttack.Position.X), (int)(targetMoveAttack.Position.Z)); this.destinyPoints = this.pathFiding.obliczSciezke(wspBegin, wspEnd); this.destinyPointer = null; } if (messages[i].Payload is Unit) { destinyUnit = (Unit)messages[i].Payload; destinyBuilding = null; } else if (messages[i].Payload is Building) { destinyUnit = null; destinyBuilding = (Building)messages[i].Payload; } if ((destinyPoints != null) && (destinyPoints.Count > 0)) { if ( ( (destinyUnit != null) && (Math.Abs(Position.X - destinyUnit.Position.X) < range) ) || ( (destinyBuilding != null) && (Math.Abs(Position.Z - destinyBuilding.Position.Z) < range) ) ) { EndMove(messages[i]); setAttack(); EventManager.CreateMessage(new Message((int)EventManager.Events.Attack, null, this, messages[i].Payload)); break; } if ((destinyPointer == null) && (destinyPoints != null) && (destinyPoints.Count > 0)) { destinyPointer = destinyPoints.GetEnumerator(); destinyPointer.MoveNext(); Vector3 vecTmp = new Vector3(destinyPointer.Current.X, 0.0f, destinyPointer.Current.Y); direction = vecTmp - Position; } if ( (!Double.IsNaN(direction.X)) && (!Double.IsNaN(direction.Y)) && (!Double.IsNaN(direction.Z)) ) { direction.Normalize(); PositionOld = Position; Position.X += direction.X * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 50.0f; Position.Z += direction.Z * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 50.0f; ChangeDirection(); } if ( (destinyPointer != null) && (Math.Abs(Position.X - destinyPointer.Current.X) < 0.5f) && (Math.Abs(Position.Z - destinyPointer.Current.Y) < 0.5f) ) { // Next step walk. if (!destinyPointer.MoveNext()) { EndMove(messages[i]); setIdle(); //Console.WriteLine("done"); } else { Vector3 vecTmp = new Vector3(destinyPointer.Current.X, 0.0f, destinyPointer.Current.Y); direction = vecTmp - Position; } } } else { EndMove(messages[i]); } break; #endregion Move To Attack #region Attack case (int)EventManager.Events.Attack: if (range <= 0) { messages[i].Done = true; break; } if (messages[i].Payload is Unit) { destinyUnit = (Unit)messages[i].Payload; destinyBuilding = null; } else if (messages[i].Payload is Building) { destinyUnit = null; destinyBuilding = (Building)messages[i].Payload; } if ( ( (destinyUnit != null) && (Math.Abs(Position.X - destinyUnit.Position.X) < range) ) || ( (destinyBuilding != null) && (Math.Abs(Position.Z - destinyBuilding.Position.Z) < range) ) ) { if (EventManager.MessageToOld(gameTime, messages[i], ratio)) { if ((GameObject)messages[i].Payload is Unit) { ((Unit)messages[i].Payload).HP -= damage; if (((Unit)messages[i].Payload).dead) { messages[i].Done = true; setIdle(); break; } } else if ((GameObject)messages[i].Payload is Building) { ((Building)messages[i].Payload).HP -= damage; if (((Building)messages[i].Payload).dead) { messages[i].Done = true; setIdle(); break; } } messages[i].timer = gameTime.TotalGameTime; Game1.sounds[0].Play(1.0f, 0.0f, 0.0f); //Console.WriteLine("Attack!"); } if (messages[i].timer == TimeSpan.Zero) { messages[i].timer = gameTime.TotalGameTime; } } else { messages[i].Done = true; setIdle(); if (messages[i].Payload is Unit) { EventManager.CreateMessage(new Message((int)EventManager.Events.MoveToAttack, null, this, destinyUnit)); } else if (messages[i].Payload is Building) { EventManager.CreateMessage(new Message((int)EventManager.Events.MoveToAttack, null, this, destinyBuilding)); } } break; #endregion Attack #region FixCollisions case (int)EventManager.Events.FixCollisions: if (EventManager.MessageToOld(gameTime, messages[i], 5000)) { messages[i].Done = true; break; } foreach (Message _msg in messages) { if (!_msg.Done) { switch (_msg.Type) { case (int)EventManager.Events.MoveUnit: case (int)EventManager.Events.MoveToAttack: case (int)EventManager.Events.MoveToBuild: Unit sen = ((Unit)messages[i].Sender); if ((destinyPoints != null) && (destinyPoints.Count > 0)) { if ( (sen.Position.X - destinyPoints[destinyPoints.Count - 1].X < 5.0f) && (sen.Position.Z - destinyPoints[destinyPoints.Count - 1].Y < 5.0f) ) { messages[i].Done = true; EndMove(_msg); break; } int[,] map = Map.WalkMeshMap; map[(int)sen.Position.X / Map.SKALA, (int)sen.Position.Z / Map.SKALA] = 1; Laikos.PathFiding.Wspolrzedne wspBegin = new Laikos.PathFiding.Wspolrzedne((int)this.Position.X, (int)this.Position.Z); Laikos.PathFiding.Wspolrzedne wspEnd = new Laikos.PathFiding.Wspolrzedne(destinyPoints[destinyPoints.Count - 1].X, destinyPoints[destinyPoints.Count - 1].Y); destinyPoints = pathFiding.obliczSciezke(wspBegin, wspEnd, map); destinyPointer = null; if ((destinyPoints != null) && (destinyPoints.Count > 0) && (destinyPointer == null)) { destinyPointer = destinyPoints.GetEnumerator(); destinyPointer.MoveNext(); Vector3 vecTmp = new Vector3(destinyPointer.Current.X, 0.0f, destinyPointer.Current.Y); direction = vecTmp - Position; } } else { EndMove(_msg); messages[i].Done = true; } messages[i].Done = true; break; } } } break; #endregion FixCollisions } } }
//This method performs much more detailed collision check. //It checks if there is collision for each mesh of model public static bool DetailedCollisionCheck(Unit unit1, Unit unit2) { //first we check if there is general collision between two models //If method returns false we dont have to perform detailed check if (!GeneralCollisionCheck(unit1, unit2)) return false; //Here we are creating BoundingBox for each mesh for model1 Matrix[] model1Transforms = new Matrix[unit1.currentModel.Model.Bones.Count]; unit1.currentModel.Model.CopyAbsoluteBoneTransformsTo(model1Transforms); BoundingBox[] model1Boxs = new BoundingBox[unit1.currentModel.Model.Meshes.Count]; for (int i = 0; i < unit1.currentModel.Model.Meshes.Count; i++) { ModelMesh mesh = unit1.currentModel.Model.Meshes[i]; BoundingSphere origSphere = mesh.BoundingSphere; BoundingBox origBox = BoundingBox.CreateFromSphere(origSphere); Matrix trans = model1Transforms[mesh.ParentBone.Index] * unit1.GetWorldMatrix(); BoundingBox transBox = XNAUtils.TransformBoundingBox(origBox, trans); model1Boxs[i] = transBox; } //and here for second model Matrix[] model2Transforms = new Matrix[unit2.currentModel.Model.Bones.Count]; unit2.currentModel.Model.CopyAbsoluteBoneTransformsTo(model2Transforms); BoundingBox[] model2Boxs = new BoundingBox[unit2.currentModel.Model.Meshes.Count]; for (int i = 0; i < unit2.currentModel.Model.Meshes.Count; i++) { ModelMesh mesh = unit2.currentModel.Model.Meshes[i]; BoundingSphere origSphere = mesh.BoundingSphere; BoundingBox origBox = BoundingBox.CreateFromSphere(origSphere); Matrix trans = model2Transforms[mesh.ParentBone.Index] * unit2.GetWorldMatrix(); BoundingBox transBox = XNAUtils.TransformBoundingBox(origBox, trans); model2Boxs[i] = transBox; } bool collision = false; //Check if any of created before Boxs intersects with another Box for (int i = 0; i < model1Boxs.Length; i++) for (int j = 0; j < model2Boxs.Length; j++) if (BoundingSphere.CreateFromBoundingBox(model1Boxs[i]).Intersects(BoundingSphere.CreateFromBoundingBox(model2Boxs[j]))) return true; return collision; }
//This method is performing basic collision detection between two models //Whole model is surrounded by BoundingBox stored in model.Tag info public static bool GeneralCollisionCheck(Unit unit1, Unit unit2) { //Retrieving data about BoundingBox from model.Tag for first model ModelExtra modelExtra = unit1.currentModel.Model.Tag as ModelExtra; BoundingSphere originalSphere1 = modelExtra.boundingSphere; unit1.boundingSphere = XNAUtils.TransformBoundingSphere(originalSphere1, unit1.GetWorldMatrix()); //Doing the same thing for second model ModelExtra modelExtra1 = unit2.currentModel.Model.Tag as ModelExtra; BoundingSphere originalSphere2 = modelExtra1.boundingSphere; unit2.boundingSphere = XNAUtils.TransformBoundingSphere(originalSphere2, unit2.GetWorldMatrix()); //Checking if global bounding Box(surronds whole model) intersects another Box bool collision = unit1.boundingSphere.Intersects(unit2.boundingSphere); return collision; }
private static bool IsEnemy(Unit _unit, Game1 _game) { return _unit.player == _game.enemy; }