public void FinalizeGame() { //Pop all menus menuContainer.RemoveAllMenus(); player = null; camera.DetachEntity(); camera.viewedSurface = null; surfaceContainer = null; input.ClearGameSubscribers(); renderer.DetachGameWorld(); StaticSoundManager.StopAmbience(); entityUpdateSystem.ResetSystem(); }
public void StartMenu() { StaticSoundManager.PlayMusic(); input.menuFactory.CreateMainMenu(); while (window.IsOpen && gameState == GameState.mainMenu) { window.Clear(); window.DispatchEvents(); input.Update(gameState); renderer.RenderGUI(window, camera); window.Display(); } StaticSoundManager.StopMusic(); }
//Entry point for the program static void Main(string[] args) { Program program = new Program(); program.InitializeWindow(); //would be nice to implement a loading screen at this point program.InitializeResources(); //end loading screen //launch sound thread before entering main program StaticSoundManager.LoadSounds(); while (program.window.IsOpen) { if (program.gameState == GameState.mainMenu) { program.StartMenu(); } else if (program.gameState == GameState.inGame) { program.RunGame(); } } #region debug code DELETION /* * float worldX = camera.GetGameView().Center.X; * float worldY = camera.GetGameView().Center.Y; * int[] cXY = SurfaceContainer.WorldToChunkCoords(worldX, worldY); * biomeText.DisplayedString = "Biome: " + tileCollection.GetTerrainTileName(surfaceContainer.GetTileFromWorld(worldX, worldY)); * biomeText.Position = new Vector2f(0, 0); * fpsText.DisplayedString = "FPS/TPS: " + fps.ToString(); * fpsText.Position = new Vector2f(0, 32); * coordinates.DisplayedString = "World Coordinates: " + worldX + ", " + worldY + * "\nChunk Coordinates:" + cXY[0] + ", " + cXY[1] + * "\nTile Coordinates:" + (int)(worldX / Props.tileSize) % Props.chunkSize + ", " + (int)(worldY / Props.tileSize) % Props.chunkSize + * "\nChunk Index:" + SurfaceContainer.WorldToChunkIndex(worldX, worldY); * coordinates.Position = new Vector2f(0, 64); */ #endregion debug code DELETIOn }
/// <summary> /// TODO: Add inheritance structure /// </summary> public override void Update(EntityCollection entityCollection, ItemCollection itemCollection) { for (int i = 0; i < dropItems.Count; i++) { for (int j = 0; j < dropItems[i].count; j++) { entityCollection.InstantiatePrototype(dropItems[i].item.name + "Item", position.Copy(), surface); } } dropItems.Clear(); if (playerState == PlayerState.Mining) { if (selectedEntity != null && selectedEntity.minable == true) { if (miningProgress % miningSoundFrequency == 0) { StaticSoundManager.PlaySound(selectedEntity.position, selectedEntity.miningSounds); } miningProgress += 1; if (miningProgress > selectedEntity.miningProps.miningTime) { StaticSoundManager.PlaySound(position, new string[] { "Pickup" }); selectedEntity.OnMined(this, itemCollection, entityCollection); selectedEntity = null; miningProgress = 0; playerState = PlayerState.Idle; } } else { playerState = PlayerState.Idle; } } if (playerState == PlayerState.Moving) { BoundingBox.ApplyPhysicalCollision(this, velocity); if (velocity.x != 0 || velocity.y != 0) { rotation = velocity.GetRotation() + 180.0f; } walking.SetRotation(rotation); walking.Update(); walking.animationSpeed = 60 / velocity.GetMagnitude(); velocity.Set(0, 0); radialLight.Update(); directionalLight.Update(); directionalLight.SetDirection(270 + rotation); } //Light logic if (surface.timeOfDay > surface.timeOfMidday / 2 - surface.lengthOfNight) { radialLight.on = true; directionalLight.on = true; } if (surface.timeOfDay > surface.timeOfMidday / 2 + surface.lengthOfNight) { radialLight.on = false; directionalLight.on = false; } }
public void RunGame() { InitializeGame(); clock = new Clock(); fpsQueue = new Queue <float>(10); frame = 0; Pathing pathTest = new Pathing(tileCollection); Vector2f target = new Vector2f(2048 * 3, 2048 * 3); RectangleShape targetBox = new RectangleShape(new Vector2f(32, 32)); targetBox.Position = target; targetBox.FillColor = Color.Green; PathNode path = pathTest.GetPath(surfaceContainer, new Vector2(2048, 2048), new Vector2(2048 * 3, 2048 * 3), 500, Base.CollisionLayer.TerrainSolid, Base.CollisionLayer.TerrainSolid | Base.CollisionLayer.Terrain, 2.0f); for (int i = 0; i < 30; i++) { fpsQueue.Enqueue(60.0f); } while (window.IsOpen && (gameState == GameState.inGame || gameState == GameState.paused)) { StaticSoundManager.PlayMusic(); StaticSoundManager.PlayAmbience(); //Check fps fps = 1.0f / clock.ElapsedTime.AsSeconds(); fpsQueue.Enqueue(fps); fpsQueue.Dequeue(); clock.Restart(); //prepare for drawing and dispatch window events window.Clear(); window.DispatchEvents(); //update input input.Update(gameState); //Do not update game world if game is paused if (gameState != GameState.paused) { //update surface surfaceContainer.Update(); //update entities entityUpdateSystem.UpdateEntities(entityCollection, itemCollection); entityUpdateSystem.AddNewEntities(); entityUpdateSystem.DestroyEntities(); } //update camera camera.Update(); //drawing game world (terrain, entities) if (camera.viewedSurface != null) { renderer.RenderWorld(window, camera, camera.viewedSurface); if (camera.focusedEntity is Player) { renderer.RenderSelectionBox(window, camera, (Player)camera.focusedEntity, textureAtlases); renderer.RenderHeldDrawable(window, camera, (Player)camera.focusedEntity, entityCollection, input); } window.SetView(camera.GetGameView()); //debug pathtesting if (path != null) { pathTest.DrawPath(window, path); } window.Draw(targetBox); //end debug pathtesting } //drawing menus (main menu, pause, ingame, etc) renderer.RenderGUI(window, camera); //Draw the player's held item (very ugly to have to put it here but couldnt think of anything better) if (camera.focusedEntity is Player) { renderer.RenderHeldItem(window, camera, ((Player)camera.focusedEntity).heldItem, input); } //Cull far away vertexarrays from renderer cache renderer.CheckCullVertexCache(camera, surfaceContainer); window.Display(); frame++; } FinalizeGame(); }