public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; this._systems = new List<GameSystem>(); this._objects = new List<GameObject>(); this._services = new Dictionary<string, GameSystem>(); this._systemsCallOrder = new List<int>(); this.curScreen = ScreenType.Start; this.graphics.PreferredBackBufferWidth = 1200; this.graphics.PreferredBackBufferHeight = 800; this.graphics.ApplyChanges(); this.CurrentPathfindingGraph = new AStarGraph(this, new List<AStarNode>()); this.play1 = SpriteType.None; this.play2 = SpriteType.None; this.winner = 0; this.playe1 = PlayerType.None; this.playe2 = PlayerType.None; gameType = GameTypes.None; LivesOrTime = 0; this.mapType = MapType.Basic; this.Winner = null; }
public bool GetBestPath(GameObject obj, GameTime gameTime, AStarGraph graph) { Stack<AStarNode> path = new Stack<AStarNode>(); Transform2DComponent transformComponent = (Transform2DComponent)obj.GetComponent(ComponentType.Transform2D); AStarComponent aStarComponent = (AStarComponent)obj.GetComponent(ComponentType.AStar); if (transformComponent == null || aStarComponent == null) { return false; } AStarNode entityNode = graph.GetClosestNode(transformComponent.GetTranslation()); aStarComponent.CurrentNode = entityNode; AStarNode goalNode; if (aStarComponent.Follow) { GameObject entity = aStarComponent.EntityToFollow; Transform2DComponent entityTransformComponent = (Transform2DComponent)entity.GetComponent(ComponentType.Transform2D); aStarComponent.EntityToFollowPosBuffer = entityTransformComponent.GetTranslation(); goalNode = graph.GetClosestNode(aStarComponent.EntityToFollowPosBuffer); } else { goalNode = graph.GetClosestNode(aStarComponent.GoalPosition); } this.resolvePath(aStarComponent, entityNode, goalNode, graph); return true; }
public GameScreen(Game1 game, SpriteType player1, SpriteType player2) { game.IsMouseVisible = true; this.envFactory = new EnvironmentFactory(game); this.clientBounds = game.Window.ClientBounds; mapInitSystemID = game.RegisterSystem(new ScreenInitializationSystem(game)); spriteInitSystemID1 = game.RegisterSystem(new SpriteInitializationSystem(game, 1)); spriteInitSystemID2 = game.RegisterSystem(new SpriteInitializationSystem(game, 2)); songInitSystemID = game.RegisterSystem(new SongInitializer(game)); if (game.playe2 == PlayerType.Human) { controllerInputSystemID = game.RegisterSystem(new ControllerInputSystem(game, 2)); //arrowInputSystemID = game.RegisterSystem(new ArrowInputSystem(game, 2)); inputSystemID2 = game.RegisterSystem(new InputSystem(game, 2)); } if (game.playe1 == PlayerType.Human) { arrowInputSystemID = game.RegisterSystem(new ArrowInputSystem(game, 1)); //controllerInputSystemID = game.RegisterSystem(new ControllerInputSystem(game, 1)); inputSystemID1 = game.RegisterSystem(new InputSystem(game, 1)); } animationSystemID1 = game.RegisterSystem(new AnimationSystem(game, 1)); animationSystemID2 = game.RegisterSystem(new AnimationSystem(game, 2)); movementSystemID1 = game.RegisterSystem(new MovementSystem(game, 1)); movementSystemID2 = game.RegisterSystem(new MovementSystem(game, 2)); actionSystemID = game.RegisterSystem(new ActionSystem(game)); physicsSystemID = game.RegisterSystem(new PhysicsSystem(game)); boundsSystemID = game.RegisterSystem(new BoundsSystem(game)); meshRendererID = game.RegisterSystem(new LinebatchMeshRenderSystem(game)); collisionRendererID = game.RegisterSystem(new CollisionRenderSystem(game)); mapRendererID = game.RegisterSystem(new MapRenderer(game)); spriteRendererID = game.RegisterSystem(new SpriteRenderer(game)); uiRendererID = game.RegisterSystem(new UIRenderer(game)); transformResolverID = game.RegisterSystem(new PhysicsTransformResolverSystem(game)); lifetimeSystemID = game.RegisterSystem(new LifetimeSystem(game)); int aStarPathRendererID = game.RegisterSystem(new AStarPathRenderer(game)); int aStarRendererID = game.RegisterSystem(new AStarPathfindingRenderer(game)); int aiSystemID = game.RegisterSystem(new AISystem(game)); int timerRendererID = game.RegisterSystem(new TimerRenderSystem(game)); int timerSystemID = game.RegisterSystem(new TimerSystem(game)); songSystemID = game.RegisterSystem(new MusicSystem(game)); //services int collisionSystemID = game.RegisterSystem(new SATCollisionSystem(game, "Collision")); int aStarServiceID = game.RegisterSystem(new AStarPathfindingSystem(game, "Pathfinding", 50)); if (game.playe1 == PlayerType.Human && game.playe2 == PlayerType.Human) { game.SetSystemCallOrder(new List<int> { this.mapInitSystemID, this.spriteInitSystemID1, this.spriteInitSystemID2, this.songInitSystemID, this.arrowInputSystemID, this.controllerInputSystemID, aiSystemID, this.inputSystemID1, this.inputSystemID2, this.animationSystemID1, this.animationSystemID2, this.movementSystemID1, this.movementSystemID2, this.actionSystemID, this.physicsSystemID, this.transformResolverID, this.boundsSystemID, this.lifetimeSystemID, //this.physicsSystemID, this.meshRendererID, this.mapRendererID, this.collisionRendererID, this.spriteRendererID, this.uiRendererID, aStarRendererID, aStarPathRendererID, songSystemID }); } else if (game.playe1 == PlayerType.Human) { game.SetSystemCallOrder(new List<int> { this.mapInitSystemID, this.spriteInitSystemID1, this.spriteInitSystemID2, this.songInitSystemID, this.arrowInputSystemID, aiSystemID, this.inputSystemID1, this.animationSystemID1, this.animationSystemID2, this.movementSystemID1, this.movementSystemID2, this.actionSystemID, this.physicsSystemID, this.transformResolverID, this.boundsSystemID, this.lifetimeSystemID, //this.physicsSystemID, this.meshRendererID, this.mapRendererID, this.collisionRendererID, this.spriteRendererID, this.uiRendererID, aStarRendererID, aStarPathRendererID, songSystemID }); } else if (game.playe2 == PlayerType.Human) { game.SetSystemCallOrder(new List<int> { this.mapInitSystemID, this.spriteInitSystemID1, this.spriteInitSystemID2, this.songInitSystemID, this.controllerInputSystemID, aiSystemID, this.inputSystemID2, this.animationSystemID1, this.animationSystemID2, this.movementSystemID1, this.movementSystemID2, this.actionSystemID, this.physicsSystemID, this.transformResolverID, this.boundsSystemID, this.lifetimeSystemID, //this.physicsSystemID, this.meshRendererID, this.mapRendererID, this.collisionRendererID, this.spriteRendererID, this.uiRendererID, aStarRendererID, aStarPathRendererID, songSystemID }); } if (game.playe1 == PlayerType.Human) { if (player1 == SpriteType.Yoshi) { this.p1 = DynamicEntityFactory.BuildPlayerControlledEntity( game, 1, Color.Green, new Vector2(100, 0), 0.0f, new Vector2(1.0f, 1.0f), 400, 1000, SpriteType.Yoshi, new List<Shape> { //Shape.BuildRectangle(new Rectangle(-55, -60, 90, 60)), Shape.BuildRectangle(new Rectangle(-40, 0, 120, 60)), Shape.BuildRectangle(new Rectangle(25, 60, 40, 30)) }, MoveDefinitions.GetYoshiMoves() ); } else { this.p1 = DynamicEntityFactory.BuildPlayerControlledEntity( game, 1, Color.Green, new Vector2(100, 0), 0.0f, new Vector2(1.0f, 1.0f), 300, 1000, SpriteType.Kirby, new List<Shape> { Shape.BuildRectangle(new Rectangle(-60, -60, 110, 110)) }, MoveDefinitions.GetKirbyMoves() ); //kirby.AddComponent(new LifetimeComponent(kirby, 5000)); //kirby.SetParent(yoshi); } } else { if (player1 == SpriteType.Yoshi) { this.p1 = DynamicEntityFactory.BuildComputerControlledEntity( game, 1, Color.Green, new Vector2(800, 0), 0.0f, new Vector2(1.0f, 1.0f), 250, 500, SpriteType.Yoshi, new List<Shape> { Shape.BuildRectangle(new Rectangle(-40, 0, 120, 60)), Shape.BuildRectangle(new Rectangle(25, 60, 40, 30)) }, MoveDefinitions.GetYoshiMoves(), this.p2 ); } else { this.p1 = DynamicEntityFactory.BuildComputerControlledEntity( game, 1, Color.Green, new Vector2(800, 0), 0.0f, new Vector2(1.0f, 1.0f), 250, 500, SpriteType.Kirby, new List<Shape> { Shape.BuildRectangle(new Rectangle(-60, -60, 110, 110)) }, MoveDefinitions.GetKirbyMoves(), this.p2 ); } } game.AddObject(p1); if (game.playe1 == PlayerType.Human) { if (player2 == SpriteType.Yoshi) { this.p2 = DynamicEntityFactory.BuildPlayerControlledEntity( game, 2, Color.Pink, new Vector2(800, 0), 0.0f, new Vector2(1.0f, 1.0f), 400, 500, SpriteType.Yoshi, new List<Shape> { //Shape.BuildRectangle(new Rectangle(-55, -60, 90, 60)), Shape.BuildRectangle(new Rectangle(-40, 0, 120, 60)), Shape.BuildRectangle(new Rectangle(25, 60, 40, 30)) }, MoveDefinitions.GetYoshiMoves() ); } else { this.p2 = DynamicEntityFactory.BuildPlayerControlledEntity( game, 2, Color.Pink, new Vector2(500, 0), 0.0f, new Vector2(1.0f, 1.0f), 300, 10000, SpriteType.Kirby, new List<Shape> { Shape.BuildRectangle(new Rectangle(-60, -60, 110, 110)) }, MoveDefinitions.GetKirbyMoves() ); //this.p2 = DynamicEntityFactory.BuildComputerControlledEntity( // game, // 2, // Color.Pink, // new Vector2(800, 0), // 0.0f, // new Vector2(1.0f, 1.0f), // 250, // 500, // SpriteType.Kirby, // new List<Shape> // { // Shape.BuildRectangle(new Rectangle(-60, -60, 110, 110)) // }, // MoveDefinitions.GetYoshiMoves(), // this.p1 //); } } else { if (player2 == SpriteType.Yoshi) { this.p2 = DynamicEntityFactory.BuildComputerControlledEntity( game, 2, Color.Pink, new Vector2(800, 0), 0.0f, new Vector2(1.0f, 1.0f), 250, 500, SpriteType.Yoshi, new List<Shape> { Shape.BuildRectangle(new Rectangle(-40, 0, 120, 60)), Shape.BuildRectangle(new Rectangle(25, 60, 40, 30)) }, MoveDefinitions.GetYoshiMoves(), this.p1 ); } else { this.p2 = DynamicEntityFactory.BuildComputerControlledEntity( game, 2, Color.Pink, new Vector2(800, 0), 0.0f, new Vector2(1.0f, 1.0f), 250, 500, SpriteType.Kirby, new List<Shape> { Shape.BuildRectangle(new Rectangle(-60, -60, 110, 110)) }, MoveDefinitions.GetKirbyMoves(), this.p1 ); } } game.AddObject(p2); if (game.mapType == MapType.Basic) { game.AddObject(new TextObject2(game, MapType.Basic)); game.AddObject(envFactory.BuildStaticRectangularObstacle( new Vector2(403, 347), new Rectangle(0, -10, -378, -36), 2000.0f, Color.Red)); game.AddObject(envFactory.BuildStaticRectangularObstacle( new Vector2(1173, 498), new Rectangle(0, -10, -380, -37), 2000.0f, Color.Red)); game.AddObject(envFactory.BuildStaticRectangularObstacle( new Vector2(1165, 119), new Rectangle(0, -10, -530, -39), 2000.0f, Color.Red)); game.AddObject(envFactory.BuildStaticRectangularObstacle( new Vector2(1050, 662), new Rectangle(0, -10, -927, -1), 2000.0f, Color.Red)); AStarPathfindingSystem pathfinding = (AStarPathfindingSystem)game.GetService("Pathfinding"); AStarNode node1 = new AStarNode(new Vector2(700, 50)); AStarNode node2 = new AStarNode(new Vector2(320, 270), true); AStarNode node3 = new AStarNode(new Vector2(820, 400)); AStarNode node4 = new AStarNode(new Vector2(700, 620), true); AStarNode node5 = new AStarNode(new Vector2(300, 620)); node1.AddNeighbor(node2); node2.AddNeighbor(node1); node2.AddNeighbor(node3); node3.AddNeighbor(node2); node3.AddNeighbor(node4); node4.AddNeighbor(node3); node4.AddNeighbor(node5); node5.AddNeighbor(node4); AStarGraph graph = new AStarGraph(game, new List<AStarNode> { node1, node2, node3, node4, node5 }); GameObject graphEntity = new GameObject(game); graphEntity.AddComponent(new AStarGraphComponent(graphEntity, graph)); game.AddObject(graphEntity); game.CurrentPathfindingGraph = graph; game.AddObject(new SongObject(game, MapType.Basic)); } else { game.AddObject(new TextObject2(game, MapType.Hyrule)); game.AddObject(envFactory.BuildStaticRectangularObstacle( new Vector2(303, 500), new Rectangle(0, -10, -10, -120), 2000.0f, Color.Red)); game.AddObject(envFactory.BuildStaticRectangularObstacle( new Vector2(302, 505), new Rectangle(0, -10, -140, -10), 2000.0f, Color.Red)); game.AddObject(envFactory.BuildStaticRectangularObstacle( new Vector2(815, 390), new Rectangle(0, -10, -516, -10), 2000.0f, Color.Red)); //game.AddObject(envFactory.BuildStaticRectangularObstacle( // new Vector2(704, 330), // new Rectangle(0, -10, -109, -10), // 2000.0f, // Color.Red)); //game.AddObject(envFactory.BuildStaticRectangularObstacle( // new Vector2(757, 223), // new Rectangle(0, -10, -107, -10), // 2000.0f, // Color.Red)); game.AddObject(envFactory.BuildStaticRectangularObstacle( new Vector2(1064, 510), new Rectangle(0, -10, -250, -10), 2000.0f, Color.Red)); game.AddObject(envFactory.BuildStaticRectangularObstacle( new Vector2(818, 510), new Rectangle(0, -10, -10, -130), 2000.0f, Color.Red)); game.AddObject(envFactory.BuildStaticRectangularObstacle( new Vector2(174, 800), new Rectangle(0, -10, -10, -300), 2000.0f, Color.Red)); game.AddObject(envFactory.BuildStaticRectangularObstacle( new Vector2(1066, 810), new Rectangle(0, -10, -10, -306), 2000.0f, Color.Red)); AStarPathfindingSystem pathfinding = (AStarPathfindingSystem)game.GetService("Pathfinding"); AStarNode node1 = new AStarNode(new Vector2(230, 480), true); AStarNode node2 = new AStarNode(new Vector2(320, 350)); AStarNode node3 = new AStarNode(new Vector2(800, 350)); AStarNode node4 = new AStarNode(new Vector2(890, 480), true); node1.AddNeighbor(node2); node2.AddNeighbor(node1); node2.AddNeighbor(node3); node3.AddNeighbor(node2); node3.AddNeighbor(node4); node4.AddNeighbor(node3); AStarGraph graph = new AStarGraph(game, new List<AStarNode> { node1, node2, node3, node4 }); GameObject graphEntity = new GameObject(game); graphEntity.AddComponent(new AStarGraphComponent(graphEntity, graph)); game.AddObject(graphEntity); game.CurrentPathfindingGraph = graph; game.AddObject(new SongObject(game, MapType.Hyrule)); } //game.AddObject(envFactory.BuildStaticRectangularObstacle( // new Vector2(clientBounds.Width / 2, clientBounds.Height), // new Rectangle(-clientBounds.Width / 2, -10, clientBounds.Width, 20), // 2000.0f, // Color.Red)); //game.AddObject(envFactory.BuildStaticRectangularObstacle( // new Vector2(clientBounds.Width / 2, clientBounds.Height / 3 * 2), // new Rectangle(-clientBounds.Width / 2, -10, clientBounds.Width / 2, 20), // 2000.0f, // Color.Red)); //if (this._game.gameType == Types.GameTypes.Timed) //{ // game.AddObject(new TimeObject(game, new Vector2(0, 0), Color.Black)); //} game.Character1 = this.p1; game.Character2 = this.p2; }
private void resolvePath(AStarComponent aStarComponent, AStarNode currentNode_in, AStarNode goalNode_in, AStarGraph graph_in) { BinaryPriorityQueue<AStarNode> openset = new BinaryPriorityQueue<AStarNode>(AStarNode.sortFScoreAscending()); AStarNode currentNode = currentNode_in; AStarNode goalNode = goalNode_in; AStarGraph graph = graph_in; if (currentNode != null) { aStarComponent.ClearPath(); for (int i = 0; i < graph.Nodes.Count; i++) { graph.Nodes[i].Initialize(); } openset.Push(currentNode); currentNode.FScore = this.getFScore(currentNode, goalNode); while (openset.Count > 0) { AStarNode current; current = openset.Pop(); List<AStarNode> neighbors = current.GetNeighbors(); if (current.GetPosition() == goalNode.GetPosition()) { AStarNode node = current; while (node.GetOptimalPathParent() != null) { aStarComponent.Path.Push(node); node = node.GetOptimalPathParent(); } //aStarComponent.Path.Push(node); break; } current.Processed = true; for (int i = 0; i < neighbors.Count; i++) { AStarNode neighbor = neighbors[i]; if (!neighbor.Processed) { float tentative_g_score = current.GScore + (current.GetPosition() - neighbor.GetPosition()).Length(); if (!neighbor.Visited || tentative_g_score <= neighbor.GScore) { neighbor.SetOptimalPathParent(current); neighbor.GScore = tentative_g_score; neighbor.FScore = neighbor.GScore + this.getFScore(neighbor, goalNode); if (!neighbor.Visited) { neighbor.Visited = true; } openset.Push(neighbor); } } } } } }
public AStarGraphComponent(GameObject parent, AStarGraph graph) : base(parent) { this.Graph = graph; }