Advertisement
actuallyjamez

Untitled

Dec 14th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.79 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Audio;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6. using ProjectTellchief.Data.Resource;
  7.  
  8. /// <summary>
  9. /// This is the main type for your game.
  10. /// </summary>
  11. public class Game1 : Game
  12.     {
  13.  
  14.         enum GameState
  15.         {
  16.             MainMenu,
  17.             Gameplay,
  18.             EndOfGame,
  19.             GamePlay,
  20.         }
  21.         GameState _state;
  22.  
  23.         bool playerDied;
  24.  
  25.         bool pushedStartGameButton;
  26.  
  27.         bool pushedMainMenuButton;
  28.  
  29.         bool pushedRestartLevelButton;
  30.  
  31.  
  32.  
  33.         void ResetLevel()
  34.         {
  35.             throw new NotImplementedException();
  36.         }
  37.  
  38.         /// <summary>
  39.         /// The menu assets.
  40.         /// </summary>
  41.        
  42.         private ResourceManager rmInstance;
  43.         private Texture2D background;      
  44.         private Texture2D dylaninbin;
  45.         private Texture2D btnnewgame;
  46.         private Texture2D btnnewgameHover;
  47.         private Texture2D title;
  48.         private Texture2D cursor;
  49.         private Texture2D btnMAIN;
  50.        
  51.  
  52.         private Vector2 cursorPos;
  53.         GraphicsDeviceManager graphics;
  54.         SpriteBatch spriteBatch;
  55.  
  56.         private SoundEffect track;
  57.  
  58.        
  59.  
  60.         public Game1()
  61.         {
  62.             graphics = new GraphicsDeviceManager(this);
  63.             Content.RootDirectory = "Content";
  64.  
  65.  
  66.  
  67.         }
  68.  
  69.  
  70.  
  71.         private AssetHierarchyDefinition GetAssetHierarchy()
  72.         {
  73.             AssetHierarchyDefinition assetDefinition = new AssetHierarchyDefinition();
  74.  
  75.             //Environment assets
  76.             AssetFolder environment = new AssetFolder("Environment", AssetFolderContentType.TEXTURES);
  77.  
  78.             //Language assets
  79.             AssetFolder language = new AssetFolder("Lang", AssetFolderContentType.TEXTFILES);
  80.  
  81.             //Sound assets
  82.             AssetFolder sounds = new AssetFolder("Sounds", AssetFolderContentType.SOUNDS);
  83.             AssetFolder sounds2 = new AssetFolder("Sound");
  84.             AssetFolder music = new AssetFolder("Music");
  85.  
  86.             //Texture assets
  87.             AssetFolder textures = new AssetFolder("Textures", AssetFolderContentType.TEXTURES);
  88.             AssetFolder tiles = new AssetFolder("Tile");
  89.             AssetFolder items = new AssetFolder("Item");
  90.             AssetFolder entities = new AssetFolder("Entity");
  91.             AssetFolder gui = new AssetFolder("Gui", AssetFolderContentType.TEXTURES);
  92.  
  93.  
  94.             textures.AddSubFolder(tiles);
  95.             textures.AddSubFolder(items);
  96.             textures.AddSubFolder(entities);
  97.  
  98.             sounds.AddSubFolder(sounds2);
  99.             sounds.AddSubFolder(music);
  100.  
  101.             //add all top level folders to the asset structure hierarchy
  102.             assetDefinition.AddFolder(environment);
  103.             assetDefinition.AddFolder(sounds);
  104.             assetDefinition.AddFolder(textures);
  105.             assetDefinition.AddFolder(gui);
  106.  
  107.             return assetDefinition;
  108.         }
  109.  
  110.  
  111.        
  112.  
  113.         /// <summary>
  114.         /// Allows the game to perform any initialization it needs to before starting to run.
  115.         /// This is where it can query for any required services and load any non-graphic
  116.         /// related content.  Calling base.Initialize will enumerate through any components
  117.         /// and initialize them as well.
  118.         /// </summary>
  119.         protected override void Initialize()
  120.  
  121.         {
  122.             IsMouseVisible = false;
  123.             // TODO: Add your initialization logic here
  124.             base.Initialize();
  125.             const int defaultWidth = 1280, defaultHeight = 720;
  126.            
  127.             int currentWidth = GraphicsDevice.Viewport.Width,
  128.             currentHeight = GraphicsDevice.Viewport.Height;
  129.  
  130.             Vector2 scale = new Vector2(currentWidth / defaultWidth,
  131.                                         currentHeight / defaultHeight);
  132.  
  133.             _state = GameState.MainMenu;
  134.             rmInstance = new ResourceManager(graphics, GetAssetHierarchy());
  135.         rmInstance.Initialize();
  136.  
  137.         }
  138.  
  139.         /// <summary>
  140.         /// LoadContent will be called once per game and is the place to load
  141.         /// all of your content.
  142.         /// </summary>
  143.         protected override void LoadContent()
  144.         {
  145.  
  146.  
  147.               // Create a new SpriteBatch, which can be used to draw textures   .
  148.             spriteBatch = new SpriteBatch(GraphicsDevice);
  149.             background = Content.Load<Texture2D>("Controls/background"); // change these names to the names of your images
  150.             dylaninbin = Content.Load<Texture2D>("Controls/dylan_in_container"); // change these names to the names of your images
  151.             btnnewgame = Content.Load<Texture2D>("Controls/New_Game_LBlue"); // change these names to the names of your images
  152.             btnnewgameHover = Content.Load<Texture2D>("Controls/New_Game_Yellow"); // change these names to the names of your images
  153.             title = Content.Load<Texture2D>("Controls/title"); // change these names to the names of your images
  154.             cursor = Content.Load<Texture2D>("Controls/cursor");
  155.             //track = Content.Load<SoundEffect>("audio/soundtrack/sadface");
  156.             track = (SoundEffect)rmInstance.GetResource("sounds.music.sadface");
  157.         }
  158.  
  159.         /// <summary>
  160.         /// Allows the game to run logic such as updating the world,
  161.         /// checking for collisions, gathering input, and playing audio.
  162.         /// </summary>
  163.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  164.         protected override void Update(GameTime gameTime)
  165.         {
  166.  
  167.  
  168.  
  169.  
  170.  
  171.             // For Mobile devices, this logic will close the Game when the Back button is pressed
  172.             // Exit() is obsolete on iOS
  173. #if !__IOS__ && !__TVOS__
  174.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  175.             {
  176.                 Exit();
  177.             }
  178. #endif     
  179.  
  180.  
  181.             base.Update(gameTime);
  182.                 switch (_state)
  183.                 {
  184.                 case GameState.MainMenu:
  185.  
  186.                     UpdateMainMenu(gameTime);
  187.                     break;
  188.                 case GameState.Gameplay:
  189.  
  190.                     UpdateGameplay(gameTime);
  191.                     break;
  192.                 case GameState.EndOfGame:
  193.  
  194.                     UpdateEndOfGame(gameTime);
  195.                     break;
  196.                 }
  197.  
  198.  
  199.  
  200.  
  201.  
  202.    
  203.             base.Update(gameTime);
  204.         }
  205.  
  206.         public bool isHovered { get; private set; }
  207.  
  208.         public bool isClicked { get; private set; }
  209.  
  210.  
  211.  
  212.  
  213.         void UpdateMainMenu(GameTime deltaTime)
  214.         {
  215.             btnMAIN = btnnewgame;
  216.             //if (Mouse.GetState().LeftButton == ButtonState.Pressed)
  217.          //   {
  218.          //       btnMAIN = btnnewgameHover;
  219.          //   }
  220.  
  221.            
  222.             MouseState mouseState = Mouse.GetState();
  223.             cursorPos = new Vector2(mouseState.X, mouseState.Y);
  224.            
  225.          
  226.             //if (pushedStartGameButton)
  227.             //_state = GameState.GamePlay;
  228.        
  229.             //var mousePoint = new Point(mouseState.X, mouseState.Y);
  230.             var rectangle = new Rectangle(100, 150, btnnewgame.Width/2, btnnewgame.Height/2);
  231.              if (rectangle.Contains(cursorPos))
  232.             {
  233.                 btnMAIN = btnnewgameHover;
  234.                 isClicked = mouseState.LeftButton == ButtonState.Pressed;
  235.             }
  236.             else
  237.             {
  238.                 btnMAIN = btnnewgame;
  239.                 isHovered = false;
  240.                 isClicked = false;
  241.             }
  242.  
  243.         }
  244.  
  245.         void UpdateGameplay(GameTime deltaTime)
  246.         {
  247.         // Respond to user actions in the game.
  248.         // Update enemies
  249.         // Handle collisions
  250.             if (playerDied)
  251.         _state = GameState.EndOfGame;
  252.         }
  253.  
  254.         void UpdateEndOfGame(GameTime deltaTime)
  255.         {
  256.             // Update scores
  257.             // Do any animations, effects, etc for getting a high score
  258.             // Respond to user input to restart level, or go back to main menu
  259.             if (pushedMainMenuButton)
  260.                 _state = GameState.MainMenu;
  261.             else if (pushedRestartLevelButton)
  262.             {
  263.                 ResetLevel();
  264.                 _state = GameState.Gameplay;
  265.          }
  266.         }
  267.  
  268.         /// <summary>
  269.         /// This is called when the game should draw itself.
  270.         /// </summary>
  271.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  272.         protected override void Draw(GameTime gameTime)
  273.         {
  274.  
  275.  
  276.             base.Draw(gameTime);
  277.             switch (_state)
  278.             {
  279.                 case GameState.MainMenu:
  280.                     DrawMainMenu(gameTime);
  281.                     break;
  282.                 case GameState.Gameplay:
  283.                     DrawGameplay(gameTime);
  284.                     break;
  285.                 case GameState.EndOfGame:
  286.                     DrawEndOfGame(gameTime);
  287.                     break;
  288.            
  289.             }
  290.         }
  291.  
  292. void DrawMainMenu(GameTime deltaTime)
  293. {
  294.             spriteBatch.Begin();
  295.             spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
  296.             spriteBatch.Draw(dylaninbin, new Rectangle(0, 0, 800, 480), Color.White);
  297.             #pragma warning disable CS0618 // Type or member is obsolete
  298.             spriteBatch.Draw(btnMAIN, new Vector2(100, 150), scale: new Vector2((float)0.5, (float)0.5));
  299.             #pragma warning restore CS0618 // Type or member is obsolete
  300.             #pragma warning disable CS0618 // Type or member is obsolete
  301.             spriteBatch.Draw(title, new Vector2(95, 25), scale: new Vector2((float)0.6, (float)0.6));
  302.             #pragma warning restore CS0618 // Type or member is obsolete
  303.             #pragma warning disable CS0618 // Type or member is obsolete
  304.             spriteBatch.Draw(cursor, cursorPos, scale: new Vector2((float)0.1, (float)0.1));
  305.             #pragma warning restore CS0618 // Type or member is obsolete
  306.             spriteBatch.End();
  307. }
  308.  
  309. void DrawGameplay(GameTime deltaTime)
  310. {
  311.     // Draw the background the level
  312.     // Draw enemies
  313.     // Draw the player
  314.     // Draw particle effects, etc
  315. }
  316.  
  317. void DrawEndOfGame(GameTime deltaTime)
  318. {
  319.             spriteBatch.Begin();
  320.             spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
  321.             spriteBatch.End();
  322.  
  323.     // Draw text and scores
  324.     // Draw menu for restarting level or going back to main menu
  325. }
  326.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement