Advertisement
Guest User

Game1

a guest
Mar 28th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace Stake_it_Out
  13. {
  14.     public class Game1 : Microsoft.Xna.Framework.Game
  15.     {
  16.         GraphicsDeviceManager graphics;
  17.         SpriteBatch spriteBatch;
  18.        
  19.  
  20.         //Environment Vars
  21.         int numberRegions, treeDensity, regionX, regionY, regionStartX, regionStartY;
  22.         Vector2  regionStartCoords;
  23.  
  24.         //Universal Tools and Input
  25.         Random RandomClass = new Random();
  26.         KeyboardState curKeyboardState, oldKeyboardState;
  27.         public MouseState mouseStateCurrent, mouseStatePrevious;
  28.  
  29.         //States
  30.         Boolean menuState, ingameState, deathState;
  31.         Boolean gameSetup;
  32.  
  33.         //PlayerVars
  34.         Vector2 playerPosition, playerGridPosition;
  35.         int innerTileOffset;
  36.         Player playerObject;
  37.  
  38.         //Tile Vars
  39.         Vector2 newTilePosition, oldTilePosition;
  40.         public int gridWidth, gridHeight, tileWidth, tileHeight;
  41.         int tileNumberX,tileNumberY, gridScope;
  42.  
  43.         public Tile[,] tileArray;
  44.         List<Tile> tileList;
  45.         List<TreeRegion> treeRegionList;
  46.        
  47.         //Internews
  48.         Interface interfaceObject;
  49.        
  50.  
  51.         public Game1()
  52.         {
  53.             graphics = new GraphicsDeviceManager(this);
  54.             Content.RootDirectory = "Content";
  55.         }
  56.  
  57.         //Initialize
  58.         protected override void Initialize()
  59.         {
  60.             //States
  61.             ingameState = true;
  62.             gameSetup = false;
  63.  
  64.             //Tile and Grid Properties
  65.             tileWidth = 30; tileHeight = 30;
  66.             gridWidth = 100; gridHeight = 100;
  67.             gridScope = gridWidth * gridHeight;
  68.             treeDensity = 200;
  69.             tileNumberX= 0;
  70.             tileNumberY = 0;
  71.  
  72.             tileArray = new Tile[gridScope, gridScope];
  73.             tileList = new List<Tile>();
  74.  
  75.             Vector2 emptyVector = Vector2.Zero;
  76.             Vector2 newTilePosition = new Vector2(emptyVector.X-tileWidth, emptyVector.Y);
  77.             oldTilePosition = newTilePosition;
  78.  
  79.             innerTileOffset = 3;
  80.             playerPosition = new Vector2(270 + innerTileOffset, 180 + innerTileOffset);
  81.             playerObject = new Player(playerPosition, GraphicsDevice.Viewport);
  82.            
  83.             //Tree Region Properties
  84.             treeRegionList = new List<TreeRegion>();
  85.  
  86.             //Init Methods
  87.             createTiles();
  88.             createInterface();
  89.             loadGraphicContent();
  90.             createTrees();
  91.  
  92.             IsMouseVisible = true;
  93.             base.Initialize();
  94.         }
  95.  
  96.  
  97.         //Loading Graphics
  98.         protected override void LoadContent()
  99.         {
  100.             spriteBatch = new SpriteBatch(GraphicsDevice);
  101.         }
  102.  
  103.  
  104.         //Update
  105.         protected override void Update(GameTime gameTime)
  106.         {
  107.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  108.                 this.Exit();
  109.             updatePlayer(gameTime);
  110.  
  111.             //Make Consistent Trees
  112.             for (int a=0; a < tileList.Count; a++)
  113.             {
  114.                 tileList[a].maintainTreeLocation();
  115.             }
  116.            
  117.  
  118.             base.Update(gameTime);
  119.         }
  120.  
  121.  
  122.         //Draw
  123.         protected override void Draw(GameTime gameTime)
  124.         {
  125.             GraphicsDevice.Clear(Color.Black);
  126.             spriteBatch.Begin();
  127.  
  128.             //Draw the for loop
  129.             for (int d = 0; d < tileList.Count; d++)
  130.             {
  131.                 tileList[d].Draw(this.spriteBatch);
  132.             }
  133.             playerObject.Draw(this.spriteBatch);
  134.             interfaceObject.Draw(this.spriteBatch);
  135.  
  136.             spriteBatch.End();
  137.             base.Draw(gameTime);
  138.         }
  139.  
  140.  
  141.         //Player and Input
  142.         private void updatePlayer(GameTime gameTime)
  143.         {
  144.             //Keyboard Input
  145.             curKeyboardState = Keyboard.GetState();
  146.  
  147.             if (curKeyboardState.IsKeyDown(Keys.A))
  148.             {
  149.                 if (oldKeyboardState.IsKeyUp(Keys.A))
  150.                 { for (int m = 0; m < tileList.Count; m++)
  151.                 {
  152.                     tileList[m].position.X += tileWidth;
  153.                 }
  154.                 }
  155.             }
  156.             if (curKeyboardState.IsKeyDown(Keys.D))
  157.             {
  158.                 if (oldKeyboardState.IsKeyUp(Keys.D))
  159.                 {
  160.                     for (int m = 0; m < tileList.Count; m++)
  161.                     {
  162.                         tileList[m].position.X -= tileWidth;
  163.                     }
  164.                 }
  165.             }
  166.             if (curKeyboardState.IsKeyDown(Keys.W))
  167.             {
  168.                 if (oldKeyboardState.IsKeyUp(Keys.W))
  169.                 {
  170.                     for (int m = 0; m < tileList.Count; m++)
  171.                     {
  172.                         tileList[m].position.Y += tileHeight;
  173.                     }
  174.                 }
  175.             }
  176.             if (curKeyboardState.IsKeyDown(Keys.S))
  177.             {
  178.                 if (oldKeyboardState.IsKeyUp(Keys.S))
  179.                 {
  180.                     for (int m = 0; m < tileList.Count; m++)
  181.                     {
  182.                         tileList[m].position.Y -= tileWidth;
  183.                     }
  184.                 }
  185.             }
  186.             if (curKeyboardState.IsKeyDown(Keys.Space))
  187.             {
  188.                 if (oldKeyboardState.IsKeyUp(Keys.Space))
  189.                 { }
  190.             }
  191.             oldKeyboardState = curKeyboardState;
  192.  
  193.             //MouseInput
  194.             mouseStateCurrent = Mouse.GetState();
  195.             if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
  196.             {
  197.                 if (mouseStatePrevious.LeftButton != ButtonState.Pressed)
  198.                 {
  199.  
  200.                 }
  201.             }
  202.             mouseStatePrevious = mouseStateCurrent;
  203.         }
  204.  
  205.  
  206.         //Create Tiles
  207.         public void createTiles()
  208.         {
  209.            
  210.             for (int a = 0; a < gridHeight; a++)
  211.             {
  212.                 for (int b = 0; b < gridWidth; b++)
  213.                 {
  214.                     //Row Tiles
  215.                     newTilePosition = new Vector2(oldTilePosition.X + tileWidth, oldTilePosition.Y);
  216.                     tileNumberX+=1;
  217.                     int grassRand = RandomClass.Next(0, 9);
  218.                     Tile tileObject = new Tile(newTilePosition, grassRand, GraphicsDevice.Viewport);
  219.                    
  220.                     int xInt = (int)newTilePosition.X;
  221.                     int yInt = (int)newTilePosition.Y;
  222.                    
  223.                     //Arrayshit
  224.                     tileArray[xInt, yInt] = tileObject;
  225.                    
  226.  
  227.                     tileList.Add(tileObject);
  228.                     oldTilePosition = newTilePosition;
  229.                 }
  230.                 //New Column
  231.                 oldTilePosition = new Vector2(oldTilePosition.X - (gridWidth * tileWidth), oldTilePosition.Y + tileHeight);
  232.             }
  233.  
  234.         }
  235.  
  236.  
  237.         //Create Trees
  238.         public void createTrees()
  239.         {
  240.             numberRegions = (gridHeight * gridWidth) / treeDensity;
  241.             for (int i = 0; i < numberRegions; i++)
  242.             {
  243.                 //Starting Pos
  244.                 int regionStartX = RandomClass.Next(0, 10);
  245.                 regionStartX = regionStartX * 30;
  246.                 int regionStartY = RandomClass.Next(0, 10);
  247.                 regionStartY = regionStartY * 30;
  248.                 regionStartCoords = new Vector2(regionStartX, regionStartY);
  249.  
  250.                 //Length and Width
  251.                 int regionX = RandomClass.Next(1, 10);
  252.                 regionX = regionStartX * 30;
  253.                 int regionY = RandomClass.Next(1, 10);
  254.                 regionY = regionStartX * 30;
  255.  
  256.                 TreeRegion treeRegionObject = new TreeRegion(regionStartCoords, regionX, regionY, GraphicsDevice.Viewport);
  257.                 treeRegionList.Add(treeRegionObject);
  258.             }
  259.             for (int p = 0; p < treeRegionList.Count; p++)
  260.             {
  261.                 treeRegionList[p].createTreess();
  262.                 treeRegionList[p].sendResults();
  263.             }
  264.         }
  265.  
  266.  
  267.         //Create Interface
  268.         public void createInterface()
  269.         {
  270.             interfaceObject = new Interface(GraphicsDevice.Viewport);
  271.         }
  272.  
  273.  
  274.         //Load Graphics
  275.         public void loadGraphicContent()
  276.         {
  277.  
  278.             //Tiles
  279.             for (int a = 0; a < tileList.Count; a++)
  280.             {
  281.                 tileList[a].LoadContent(this.Content, "graphic0", "graphic1", "graphic2", "graphic3", "graphic4", "graphic5", "graphic6", "graphic7", "graphic8", "graphic9","treeGraphic");
  282.             }
  283.  
  284.             //Player
  285.             playerObject.LoadContent(this.Content, "playerGraphic");
  286.  
  287.             //Interface
  288.             interfaceObject.LoadContent(this.Content, "interfaceGraphic", "interfaceGraphic2","largeIcon0","largeIcon1", "smallIcon");
  289.  
  290.         }
  291.     }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement