/* * GameWorld.cs * Authors: Brian Chesbrough * * Copyright Matthew Belmonte 2007 */ using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; using System.Diagnostics; using Pina3D.Particles; using tAC_Engine; namespace Astropolis { /// /// This is the Game World for HackerHavok /// The game board and tiles are created and updated here /// All entities in the game world are stored here to be rendered /// class GameWorldOld { private SpriteBatch mSpriteBatch; // Internal buffer private List mEntities; // General entities to render private List mGameBoard; // the Gameboard of tiles. private List mActiveTiles; // list of tiles the player can interact with this turn private float mBoardWidth = 10; // Board width in tiles private float mBoardLength = 10; // Board Length in tiles private Texture2D mTileSprite; // The texture to be used for tiles private float mScreenSpaceRight; // Right edge of screen in screen space private float mScreenSpaceLeft; // Left edge of screen in screen space private float mScreenSpaceTop; // Top edge of screen in screen space private float mScreenSpaceBottom; // Bottom edge of screen in screen space private float mXOffSet; // Variable for moving screen/calculating screen space private float mYOffSet; // Variable for moving screen/calculating screen space private float mPreviousXOffset; // Variable for optimization private float mPreviousYOffset; // Variable for optimization private float mWorldSpaceWidth; // Width of gameboard in world space (entire width; offscreen included) public float WorldWidth { get { return mWorldSpaceWidth - mXOffSet; } } private float mWorldSpaceHeight; // Height of gameboard in world space (entire height; offscreen included) public float WorldHeight { get { return mWorldSpaceWidth - mYOffSet; } } private float mScrollSpeed; // Speed at which the screen will scroll public List Entities { get { return mEntities; } } public List GameBoard { get { return mGameBoard; } } public float XOffSet { set { mXOffSet = value; } get { return mXOffSet; } } public float YOffSet { set { mYOffSet = value; } get { return mYOffSet; } } public Entity center = null; /// /// Constructor /// public GameWorldOld() { mSpriteBatch = new SpriteBatch(GenericBaseApplication.GameManager.GraphicsDevice); mEntities = new List(); mGameBoard = new List(); mTileSprite = TextureManager.Load(@"Content\MiniGames\HHhexTile"); //fill tiles on game board for (int y = 0; y < mBoardLength; y++) { for (int x = 0; x < mBoardWidth; x++) { int xPos = x * 55; //modulate Y up and down to position Hex blocks int yPos = ( 25 * (x % 2) ) + (50 * y); HackerTile mNewTile = new HackerTile(mTileSprite, xPos, yPos); mNewTile.Visible = false; mGameBoard.Add(mNewTile); } } //Calculate board width and height mWorldSpaceWidth = 55 * mBoardWidth; mWorldSpaceHeight = mGameBoard[1].Height * mBoardLength; //initalize screen parameters mXOffSet = 0; mYOffSet = 0; mPreviousXOffset = 1; mPreviousYOffset = 1; mScreenSpaceRight = GenericBaseApplication.GameManager.ScreenWidth; mScreenSpaceLeft = 0; mScreenSpaceTop = 0; mScreenSpaceBottom = GenericBaseApplication.GameManager.ScreenHeight; mScrollSpeed = 100; } /// /// Adds a new Entity into the field and sorts them for drawing /// /// public void AddEntity(Entity newEntity) { mEntities.Add(newEntity); mEntities.Sort(); } /// /// Removes an entity from the field /// /// public void RemoveEntity(Entity oldEntity) { mEntities.Remove(oldEntity); } /// /// Updates GameBoard related stuff: /// Screen Space variables /// Screen scrolling /// what entities are visible /// highlighted tile /// public void Update() { //Original //mScreenSpaceLeft = mXOffSet; //mScreenSpaceRight = GenericBaseApplication.GameManager.ScreenWidth + mXOffSet; //mScreenSpaceTop = mYOffSet; //mScreenSpaceBottom = GenericBaseApplication.GameManager.ScreenHeight + mYOffSet; //update screen space variables // mScreenSpaceLeft = mXOffSet - GenericBaseApplication.GameManager.ScreenWidth; // mScreenSpaceRight = GenericBaseApplication.GameManager.ScreenWidth + mXOffSet; // mScreenSpaceTop = mYOffSet - GenericBaseApplication.GameManager.ScreenHeight; // mScreenSpaceBottom = GenericBaseApplication.GameManager.ScreenHeight + mYOffSet; //scroll screen ScrollScreen(); foreach (Entity thisEntity in mEntities) { thisEntity.Update(); } //update whats visible updateVisibleEntities(); // System.Console.WriteLine("Screen L: " + mScreenSpaceLeft + " R: " + mScreenSpaceRight + // " B: " + mScreenSpaceBottom + " T: " + mScreenSpaceTop); // thisTile.Update(mouse); } /// /// This checks the position of the mouse and if it's near /// a screen edge it increases the x or Y offset to cause /// the screen to scroll in that direction /// public void ScrollScreen() { float dt = AstroBaseApplication.GameManager.ElapsedSeconds; // System.Console.WriteLine("X: " + center.X + " Y: "+center.Y); if (center != null) { mXOffSet = center.X - mScreenSpaceRight/2; mYOffSet = center.Y - mScreenSpaceBottom / 2; } } /// /// Updates visability of entities in/out of screen space /// public void updateVisibleEntities() { //If the screen has scrolled since the last update... if (mPreviousXOffset != mXOffSet || mPreviousYOffset != mYOffSet) { //Update what tiles are visible in screen space foreach (HackerTile thisTile in mGameBoard) { //If this tile is in screen space + one extra tile on each side if (thisTile.X > (mScreenSpaceLeft - thisTile.Width) && thisTile.X < (mScreenSpaceRight + thisTile.Width) && thisTile.Y > (mScreenSpaceTop - thisTile.Height) && thisTile.Y < (mScreenSpaceBottom + thisTile.Height)) { thisTile.Visible = true; } else { thisTile.Visible = false; } } //Update what entities are visible in screen space foreach (Entity thisEntity in mEntities) { //If your not the player if (!(thisEntity is PlayerShip)) { //If this tile is in screen space + one extra tile on each side if (thisEntity.X > (mScreenSpaceLeft - mGameBoard[1].Width) && thisEntity.X < (mScreenSpaceRight + mGameBoard[1].Width) && thisEntity.Y > (mScreenSpaceTop - mGameBoard[1].Height) && thisEntity.Y < (mScreenSpaceBottom + mGameBoard[1].Height)) { thisEntity.Visible = true; } else { thisEntity.Visible = false; } } } mPreviousXOffset = mXOffSet; mPreviousYOffset = mYOffSet; } } public void setActiveTiles(float movementMeter, Vector3 playerPos) { if (mActiveTiles != null) { foreach (HackerTile thisTile in mActiveTiles) { thisTile.Tint = Color.TransparentBlack; } } mActiveTiles = new List(); float distanceToPlayer; foreach (HackerTile thisTile in mGameBoard) { if (thisTile.Visible) { distanceToPlayer = (float)Math.Sqrt(Math.Pow((playerPos.X - thisTile.X), 2) + Math.Pow((playerPos.Y - thisTile.Y), 2)); if (distanceToPlayer < movementMeter) { mActiveTiles.Add(thisTile); thisTile.Tint = Color.White; } } } } /// /// Draws all registered visible entities /// public void Render() { mSpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None); Rectangle drawMe = new Rectangle(); // First draw GameBoard for (int i = 0; i < mGameBoard.Count; i++) { HackerTile e = mGameBoard[i]; int x1; int y1; if (e.Visible) { //translate to screen space x1 = (int)Math.Round((e.X - mXOffSet)); y1 = (int)Math.Round((e.Y - mYOffSet)); drawMe.X = (int)x1; drawMe.Y = (int)y1; drawMe.Width = (int)e.Width; drawMe.Height = (int)e.Height; e.Draw(mSpriteBatch, drawMe, MathHelper.Clamp(e.Z, 0f, 1f)); } } //draw the rest of the entities, no special cases yet for (int i = 0; i < mEntities.Count; i++) { Entity e = mEntities[i]; int x1; int y1; if (e.Visible) { //translate to screen space x1 = (int)Math.Round((e.X - mXOffSet)); y1 = (int)Math.Round((e.Y - mYOffSet)); drawMe.X = x1; drawMe.Y = y1; drawMe.Width = (int)e.Width; drawMe.Height = (int)e.Height; e.Draw(mSpriteBatch, drawMe, MathHelper.Clamp(e.Z, 0f, 1f)); } } mSpriteBatch.End(); } } }