public void changeState(Gamestates state) { CurrentState = state; switch (CurrentState) { case Gamestates.ready: onReadyState(); break; case Gamestates.game: onGameState(); break; case Gamestates.pause: onPauseState(); break; case Gamestates.resume: onResumeState(); break; case Gamestates.end: onEndState(); break; default: break; } }
void OnCollisionEnter2D(Collision2D collision) { if (collision.collider.tag == "HostileTerrain") { gameStates = Gamestates.dead; } if (collision.collider.tag == "Terrain" && rb.velocity.y == 0) { grounded = true; } }
public MainGame() : base("Zeldo") { glClearColor(0, 0, 0, 1); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glPrimitiveRestartIndex(Constants.RestartIndex); glfwSetInputMode(window.Address, GLFW_CURSOR, GLFW_CURSOR_DISABLED); Language.Reload(Languages.English); canvas = new Canvas(); sb = new SpriteBatch(); mainTarget = new RenderTarget(Resolution.RenderWidth, Resolution.RenderHeight, RenderTargetFlags.Color | RenderTargetFlags.Depth); mainSprite = new Sprite(mainTarget, null, Alignments.Left | Alignments.Top); mainSprite.Mods = SpriteModifiers.FlipVertical; // The first loop is created manually. Others are created via gamestate messages. currentState = Gamestates.Gameplay; nextState = currentState; activeLoop = CreateLoop(currentState); MessageSystem.Subscribe(this, CoreMessageTypes.Keyboard, (messageType, data, dt) => { ProcessKeyboard((KeyboardData)data); }); MessageSystem.Subscribe(this, CoreMessageTypes.ResizeWindow, (messageType, data, dt) => { mainSprite.ScaleTo(Resolution.WindowWidth, Resolution.WindowHeight); }); MessageSystem.Subscribe(this, CustomMessageTypes.Gamestate, (messageType, data, dt) => { var state = (Gamestates)data; Debug.Assert(state != Gamestates.Splash, "Can't transition to the splash loop."); // This implementation means that if multiple gamestate messages are sent on a single frame, the last // state will take priority (although that should never happen in practice). if (state != currentState) { nextState = state; } }); // Calling this function here is required to ensure that all classes receive initial resize messages. MessageSystem.ProcessChanges(); MessageSystem.Send(CoreMessageTypes.ResizeRender, Resolution.RenderDimensions); MessageSystem.Send(CoreMessageTypes.ResizeWindow, Resolution.WindowDimensions); }
void Respawn() { if (currentCheckpoint != null) { transform.position = currentCheckpoint.position; } else if (currentCheckpoint == null) { transform.position = GameObject.Find("Spawn").GetComponent <Transform>().position; } rb.velocity = Vector2.zero; gameStates = Gamestates.alive; }
protected override void Update(float dt) { activeLoop.Update(dt); canvas.Update(dt); // Gamestate changes don't apply until the end of the current frame. if (nextState != currentState) { activeLoop = CreateLoop(nextState); currentState = nextState; } MessageSystem.ProcessChanges(); }
void OnCollisionEnter2D(Collision2D collision) { if (collision.collider.tag == "HostileTerrain") { gameStates = Gamestates.dead; } if (collision.collider.tag == "Terrain" && rb.velocity.y == 0) { grounded = true; } if (collision.collider.tag == "Player") { Physics2D.IgnoreCollision(collision.collider, collision.otherCollider, true); } }
private void SwitchGamestate(Gamestates gamestate) { this.gamestate = gamestate; switch (gamestate) { case Gamestates.SPLASH: splashScreen = new SplashScreen(); break; case Gamestates.GAMEPLAY: InitializeGameplay(); break; case Gamestates.EMITTER_TESTING: InitializeEmitterTesting(); break; } }
private GameLoop CreateLoop(Gamestates state) { GameLoop loop = null; switch (state) { case Gamestates.Gameplay: loop = new GameplayLoop((TitleLoop)activeLoop); break; case Gamestates.Title: loop = new TitleLoop(); break; case Gamestates.Splash: loop = new SplashLoop(); break; } loop.Canvas = canvas; loop.SpriteBatch = sb; loop.Initialize(); return(loop); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width; graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height; graphics.IsFullScreen = false; graphics.ApplyChanges(); gamestates = Gamestates.startmenu; // Inladning av alla textures #region Inladning av textures Texture2D playerSprite = Content.Load <Texture2D>("piggy"); Texture2D playerCrouch = Content.Load <Texture2D>("Crouch"); flyingsprite = Content.Load <Texture2D>("snow123"); objectSprite = Content.Load <Texture2D>("flyingGrasBlock_100x30"); damagesprite = Content.Load <Texture2D>("grasBlock_100x60"); background = Content.Load <Texture2D>("Forest-31"); munkSprite = Content.Load <Texture2D>("snow123"); backgroundWidth = background.Width; backgroundTest = new Vector2(0, 0); //Meny bakgrunder #region Meny Bakgrunder startmenuTexture = Content.Load <Texture2D>("title_screen_almost"); shopmenuTexture = Content.Load <Texture2D>("liten_shopscreen_test"); pausemenuTexture = Content.Load <Texture2D>("liten_pausescreen_test"); levelmenuBackground = Content.Load <Texture2D>("levelmeny_bakgrund_utan"); #endregion //Alla knappterturers #region Knapptexturers playButton = Content.Load <Texture2D>("playButton"); playButtonActive = Content.Load <Texture2D>("playButton_active"); shopButton = Content.Load <Texture2D>("shopButton"); shopButtonActive = Content.Load <Texture2D>("shopButton_active"); exitButton = Content.Load <Texture2D>("exitButton"); exitButtonActive = Content.Load <Texture2D>("exitButton_active"); resumeButton = Content.Load <Texture2D>("resumeButton"); resumeButtonActive = Content.Load <Texture2D>("resumeButton_active"); leaveButton = Content.Load <Texture2D>("leaveButton"); leaveButtonActive = Content.Load <Texture2D>("leaveButton_active"); backButton = Content.Load <Texture2D>("backButton"); backButtonActive = Content.Load <Texture2D>("backButton_active"); buyButton = Content.Load <Texture2D>("buyButton"); buyButtonActive = Content.Load <Texture2D>("buyButton_active"); level1Texture = Content.Load <Texture2D>("level-1"); level1ZoomTexture = Content.Load <Texture2D>("level-1-zoom"); level2Texture = Content.Load <Texture2D>("level-2"); level3Texture = Content.Load <Texture2D>("level-3"); level4Texture = Content.Load <Texture2D>("level-4"); #endregion buyJump = Content.Load <SpriteFont>("BuyJump"); #endregion startmenu = new Startmenu(startmenuTexture, playButton, playButtonActive, shopButton, shopButtonActive, exitButton, exitButtonActive); shopmenu = new Shopmenu(shopmenuTexture, buyButton, buyButtonActive, backButton, backButtonActive); levelMenu = new LevelMenu(levelmenuBackground, backButton, backButtonActive); pausemenu = new Pausemenu(pausemenuTexture, resumeButton, resumeButtonActive, leaveButton, leaveButtonActive); player = new Player(playerSprite, playerCrouch); base.Initialize(); }
/// <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) { // De olika if-satserna nedan som anroppar de olika "Update" metoderna // är till för att när man till exempel spelaren inte ska fortsätta springa runt // när man har pausat spelet. //Kör "testleveln och playern" if (gamestates == Gamestates.inGame) #region Allt i test level och player { IsMouseVisible = false; gamestates = lvl1.Update(gameTime, player, effect); camera.Update(player.position); lvl1.Update(gameTime, player, effect); } #endregion //Lämnar spelet if (gamestates == Gamestates.exitgame) { Exit(); } //Visar start menyn if (gamestates == Gamestates.startmenu) { IsMouseVisible = true; gamestates = startmenu.Update(); if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Delete)) { Exit(); } } //Visar level menyn if (gamestates == Gamestates.levelmenu) { IsMouseVisible = true; gamestates = levelMenu.Update(); } //Visar paus menyn if (gamestates == Gamestates.pausemenu) { IsMouseVisible = true; gamestates = pausemenu.Update(); } //Visar shop menyn if (gamestates == Gamestates.shopmenu) { IsMouseVisible = true; gamestates = shopmenu.Update(player); } // TODO: Add your update logic here base.Update(gameTime); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width; graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height; graphics.IsFullScreen = false; graphics.ApplyChanges(); gamestates = Gamestates.startmenu; // Inladning av alla textures #region Inladning av textures playerSprite = Content.Load <Texture2D>("piggy"); Texture2D playerCrouch = Content.Load <Texture2D>("Crouch"); healthTexture = Content.Load <Texture2D>("health"); grenSprite = Content.Load <Texture2D>("GREN"); flyingsprite = Content.Load <Texture2D>("flygande_gren"); groundBlockTexture = Content.Load <Texture2D>("srort_block"); snowGroundTexture = Content.Load <Texture2D>("snwo_groundblock"); damagesprite = Content.Load <Texture2D>("Mario_runing"); background = Content.Load <Texture2D>("Forest-31"); munkSprite = Content.Load <Texture2D>("sockerMunk_texture"); backgroundWidth = background.Width; backgroundTest = new Vector2(0, 0); rainTexture = Content.Load <Texture2D>("rain"); snowTexture = Content.Load <Texture2D>("big_snowflake2"); //Meny bakgrunder #region Meny Bakgrunder Texture2D startmenuTexture = Content.Load <Texture2D>("title_screen_almost"); Texture2D shopmenuTexture = Content.Load <Texture2D>("new_shop_screen"); Texture2D pausemenuTexture = Content.Load <Texture2D>("liten_pausescreen_test"); Texture2D levelmenuTexture = Content.Load <Texture2D>("levelmeny_bakgrund_utan"); Texture2D gameOverMenuTexture = Content.Load <Texture2D>("new_gameOver_screen"); Texture2D youWinMenuTexture = Content.Load <Texture2D>("wining_screen"); #endregion //Alla knappterturers #region Knapptexturers playButton = Content.Load <Texture2D>("playButton"); playButtonActive = Content.Load <Texture2D>("playButton_active"); Texture2D shopButton = Content.Load <Texture2D>("shopButton"); Texture2D shopButtonActive = Content.Load <Texture2D>("shopButton_active"); Texture2D exitButton = Content.Load <Texture2D>("exitButton"); Texture2D exitButtonActive = Content.Load <Texture2D>("exitButton_active"); Texture2D resumeButton = Content.Load <Texture2D>("resumeButton"); Texture2D resumeButtonActive = Content.Load <Texture2D>("resumeButton_active"); leaveButton = Content.Load <Texture2D>("leaveButton"); leaveButtonActive = Content.Load <Texture2D>("leaveButton_active"); backButton = Content.Load <Texture2D>("backButton"); backButtonActive = Content.Load <Texture2D>("backButton_active"); Texture2D buyButton = Content.Load <Texture2D>("buyButton"); Texture2D buyButtonActive = Content.Load <Texture2D>("buyButton_active"); Texture2D easyButton = Content.Load <Texture2D>("easy_button"); Texture2D easyButtonActive = Content.Load <Texture2D>("easy_button_active"); Texture2D normalButton = Content.Load <Texture2D>("normal_button"); Texture2D normalButtonActive = Content.Load <Texture2D>("normal_button_active"); Texture2D hardButton = Content.Load <Texture2D>("hard_button"); Texture2D hardButtonActive = Content.Load <Texture2D>("hard_button_active"); Texture2D sonicButton = Content.Load <Texture2D>("sonic_button"); Texture2D sonicButtonActive = Content.Load <Texture2D>("sonic_button_active"); Texture2D level1Texture = Content.Load <Texture2D>("level1_button"); Texture2D level1TextureHover = Content.Load <Texture2D>("level1_button_active"); Texture2D level2Texture = Content.Load <Texture2D>("level2_button"); Texture2D level2TextureHover = Content.Load <Texture2D>("level2_button_active"); Texture2D level3Texture = Content.Load <Texture2D>("level3_button"); Texture2D level3TextureHover = Content.Load <Texture2D>("level3_button_active"); Texture2D level4Texture = Content.Load <Texture2D>("level4_button"); Texture2D level4TextureHover = Content.Load <Texture2D>("level4_button_active"); #endregion comicSansFont = Content.Load <SpriteFont>("BuyJump"); #endregion startmenu = new Startmenu(startmenuTexture, playButton, playButtonActive, shopButton, shopButtonActive, exitButton, exitButtonActive); shopmenu = new Shopmenu(shopmenuTexture, buyButton, buyButtonActive, backButton, backButtonActive); levelMenu = new LevelMenu(playButton, playButtonActive, levelmenuTexture, level1Texture, level1TextureHover, level2Texture, level2TextureHover, level3Texture, level3TextureHover, level4Texture, level4TextureHover, backButton, backButtonActive, easyButton, easyButtonActive, normalButton, normalButtonActive, hardButton, hardButtonActive, sonicButton, sonicButtonActive); pausemenu = new Pausemenu(pausemenuTexture, resumeButton, resumeButtonActive, leaveButton, leaveButtonActive); player = new Player(playerSprite, playerCrouch, healthTexture); gameOverMenu = new GameOverMenu(gameOverMenuTexture, backButton, backButtonActive, playButton, playButtonActive); youWinMenu = new YouWinMenu(youWinMenuTexture, backButton, backButtonActive); base.Initialize(); }
/// <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) { //All kod i regonen nedan alltså mellan de två gröna sträcken är av Kilian //=========================================================================================================================== #region Kilians del i Game1 // De olika if-satserna nedan som anroppar de olika "Update" metoderna // är till för att när man till exempel spelaren inte ska fortsätta springa runt // när man har pausat spelet. //Updaterar "testleveln och playern" if (gamestates == Gamestates.inGame) #region Allt i test level och player { IsMouseVisible = false; gamestates = lvl1.Update(gameTime, player, effect, eatingMunk); camera.Update(player.position); lvl1.Update(gameTime, player, effect, eatingMunk); } #endregion //Lämnar spelet if (gamestates == Gamestates.exitgame) { Exit(); } //Updaterar start menyn if (gamestates == Gamestates.startmenu) { IsMouseVisible = true; gamestates = startmenu.Update(); if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Delete)) { Exit(); } } //Updaterar level menyn if (gamestates == Gamestates.levelmenu) { IsMouseVisible = true; gamestates = levelMenu.Update(player); } //Updaterar paus menyn if (gamestates == Gamestates.pausemenu) { IsMouseVisible = true; gamestates = pausemenu.Update(); if (gamestates == Gamestates.startmenu) { //Nollställer lvl1 när man lämnar spelet. lvl1 = new Level1(snowGroundTexture, snowTexture, rainTexture, player, groundBlockTexture, damagesprite, munkSprite, grenSprite, flyingsprite); //Nollställer player = new Player(playerSprite, playerSprite, healthTexture); } } //Updaterar shop menyn if (gamestates == Gamestates.shopmenu) { IsMouseVisible = true; gamestates = shopmenu.Update(player); } //Updaterar gameover menyn if (gamestates == Gamestates.gameOverMenu) { //Nollställertt lvl1 när man har förlorat. lvl1 = new Level1(snowGroundTexture, snowTexture, rainTexture, player, groundBlockTexture, damagesprite, munkSprite, grenSprite, flyingsprite); //Nollställer playern när man har förlorat. player = new Player(playerSprite, playerSprite, healthTexture); IsMouseVisible = true; gamestates = gameOverMenu.Update(player); } if (gamestates == Gamestates.youWinMenu) { IsMouseVisible = true; gamestates = youWinMenu.Update(); //Nollställer lvl1 när man lämnar spelet. lvl1 = new Level1(snowGroundTexture, snowTexture, rainTexture, player, groundBlockTexture, damagesprite, munkSprite, grenSprite, flyingsprite); //Nollställer player = new Player(playerSprite, playerSprite, healthTexture); } // TODO: Add your update logic here base.Update(gameTime); }