/* * Created By Joshua Ray * 11/11/2012 * * Class that represents the Treaures of the game * */ /// <summary> /// Constructor. Places the enemy at the given Mazeblock. The /// mazeblock should be walkable /// </summary> /// <param name="StartBlock">Starting block</param> public Treasure(MazeBlock StartBlock) { kinetics = Kinetic.ZERO(); int choice = Utilities.Instance().rand.Next(0, 2); switch (choice) { case 0: sprite = TreasureHunter.content.Load <Texture2D>(@"PlanetCute\Gem Blue"); color = Color.Blue; break; case 1: sprite = TreasureHunter.content.Load <Texture2D>(@"PlanetCute\Gem Green"); color = Color.Green; break; case 2: sprite = TreasureHunter.content.Load <Texture2D>(@"PlanetCute\Gem Orange"); color = Color.Orange; break; } kinetics.position = new Vector2(StartBlock.kinetics.boundingBox.Center.X - sprite.Width / 2, StartBlock.kinetics.boundingBox.Center.Y - sprite.Height / 2); Kinetic.SetBoundingBoxDimensions(ref kinetics.boundingBox, sprite); kinetics.Update(); }
/// <summary> /// Constructor. Places the enemy at the given Mazeblock. The /// mazeblock should be walkable /// </summary> /// <param name="StartBlock">Starting block</param> public Treasure(MazeBlock StartBlock) { kinetics = Kinetic.ZERO(); Random rand = new Random(DateTime.Now.Millisecond); color = rand.Next(0, 2); switch (color) { case 0: sprite = TreasureHunter.content.Load <Texture2D>(@"PlanetCute\Gem Blue"); break; case 1: sprite = TreasureHunter.content.Load <Texture2D>(@"PlanetCute\Gem Green"); break; case 2: sprite = TreasureHunter.content.Load <Texture2D>(@"PlanetCute\Gem Orange"); break; } kinetics.position = new Vector2(StartBlock.kinetics.boundingBox.Center.X - sprite.Width / 2, StartBlock.kinetics.boundingBox.Center.Y - sprite.Height / 2); Kinetic.SetBoundingBoxDimensions(ref kinetics.boundingBox, sprite); kinetics.Update(); }
/// <summary> /// Resets to initial settings /// </summary> public override void Reset() { currentBlock = startBlock; target.X = currentBlock.kinetics.boundingBox.Center.X; target.Y = currentBlock.kinetics.boundingBox.Center.Y; kinetics.position = target; }
/// <summary> /// Constructor. /// </summary> /// <param name="Maze">Maze that will be used for referencing</param> public Radar(MazeBlock[,] Maze) { map = new Texture2D( TreasureHunter.graphics.GraphicsDevice, Maze.GetLength(0), Maze.GetLength(1), false, SurfaceFormat.Color); Color[] data = new Color[Maze.Length]; for (int index = 0; index < Maze.GetLength(0); index++) { for (int endex = 0; endex < Maze.GetLength(1); endex++) { MazeBlock temp = Maze[index, endex]; if (temp != null) { if (temp.Walkable) { data[index + endex * Maze.GetLength(0)] = Color.Red; continue; } } data[index + endex * Maze.GetLength(0)] = Color.Transparent; } } map.SetData(data); }
/// <summary> /// Algorithm that chooses the next Maze Block that is nearest to the player. /// </summary> /// <returns>Maze Block that is nearest to the player</returns> MazeBlock ChooseClosestToPlayer() { MazeBlock temp = null; float distance = 10000; if (currentBlock.Up != null) { if (currentBlock.Up.Walkable) { float tempDistance = Vector2.Distance(currentBlock.Up.kinetics.position, Avatar.Instance().kinetics.position); if (tempDistance < distance) { distance = tempDistance; temp = currentBlock.Up; } } } if (currentBlock.Down != null) { if (currentBlock.Down.Walkable) { float tempDistance = Vector2.Distance(currentBlock.Down.kinetics.position, Avatar.Instance().kinetics.position); if (tempDistance < distance) { distance = tempDistance; temp = currentBlock.Down; } } } if (currentBlock.Left != null) { if (currentBlock.Left.Walkable) { float tempDistance = Vector2.Distance(currentBlock.Left.kinetics.position, Avatar.Instance().kinetics.position); if (tempDistance < distance) { distance = tempDistance; temp = currentBlock.Left; } } } if (currentBlock.Right != null) { if (currentBlock.Right.Walkable) { float tempDistance = Vector2.Distance(currentBlock.Right.kinetics.position, Avatar.Instance().kinetics.position); if (tempDistance < distance) { distance = tempDistance; temp = currentBlock.Right; } } } return(temp); }
/// <summary> /// Constructor /// </summary> /// <param name="Width">Unit width of the maze</param> /// <param name="Height">Unit height of the maze</param> /// <param name="ScaleX">Unit width of maze cell</param> /// <param name="ScaleY">Unit height of maze cell</param> /// <param name="Seconds">Time required to complete</param> public Level(int Width, int Height, int ScaleX, int ScaleY, int Seconds) { width = Width; height = Height; scaleX = ScaleX; scaleY = ScaleY; discovered = 0; death = 0; MazeGenerator levelRef = new MazeGenerator(width, height, ScaleX, ScaleY); scaledWidth = levelRef.ScaledWidth(); scaledHeight = levelRef.ScaledHeight(); ObjectList = new List <GameObject>(); List <MazeBlock> walkables = new List <MazeBlock>(); radar = new Radar(levelRef.maze); foreach (GameObject Obj in levelRef.maze) { MazeBlock temp = Obj as MazeBlock; if (temp.Walkable) { walkables.Add(temp); walkableSpace++; } InsertGameObject(Obj); } firstPosition = walkables.ElementAt <MazeBlock>(0).kinetics.position; treasure = new Treasure(walkables.ElementAt <MazeBlock>(walkables.Count - 1)); InsertGameObject(treasure); walkables.RemoveAt(0); Random rand = new Random(DateTime.Now.Millisecond); if (walkables.Count > 0) { walkables.RemoveAt(walkables.Count - 1); int index = walkables.Count / 10; while (index > 0) { MazeBlock block = walkables.ElementAt <MazeBlock>(rand.Next(0, walkables.Count)); if (Vector2.Distance(firstPosition, block.kinetics.position) > 500) { InsertGameObject(new Enemy(block)); index--; } } } timeLeft = new Timer(Seconds, true); }
/// <summary> /// Constructor. Places the enemy at the given Mazeblock. The /// mazeblock should be walkable /// </summary> /// <param name="StartBlock">Starting block</param> public Enemy(MazeBlock StartBlock) { kinetics = Kinetic.ZERO(); kinetics.position = new Vector2(StartBlock.kinetics.boundingBox.Center.X, StartBlock.kinetics.boundingBox.Center.Y); sprite = TreasureHunter.content.Load <Texture2D>(@"PlanetCute\Enemy Bug Small"); Kinetic.SetBoundingBoxDimensions(ref kinetics.boundingBox, sprite); currentBlock = StartBlock; target.X = currentBlock.kinetics.boundingBox.Center.X; target.Y = currentBlock.kinetics.boundingBox.Center.Y; startBlock = StartBlock; }
/// <summary> /// Constructor /// </summary> /// <param name="Width">Unit width of the maze</param> /// <param name="Height">Unit height of the maze</param> /// <param name="ScaleX">Unit width of maze cell</param> /// <param name="ScaleY">Unit height of maze cell</param> /// <param name="Seconds">Time required to complete</param> public Level(int Width, int Height, int ScaleX, int ScaleY, int Seconds) { unitWidth = Width; unitHeight = Height; cellWidth = ScaleX; cellHeight = ScaleY; combo = 0; MaxCombo = 0; discovered = 0; death = 0; List <MazeBlock> walkables = new List <MazeBlock>(); UpdateList = new List <GameObject>(); UpdateList = new List <GameObject>(); DrawList = new List <GameObject>(); while (walkables.Count() < 1) { MazeGenerator levelRef = new MazeGenerator(unitWidth, unitHeight, ScaleX, ScaleY); walkables = new List <MazeBlock>(); UpdateList = new List <GameObject>(); radar = new Radar(levelRef.maze); levelWidth = levelRef.MazeWidth(); levelHeight = levelRef.MazeHeight(); foreach (GameObject Obj in levelRef.maze) { if (Obj == null) { continue; } MazeBlock temp = Obj as MazeBlock; if (temp.Walkable) { walkables.Add(temp); walkableSpace++; InsertGameObjectDraw(Obj); } InsertGameObject(Obj); } } firstPosition = walkables.ElementAt <MazeBlock>(0).kinetics.position; treasure = new Treasure(walkables.ElementAt <MazeBlock>(walkables.Count - 1)); InsertGameObject(treasure); walkables.RemoveAt(0); PlaceEnemies(walkables, 10); timeLeft = new Timer(Seconds, true); comboTimer = new Timer(1); }
void PlaceEnemies(List <MazeBlock> Walkables, int Count) { if (Walkables.Count > 0) { Walkables.RemoveAt(Walkables.Count - 1); while (Count > 0) { MazeBlock block = Walkables.ElementAt <MazeBlock>(Utilities.Instance().rand.Next(0, Walkables.Count)); if (Vector2.Distance(firstPosition, block.kinetics.position) > 500) { InsertGameObject(new Enemy(block)); } Count--; } } }
/// <summary> /// Algorithm that decides where the enemy should move to next. /// </summary> void MakeDecision() { MazeBlock temp = null; if (Utilities.Instance().rand.NextDouble() > .9f) { temp = Choose(choice); } while (temp == null) { int tempChoice = Utilities.Instance().rand.Next(0, 3); if (tempChoice == choice) { } temp = ChooseClosestToPlayer(); } currentBlock = temp; target.X = currentBlock.kinetics.boundingBox.Center.X - sprite.Width / 2; target.Y = currentBlock.kinetics.boundingBox.Center.Y - sprite.Height / 2;; }
/// <summary> /// Algorithm that decides where the enemy should move to next. /// </summary> void MakeDecision() { Random rand = new Random(DateTime.Now.Millisecond); MazeBlock temp = null; if (rand.NextDouble() > .9f) { temp = Choose(choice); } while (temp == null) { int tempChoice = rand.Next(0, 3); if (tempChoice == choice) { } temp = ChooseClosestToPlayer(); } currentBlock = temp; target.X = currentBlock.kinetics.boundingBox.Center.X - sprite.Width / 2; target.Y = currentBlock.kinetics.boundingBox.Center.Y - sprite.Height / 2;; }
/// <summary> /// Constructor. /// </summary> /// <param name="Width">Width of the maze in units</param> /// <param name="Height">Height of the maze in units</param> /// <param name="ScaleX">Width of a maze unit</param> /// <param name="ScaleY">Height of a maze unit</param> public MazeGenerator(int Width, int Height, int ScaleX, int ScaleY) { rand = new Random(DateTime.Now.Millisecond); width = Width; height = Height; scaleX = ScaleX; scaleY = ScaleY; maze = new MazeBlock[Width, Height]; for (int index = 0; index < width; index++) { for (int endex = 0; endex < height; endex++) { maze[index, endex] = new MazeBlock(); maze[index, endex].Walkable = false; maze[index, endex].kinetics.position = new Vector2(index * ScaleX, endex * ScaleY); maze[index, endex].kinetics.maxSpeed = 0; maze[index, endex].kinetics.Update(); } } GenerateMaze(); AssignNeighbors(); }
/// <summary> /// Constructor. /// </summary> /// <param name="Width">Width of the maze in units</param> /// <param name="Height">Height of the maze in units</param> /// <param name="ScaleX">Width of a maze unit</param> /// <param name="ScaleY">Height of a maze unit</param> public MazeGenerator(int Width, int Height, int UnitWidth, int UnitHeight) { width = Width; height = Height; unitWidth = UnitWidth; unitHeight = UnitHeight; maze = new MazeBlock[Width, Height]; for (int index = 0; index < width; index++) { for (int endex = 0; endex < height; endex++) { maze[index, endex] = new MazeBlock(); maze[index, endex].Walkable = false; maze[index, endex].kinetics.position = new Vector2(index * unitWidth, endex * unitHeight); maze[index, endex].kinetics.maxSpeed = 0; maze[index, endex].kinetics.Update(); } } GenerateMaze(); AssignNeighbors(); PruneBlocks(); }
public void PlaceAt(MazeBlock Block) { kinetics.position = new Vector2(Block.kinetics.boundingBox.Center.X, Block.kinetics.boundingBox.Center.Y); }
/// <summary> /// Method for resolving collisions with given Item /// </summary> /// <param name="Item">Item that has collided with this</param> public override void CollisionResolution(GameObject Item) { if (Item.GetType() == typeof(MazeBlock)) { return; } if (Item.GetType() == typeof(Avatar)) { if (!Walkable) { float margin = (float)Math.Ceiling(Avatar.Instance().speed) + 1; //Down if (Item.kinetics.boundingBox.Bottom >= kinetics.boundingBox.Top && Item.kinetics.boundingBox.Bottom <= kinetics.boundingBox.Top + margin) { if (ControllerInput.Instance().GetKey(Microsoft.Xna.Framework.Input.Keys.Space).Pressed) { MazeBlock target = Down; if (target.Walkable == true) { Item.kinetics.position.X = target.kinetics.position.X + 10; Item.kinetics.position.Y = target.kinetics.position.Y - 25; Item.kinetics.boundingBox.X = (int)target.kinetics.position.X + Item.kinetics.BoundingBoxOffset.X; Item.kinetics.boundingBox.Y = (int)target.kinetics.position.Y + Item.kinetics.BoundingBoxOffset.Y; Avatar.Instance().Teleported(); } } else { float offset = Item.kinetics.boundingBox.Top - Item.kinetics.position.Y; Item.kinetics.position.Y = kinetics.boundingBox.Top - Item.kinetics.boundingBox.Height - Item.kinetics.BoundingBoxOffset.Y; Item.kinetics.boundingBox.Y = (int)(Item.kinetics.position.Y + Item.kinetics.BoundingBoxOffset.Y); } return; } //Left if (Item.kinetics.boundingBox.Right >= kinetics.boundingBox.Left && Item.kinetics.boundingBox.Right <= kinetics.boundingBox.Left + margin) { if (ControllerInput.Instance().GetKey(Microsoft.Xna.Framework.Input.Keys.Space).Pressed) { MazeBlock target = Right; if (target.Walkable == true) { Item.kinetics.position.X = target.kinetics.position.X + 10; Item.kinetics.position.Y = target.kinetics.position.Y - 25; Item.kinetics.boundingBox.X = (int)target.kinetics.position.X + Item.kinetics.BoundingBoxOffset.X; Item.kinetics.boundingBox.Y = (int)target.kinetics.position.Y + Item.kinetics.BoundingBoxOffset.Y; Avatar.Instance().Teleported(); } } else { Item.kinetics.position.X = kinetics.boundingBox.Left - Item.kinetics.boundingBox.Width - Item.kinetics.BoundingBoxOffset.X; Item.kinetics.boundingBox.X = (int)(Item.kinetics.position.X + Item.kinetics.BoundingBoxOffset.X); return; } } if (Item.kinetics.boundingBox.Left <= kinetics.boundingBox.Right && Item.kinetics.boundingBox.Left >= kinetics.boundingBox.Right - margin) { if (Item.kinetics.boundingBox.Top <= kinetics.boundingBox.Bottom && Item.kinetics.boundingBox.Top >= kinetics.boundingBox.Bottom - margin) { if (Right != null) { if (!Right.Walkable) { if (ControllerInput.Instance().GetKey(Microsoft.Xna.Framework.Input.Keys.Space).Pressed) { MazeBlock target = Up; if (target.Walkable == true) { Item.kinetics.position.X = target.kinetics.position.X + 10; Item.kinetics.position.Y = target.kinetics.position.Y - 25; Item.kinetics.boundingBox.X = (int)target.kinetics.position.X + Item.kinetics.BoundingBoxOffset.X; Item.kinetics.boundingBox.Y = (int)target.kinetics.position.Y + Item.kinetics.BoundingBoxOffset.Y; Avatar.Instance().Teleported(); } } else { float offset = Item.kinetics.boundingBox.Top - Item.kinetics.position.Y; Item.kinetics.position.Y = kinetics.boundingBox.Bottom - Item.kinetics.BoundingBoxOffset.Y; Item.kinetics.boundingBox.Y = (int)(Item.kinetics.position.Y + Item.kinetics.BoundingBoxOffset.Y); Item.kinetics.velocity.Y = 0; return; } } } } if (ControllerInput.Instance().GetKey(Microsoft.Xna.Framework.Input.Keys.Space).Pressed) { MazeBlock target = Left; if (target.Walkable == true) { Item.kinetics.position.X = target.kinetics.position.X + 10; Item.kinetics.position.Y = target.kinetics.position.Y - 25; Item.kinetics.boundingBox.X = (int)target.kinetics.position.X + Item.kinetics.BoundingBoxOffset.X; Item.kinetics.boundingBox.Y = (int)target.kinetics.position.Y + Item.kinetics.BoundingBoxOffset.Y; Avatar.Instance().Teleported(); } } else { Item.kinetics.position.X = kinetics.boundingBox.Right - Item.kinetics.BoundingBoxOffset.X; Item.kinetics.boundingBox.X = (int)(Item.kinetics.position.X + Item.kinetics.BoundingBoxOffset.X); } return; } if (Item.kinetics.boundingBox.Top <= kinetics.boundingBox.Bottom && Item.kinetics.boundingBox.Top >= kinetics.boundingBox.Bottom - margin) { if (ControllerInput.Instance().GetKey(Microsoft.Xna.Framework.Input.Keys.Space).Pressed) { MazeBlock target = Up; if (target.Walkable == true) { Item.kinetics.position.X = target.kinetics.position.X + 10; Item.kinetics.position.Y = target.kinetics.position.Y - 25; Item.kinetics.boundingBox.X = (int)target.kinetics.position.X + Item.kinetics.BoundingBoxOffset.X; Item.kinetics.boundingBox.Y = (int)target.kinetics.position.Y + Item.kinetics.BoundingBoxOffset.Y; Avatar.Instance().Teleported(); } } else { float offset = Item.kinetics.boundingBox.Top - Item.kinetics.position.Y; Item.kinetics.position.Y = kinetics.boundingBox.Bottom - Item.kinetics.BoundingBoxOffset.Y; Item.kinetics.boundingBox.Y = (int)(Item.kinetics.position.Y + Item.kinetics.BoundingBoxOffset.Y); Item.kinetics.velocity.Y = 0; } return; } } else { if (!discovered) { LevelManager.Instance().Discovered(); color = Color.White; } discovered = true; } } }