using Microsoft.Xna.Framework; using Sprint3_actual.BorderElements; using Sprint3_actual.Collision_Detection; using Sprint3_actual.SoundManagement; using System; using System.Collections.Generic; using System.Text; namespace Sprint3_actual.Everything_NPC { /* * IDEA: If we want to improve this implementation in the future, we should create an object * called MoldormNode which would store direction, position, and sprite information about * said node, then add all those objects into one linked list as opposed to having three * separate linked lists. Doing this initially may have saved me quite a few gray hairs */ public class Moldorm : INPC { private Vector2[] Direction = { new Vector2(0,-3), new Vector2(3,-3), new Vector2(3,0), new Vector2(3,3), new Vector2(0,3), new Vector2(-3,3), new Vector2(-3,0), new Vector2(-3,-3), }; public bool isFirstNode; private bool canCollideWithWall; private Vector2 currentDirection; private Vector2 previousDirection; private Vector2 previousDirection2; private int currentDirectionIndex; private LinkedList MoldormBody; private LinkedList bodyPosRect; private LinkedList bodyPartDirection; private room Room; private bool death; private bool removed; private bool spawning; private Rectangle posRect; private Rectangle prevPosition; private Rectangle prevPosition2; private Rectangle prevPosition3; private const int posrectWH = 24; private int collideBuffer; private int index; private float life; private int canTakeDamage; public bool canTakeDirectDamage => true; public bool hasMultipleNodes => true; public Moldorm(int spawnX, int spawnY, room Room) { isFirstNode = false; index = 0; canTakeDamage = 0; life = 5.0f; this.Room = Room; death = false; removed = false; spawning = false; canCollideWithWall = true; currentDirection = Direction[7]; currentDirectionIndex = 7; posRect = new Rectangle(spawnX, spawnY, posrectWH, posrectWH); MoldormBody = new LinkedList(); bodyPosRect = new LinkedList(); bodyPartDirection = new LinkedList(); bodyPosRect.AddLast(posRect); bodyPosRect.AddLast(new Rectangle(spawnX + 24, spawnY + 24, posrectWH, posrectWH)); bodyPosRect.AddLast(new Rectangle(spawnX + 48, spawnY + 48, posrectWH, posrectWH)); bodyPosRect.AddLast(new Rectangle(spawnX + 72, spawnY + 72, posrectWH, posrectWH)); bodyPosRect.AddLast(new Rectangle(spawnX + 96, spawnY + 96, posrectWH, posrectWH)); MoldormBody.AddLast(NPCSpriteFactory.Instance.CreateMoldormBody(posRect.X, posRect.Y, this)); MoldormBody.AddLast(NPCSpriteFactory.Instance.CreateMoldormBody(posRect.X + 24, posRect.Y + 24, this)); MoldormBody.AddLast(NPCSpriteFactory.Instance.CreateMoldormBody(posRect.X + 48, posRect.Y + 48, this)); MoldormBody.AddLast(NPCSpriteFactory.Instance.CreateMoldormBody(posRect.X + 72, posRect.Y + 72, this)); MoldormBody.AddLast(NPCSpriteFactory.Instance.CreateMoldormBody(posRect.X + 96, posRect.Y + 96, this)); initializeLinkedList(bodyPartDirection, currentDirection); collideBuffer = -1; } private void initializeLinkedList(LinkedList list, object o) { for (int i = 0; i < 5; i++) { list.AddLast(o); } } public LinkedListNode getRectangleList() { return bodyPosRect.First; } public float Collide() { return 0; } public float Collide(ICollision side, object borderType) { bool collidingWithDoor = borderType != null && borderType.GetType() == typeof(DungeonDoor); //bool collidingWithWall = borderType != null && borderType.GetType() == typeof(DungeonBorder); if (collideBuffer < 0 && isFirstNode && canCollideWithWall) { previousDirection2 = previousDirection; previousDirection = currentDirection; switch (side) { case ICollision.Top: case ICollision.Bottom: currentDirection.Y *= -1; break; case ICollision.Right: case ICollision.Left: currentDirection.X *= -1; break; } for (int i = 0; i < 8; i++) { if (currentDirection.Equals(Direction[i])) currentDirectionIndex = i; } prevPosition3 = prevPosition2; prevPosition2 = prevPosition; prevPosition = (Rectangle)bodyPosRect.First.Value; if(collidingWithDoor) { collideBuffer = 40; canCollideWithWall = false; } else collideBuffer = -1; } return 0; } public void DrawNPC() { LinkedListNode node = MoldormBody.First; LinkedListNode posNode = bodyPosRect.First; while(node != null) { posRect =(Rectangle)posNode.Value; ((ISprite)node.Value).draw(); node = node.Next; posNode = posNode.Next; } } public Rectangle getRect() { return new Rectangle(); } public Rectangle getMoldormBodyRect(int maxIndex) { if (MoldormBody.Count - 1 < index) index = 0; bool usedMaxIndex = true; if (maxIndex < 0) { maxIndex = index; usedMaxIndex = false; } bool found = false; int i = 0; LinkedListNode node = bodyPosRect.First; while (!found) { if (i == maxIndex) found = true; else { node = node.Next; i++; } } if (!usedMaxIndex) { if (index < MoldormBody.Count - 1) index++; else index = 0; } return (Rectangle)node.Value; } public bool IsDead() { return death; } public bool IsRemoved() { return removed; } public bool IsSpawning() { return spawning; } public void SetPosition(int x, int y) { //NO-OP } public void Spawn() { //NO-OP } public void takeDamage(float damage) { if (canTakeDamage <= 0 && !death) { life -= 0.5f; if (life <= 0.0f) { death = true; removed = true; SoundEngine.Instance.playEnemyDeath(); MoldormBody.RemoveLast(); bodyPosRect.RemoveLast(); bodyPartDirection.RemoveLast(); Room.StageItemList.Add(new SmallKeyItem(posRect.X, posRect.Y)); } else { SoundEngine.Instance.playDamage(); if (life % 1.0 == 0) { MoldormBody.RemoveLast(); bodyPosRect.RemoveLast(); bodyPartDirection.RemoveLast(); } } } } public void UpdateNPC() { LinkedListNode rectNode = bodyPosRect.First; LinkedListNode directionNode = bodyPartDirection.First; //I can provide a better implementation with a collection structure, but I've already spent two many hours on this to do it right now while (rectNode != null) { if(rectNode == bodyPosRect.First) { directionNode.Value = currentDirection; } else if (((Rectangle)rectNode.Value).X == prevPosition.X && ((Rectangle)rectNode.Value).Y == prevPosition.Y) { directionNode.Value = currentDirection; } else if (((Rectangle)rectNode.Value).X == prevPosition2.X && ((Rectangle)rectNode.Value).Y == prevPosition2.Y) { directionNode.Value = previousDirection; } else if (((Rectangle)rectNode.Value).X == prevPosition3.X && ((Rectangle)rectNode.Value).Y == prevPosition3.Y) { directionNode.Value = previousDirection2; } rectNode = rectNode.Next; directionNode = directionNode.Next; } rectNode = bodyPosRect.First; directionNode = bodyPartDirection.First; LinkedList temp = new LinkedList(); while(rectNode != null) { Rectangle currentNode = (Rectangle)rectNode.Value; currentNode.X += (int)((Vector2)directionNode.Value).X; currentNode.Y += (int)((Vector2)directionNode.Value).Y; temp.AddLast(currentNode); rectNode = rectNode.Next; directionNode = directionNode.Next; } bodyPosRect = temp; collideBuffer--; if (collideBuffer < 0) canCollideWithWall = true; } } }