Changeset 1758
- Timestamp:
- 2011-06-08 14:50:49 (12 years ago)
- Location:
- 2011/23/sijoseha/AdventureGame/AdventureGame
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
2011/23/sijoseha/AdventureGame/AdventureGame/AdventureGame.cs
r1749 r1758 20 20 public class AdventureGame : Microsoft.Xna.Framework.Game 21 21 { 22 #region PublicStuff 23 22 24 GraphicsDeviceManager graphics; 23 25 … … 38 40 Viewport mapView, hudView, miniMapView; 39 41 40 Button saveButton;42 #endregion 41 43 42 44 public AdventureGame() … … 54 56 protected override void Initialize() 55 57 { 56 57 58 //graphics.IsFullScreen = true;59 //graphics.ApplyChanges();60 58 IsMouseVisible = true; 61 59 base.Initialize(); … … 70 68 // Create a new SpriteBatch, which can be used to draw textures. 71 69 spriteBatch = new SpriteBatch(GraphicsDevice); 70 71 LoadTextures(); 72 72 basicFont = Content.Load<SpriteFont>("Basicfont"); 73 selectionBox = Content.Load<Texture2D>("selectionbox"); 74 tileImage = Content.Load<Texture2D>("tilet"); 75 miniMapBorders = Content.Load<Texture2D>("miniMapBorders"); 76 77 mapView = new Viewport(0, 0, 78 graphics.PreferredBackBufferWidth - 200, 79 graphics.PreferredBackBufferHeight); 80 hudView = new Viewport(graphics.PreferredBackBufferWidth - 200, 81 0, 200, graphics.PreferredBackBufferHeight); 82 miniMapView = new Viewport(graphics.PreferredBackBufferWidth - 200, 83 graphics.PreferredBackBufferHeight - 200, 200, 200); 84 85 gameHUD = new HUD(); 86 gameHUD.ViewPort = hudView; 87 gameHUD.AddButton(new Button(new Rectangle(10, 10, 150, 40), "Save", Color.YellowGreen, Color.Red, GraphicsDevice)); 88 89 gameHUD.Buttons[0].Clicked += new ButtonClickedHandler(SaveMap); 90 91 gameMap = new Map(this, tileImage, TILE_SIZE, 200, 200); 92 gameHUD.AddMiniMap(new MiniMap(gameMap, miniMapView, miniMapBorders)); 73 74 CreateViews(); 75 gameMap = new Map(this, tileImage, TILE_SIZE, 20, 20); 76 CreateHUD(); 77 93 78 gameCamera = new Camera(mapView); 94 79 } … … 102 87 // TODO: Unload any non ContentManager content here 103 88 } 89 90 #region Updating 104 91 105 92 /// <summary> … … 114 101 this.Exit(); 115 102 103 UpdateMouse(); 104 UpdateKeyboard(); 105 106 base.Update(gameTime); 107 } 108 109 private void UpdateMouse() 110 { 116 111 mouseState = Mouse.GetState(); 117 keyState = Keyboard.GetState();118 112 119 113 if (previousMouseState.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed) … … 139 133 { 140 134 if (mousePos.Intersects(button.Boundaries)) 141 button.Pressed(); 135 if (button.Selectable) 136 gameHUD.Select(button); 137 else 138 button.Pressed(); 142 139 } 143 140 } 144 141 } 145 142 143 previousMouseState = mouseState; 144 } 145 146 private void UpdateKeyboard() 147 { 148 keyState = Keyboard.GetState(); 146 149 if (keyState.IsKeyDown(Keys.D1) && previousKeyState.IsKeyUp(Keys.D1)) 147 150 gameMap.ChangeTileTo(TileType.Grass); … … 161 164 if (keyState.IsKeyDown(Keys.Down)) 162 165 gameCamera.Move(new Vector2(0, 5)); 163 previousMouseState = mouseState; 166 164 167 previousKeyState = keyState; 165 base.Update(gameTime); 166 } 168 } 169 170 private void LoadTextures() 171 { 172 selectionBox = Content.Load<Texture2D>("selectionbox"); 173 tileImage = Content.Load<Texture2D>("tilet"); 174 miniMapBorders = Content.Load<Texture2D>("miniMapBorders"); 175 } 176 #endregion 177 178 #region Drawing 167 179 168 180 /// <summary> … … 173 185 { 174 186 GraphicsDevice.Clear(Color.CornflowerBlue); 175 GraphicsDevice.Viewport = mapView; 176 spriteBatch.Begin(SpriteSortMode.Deferred, 177 BlendState.AlphaBlend, 178 null,null,null,null,gameCamera.getTransformation(mapView)); 179 gameMap.Draw(spriteBatch, gameCamera); 180 spriteBatch.End(); 181 GraphicsDevice.Viewport = gameHUD.ViewPort; 182 spriteBatch.Begin(); 183 gameHUD.Draw(spriteBatch); 184 spriteBatch.End(); 187 DrawMap(); 188 DrawHUD(); 189 DrawMiniMap(); 190 base.Draw(gameTime); 191 } 192 193 private void DrawMiniMap() 194 { 185 195 GraphicsDevice.Viewport = miniMapView; 186 196 spriteBatch.Begin(SpriteSortMode.Deferred, … … 190 200 spriteBatch.End(); 191 201 spriteBatch.Begin(); 192 spriteBatch.Draw(gameHUD.MiniMap.Borders, new Rectangle(0,0,200,200), Color.White); 193 spriteBatch.End(); 194 base.Draw(gameTime); 202 spriteBatch.Draw(gameHUD.MiniMap.Borders, new Rectangle(0, 0, 200, 200), Color.White); 203 spriteBatch.End(); 204 } 205 206 private void DrawHUD() 207 { 208 GraphicsDevice.Viewport = gameHUD.ViewPort; 209 spriteBatch.Begin(); 210 gameHUD.Draw(spriteBatch); 211 spriteBatch.End(); 212 } 213 214 private void DrawMap() 215 { 216 GraphicsDevice.Viewport = mapView; 217 spriteBatch.Begin(SpriteSortMode.Deferred, 218 BlendState.AlphaBlend, 219 null, null, null, null, gameCamera.getTransformation(mapView)); 220 gameMap.Draw(spriteBatch, gameCamera); 221 spriteBatch.End(); 222 } 223 224 #endregion 225 226 private void CreateViews() 227 { 228 mapView = new Viewport(0, 0, 229 graphics.PreferredBackBufferWidth - 200, 230 graphics.PreferredBackBufferHeight); 231 hudView = new Viewport(graphics.PreferredBackBufferWidth - 200, 232 0, 200, graphics.PreferredBackBufferHeight); 233 miniMapView = new Viewport(graphics.PreferredBackBufferWidth - 200, 234 graphics.PreferredBackBufferHeight - 200, 200, 200); 235 } 236 237 private void CreateHUD() 238 { 239 gameHUD = new HUD(); 240 gameHUD.ViewPort = hudView; 241 gameHUD.AddButton(new Button 242 (new Rectangle(10, 10, 150, 40), 243 "Save", Color.YellowGreen, Color.Red, GraphicsDevice, SaveMap)); 244 245 gameHUD.AddButton(new Button( 246 new Rectangle(10,60,32,32), gameMap.textureType[TileType.Grass], 247 Color.Red, true, GraphicsDevice)); 248 gameHUD.AddMiniMap(new MiniMap(gameMap, miniMapView, miniMapBorders)); 249 250 } 251 252 private bool SelectHudTile() 253 { 254 return false; 195 255 } 196 256 -
2011/23/sijoseha/AdventureGame/AdventureGame/Button.cs
r1734 r1758 9 9 namespace AdventureGame 10 10 { 11 12 11 public delegate void ButtonClickedHandler(object sender, EventArgs e); 13 12 … … 21 20 public int Height { get { return Boundaries.Height; } private set { } } 22 21 private Texture2D Image { get; set; } 22 private Texture2D SelectedImage { get; set; } 23 23 public Color BackgroundColor { get; set; } 24 24 public Color BorderColor { get; set; } 25 25 public string ButtonText { get; set; } 26 public bool Selectable { get; set; } 27 public bool Selected { get; set; } 26 28 27 public Button(Rectangle boundaries, string text, Color bgColor, Color borderColor, GraphicsDevice device) 29 public Button(Rectangle boundaries, Texture2D texture, Color borderColor, bool selectable, GraphicsDevice device) 30 { 31 this.Boundaries = boundaries; 32 this.BorderColor = borderColor; 33 this.Image = CreateTexture(borderColor, texture, device); 34 this.SelectedImage = CreateTexture(Color.White, texture, device); 35 } 36 37 public Button(Rectangle boundaries, string text, Color bgColor, Color borderColor, GraphicsDevice device, ButtonClickedHandler clickAction) 28 38 { 29 39 this.Boundaries = boundaries; … … 31 41 this.BackgroundColor = bgColor; 32 42 this.BorderColor = borderColor; 43 this.Image = CreateTexture(bgColor, borderColor, device); 44 45 } 46 47 private Texture2D CreateTexture(Color bordercolor, Texture2D baseTexture, GraphicsDevice device) 48 { 49 Texture2D texture = new Texture2D(device, this.Width, this.Height); 33 50 Color[,] tex = new Color[this.Width, this.Height]; 51 Color[] baseColors = new Color[this.Width*this.Height]; 52 baseTexture.GetData<Color>(baseColors); 34 53 for (int x = 0; x < this.Width; x++) 35 54 { 36 55 for (int y = 0; y < this.Height; y++) 37 56 { 38 if (x == 0 || y == 0 || x == this.Width || y == this.Height) 39 tex[x, y] = borderColor; 40 tex[x, y] = bgColor; 57 if (x == 0 || y == 0 || x == this.Width-1 || y == this.Height-1) 58 tex[x, y] = bordercolor; 59 else 60 tex[x, y] = baseColors[x + y * this.Width]; 41 61 } 42 62 } … … 45 65 for (int y = 0; y < this.Height; y++) 46 66 tex1D[x + y * this.Width] = tex[x, y]; 47 this.Image = new Texture2D(device, this.Width, this.Height); 48 this.Image.SetData(tex1D); 67 texture.SetData<Color>(tex1D); 68 return texture; 69 } 70 71 private Texture2D CreateTexture(Color bgcolor, Color bordercolor, GraphicsDevice device) 72 { 73 Color[,] tex = new Color[this.Width, this.Height]; 74 for (int x = 0; x < this.Width; x++) 75 { 76 for (int y = 0; y < this.Height; y++) 77 { 78 if (x == 0 || y == 0 || x == this.Width-1 || y == this.Height-1) 79 tex[x, y] = bordercolor; 80 else 81 tex[x, y] = bgcolor; 82 } 83 } 84 Color[] tex1D = new Color[this.Width * this.Height]; 85 for (int x = 0; x < this.Width; x++) 86 for (int y = 0; y < this.Height; y++) 87 tex1D[x + y * this.Width] = tex[x, y]; 88 Texture2D texture = new Texture2D(device, this.Width, this.Height); 89 texture.SetData<Color>(tex1D); 90 return texture; 49 91 } 50 92 … … 62 104 public void Draw(SpriteBatch sb) 63 105 { 64 sb.Draw(this.Image, this.Boundaries, Color.White); 65 sb.DrawString(AdventureGame.basicFont, this.ButtonText, this.Position, Color.Black); 106 if(this.Selected) 107 sb.Draw(this.SelectedImage, this.Boundaries, Color.White); 108 else 109 sb.Draw(this.Image, this.Boundaries, Color.White); 110 if(this.ButtonText != null) 111 sb.DrawString(AdventureGame.basicFont, this.ButtonText, this.Position, Color.Black); 66 112 } 67 113 } -
2011/23/sijoseha/AdventureGame/AdventureGame/HUD.cs
r1749 r1758 13 13 public List<Button> Buttons { get; set; } 14 14 public MiniMap MiniMap; 15 16 public Button SelectedButton; 15 17 16 18 public HUD() … … 29 31 } 30 32 33 public void Select(Button button) 34 { 35 for (int i = 0; i < Buttons.Count; i++) 36 { 37 Buttons[i].Selected = false; 38 } 39 button.Selected = true; 40 SelectedButton = button; 41 } 42 31 43 public void Draw(SpriteBatch sb) 32 44 { -
2011/23/sijoseha/AdventureGame/AdventureGame/Tile.cs
r1733 r1758 36 36 this.Boundaries = new Rectangle((int)position.X, (int)position.Y, width, height); 37 37 } 38 39 //public void Draw(SpriteBatch sb)40 //{41 // sb.Draw(this.Image, this.Boundaries, Color.White);42 // if (this.Selected)43 // sb.Draw(AdventureGame.selectionBox, this.Boundaries, Color.White);44 //}45 46 38 } 47 39 }
Note: See TracChangeset
for help on using the changeset viewer.