public QuadTree objectMoved(BasicModel bm, QuadTree prevNode) { QuadTree result = null; if (boundingBox.Contains(bm.position) == ContainmentType.Contains) { if (topLeft == null) { if (this != prevNode) { if (prevNode != null) { prevNode.objects.Remove(bm); } return(addObject(bm)); } return(this); } if ((result = topLeft.objectMoved(bm, prevNode)) == null) { if ((result = topRight.objectMoved(bm, prevNode)) == null) { if ((result = bottomLeft.objectMoved(bm, prevNode)) == null) { result = bottomRight.objectMoved(bm, prevNode); } } } } return(result); }
public void removeObject(BasicModel bm) { QuadTree leaf = findLeaf(bm.position); if (leaf != null) { leaf.objects.Remove(bm); } }
public void setClosedGrids(BasicModel bm) { foreach (Node node in gridBoxes) { if (node.box.Contains(bm.position) == ContainmentType.Contains) { closedList.Add(node); } } }
public Node nodeModelIn(BasicModel bm) { foreach (Node node in gridBoxes) { if (node.box.Contains(bm.position) == ContainmentType.Contains) { return(node); } } return(null); }
public Node setStartNode(BasicModel bm) { foreach (Node node in gridBoxes) { if (node.box.Contains(bm.position) == ContainmentType.Contains) { startNode = node; return(node); } } return(null); }
public BasicModel modelCollidingWith(Ray r) { BasicModel tempModel = null; float smallestValue = float.MaxValue; foreach (BasicModel bm in modelManager.models) { float?intersects = r.Intersects(bm.box); if (intersects.HasValue) { if (intersects.Value < smallestValue) { smallestValue = intersects.Value; tempModel = bm; } } } return(tempModel); }
//add objects to this quadtree public QuadTree addObject(BasicModel bm) { QuadTree result = null; if (boundingBox.Contains(bm.position) == ContainmentType.Contains) { if (topLeft == null) { if (objects.Count < maxObjects) { objects.Add(bm); return(this); } //splits the box into 4 split(); if (topLeft == null) { maxObjects *= 2; return(addObject(bm)); } } result = topLeft.addObject(bm); if (result == null) { result = topRight.addObject(bm); if (result == null) { result = bottomLeft.addObject(bm); if (result == null) { result = bottomRight.addObject(bm); } } } } return(result); }
public CharacterStateMachine(BasicModel owner, States initialState) : base(owner) { }
public EnemyStateMachine(BasicModel owner, EnemyStateMachine.States initialState) : base(owner) { }
public StateMachine(BasicModel owner) { this.owner = owner; currentState = States.IDLE; }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> /// protected override void Update(GameTime gameTime) { //killing and death BasicModel basic = enemyCollidingWith(chosen.box); if (basic != null) { collisionType type = collisionBoxToBox(chosen.box, basic.box); if (type == collisionType.TOP) { //remove enemy modelManager.models.Remove(basic); score += 200; } else if (type != collisionType.TOP && type != collisionType.NO_COLLISION) { reloadWorld(); } } // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } if (!isLevelLoaded) { loadLevelBoxes(); aStar = new AStar(modelManager.widthOfMap, modelManager.heightOfMap, (int)modelManager.widthOfMap, (int)modelManager.heightOfMap, this); quadTree = new QuadTree(1, new BoundingBox(new Vector3(0, -modelManager.heightOfMap * 88.7f, 0), new Vector3(modelManager.widthOfMap * 117.3f, 0, 0)), this); if (isLevelLoaded) { Components.Add(quadTree); Components.Add(aStar); } } else { fpsTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; keyboard = Keyboard.GetState(); if (!prevKeyboard.IsKeyDown(Keys.Enter) && keyboard.IsKeyDown(Keys.Enter)) { if (aStar.state == AStar.SearchState.SEARCHING) { aStar.searchStep(); } } if (frameCount % 20 == 0 && aStar.state != AStar.SearchState.SEARCHING && follower.npcState.getCurrentState() != StateMachine.States.IDLE) { aStar.reset(); aStar.openList.Add(aStar.setStartNode(follower)); aStar.endNode = aStar.nodeModelIn(chosen); foreach (BasicModel bm in modelManager.models) { if (bm is GroundModel) { aStar.closedList.Add(aStar.nodeModelIn(bm)); } } aStar.state = AStar.SearchState.SEARCHING; } frameCount++; if (fpsTimer >= 1) { FPS = frameCount; frameCount = 0; fpsTimer = 0; } //keyboard check for chosen character chosen.keyCheck(); //bounds the PC's and enemies to gravity and the level chosen.checkCollision(); follower.checkCollision(); foreach (Enemy en in modelManager.models.OfType <Enemy>()) { en.Update(camera); en.checkCollision(); } //debug shnuff if (!prevKeyboard.IsKeyDown(Keys.Z) && keyboard.IsKeyDown(Keys.Z)) { Console.WriteLine("stateFollower: " + follower.npcState); Console.WriteLine("newLine___" + chosen.ToString()); } //Switch your character if (!prevKeyboard.IsKeyDown(Keys.E) && keyboard.IsKeyDown(Keys.E) && chosen is Human) { foreach (Dwarf dwarf in modelManager.models.OfType <Dwarf>()) { follower = chosen; chosen = dwarf; } } else if (!prevKeyboard.IsKeyDown(Keys.E) && keyboard.IsKeyDown(Keys.E) && chosen is Dwarf) { foreach (Human human in modelManager.models.OfType <Human>()) { follower = chosen; chosen = human; } } //toggle follow camera if (!prevKeyboard.IsKeyDown(Keys.X) && keyboard.IsKeyDown(Keys.X)) { if (!isFollowingPlayer) { isFollowingPlayer = true; } else { isFollowingPlayer = false; } } //follow character maybe if (isFollowingPlayer) { Vector3 tempCamPos = camera.position; tempCamPos.X = chosen.position.X; tempCamPos.Y = chosen.position.Y - 50; camera.position = tempCamPos; } //reset characters to original position if (keyboard.IsKeyDown(Keys.R)) { reloadWorld(); SoundEffectInstance se = (SoundEffectInstance)soundManager.soundBank["dwarfStep"]; se.Play(); } if (keyboard.IsKeyDown(Keys.T)) { SoundEffectInstance se = (SoundEffectInstance)soundManager.soundBank["humanStep"]; se.Play(); } prevKeyboard = keyboard; } base.Update(gameTime); }