using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using DarkWynter.Engine.Menus;
using DWControllerDemo;
using DarkWynter.Engine.Controllers;
using DarkWynter.Engine.UserInterface;
using System.Xml;
using System.Diagnostics;
namespace NoName
{
///
/// This is the main type for your game
///
public class SideScroller : Microsoft.Xna.Framework.Game
{
public static SideScroller sideScroller;
public MenuSystem menuSystem;
ControllerManager controllerManager;
List args = new List();
List menuController;
List gameController;
public Color backColor = Color.CornflowerBlue;
string fileName = System.IO.Directory.GetCurrentDirectory();
Stopwatch gameTimer = new Stopwatch();
bool gameTimeStarted = false;
public SideScroller()
{
Statics.Globals.graphics = new GraphicsDeviceManager(this);
Statics.Globals.content = new ContentManager(Services);
Statics.Globals.content.RootDirectory = "Content";
//Statics.Globals.graphics.PreferredBackBufferHeight = 800;
// Statics.Globals.graphics.PreferredBackBufferWidth = 1280;
sideScroller = this;
}
///
/// 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.
///
protected override void Initialize()
{
Statics.Globals.gameFonts.Arial = Statics.Globals.content.Load("_fonts/Arial");
menuSystem = new MenuSystem();
Statics.Globals.engineState = Statics.Enums.EngineState.MENU_MODE;
// Create a List of Child Controllers
List controllerTypes = new List();
controllerTypes.Add(typeof(MenuController));
controllerTypes.Add(typeof(GameController));
// Pass in Width and Height used by mouse.
controllerManager = new ControllerManager(Statics.Globals.graphics.GraphicsDevice.Viewport.Width,
Statics.Globals.graphics.GraphicsDevice.Viewport.Height, controllerTypes);
// Pass “this” as EventArgs to Conroller delegates functions
args.Add(this);
base.Initialize();
}
///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
Statics.Globals.spriteBatch = new SpriteBatch(Statics.Globals.graphics.GraphicsDevice);
LoadLevelsXML();
#region HUD Load and Setup
//hud
Statics.Globals.viewport = Statics.Globals.graphics.GraphicsDevice.Viewport;
fileName += "\\_xml\\HUD.xml";
Statics.Globals.hud = new HeadsUpDisplay(this, fileName);
for (int x = 0; x < Statics.Globals.hud.textDisplays.Count; x++)
{
if (Statics.Globals.hud.textDisplays[x].name == "timerText")
{
Statics.Globals.timerIndex = x;
}
}
for (int x = 0; x < Statics.Globals.hud.imageDisplays.Count; x++)
{
if (Statics.Globals.hud.imageDisplays[x].name == "dialogueBox")
{
Statics.Globals.dialogueTextureIndex = x;
}
}
for (int x = 0; x < Statics.Globals.hud.textDisplays.Count; x++)
{
if (Statics.Globals.hud.textDisplays[x].name == "speakerText")
{
Statics.Globals.dialogueSpeakerIndex = x;
}
else if (Statics.Globals.hud.textDisplays[x].name == "dialogueText")
{
Statics.Globals.dialogueTextIndex = x;
}
else if (Statics.Globals.hud.textDisplays[x].name == "positionText")
{
Statics.Globals.DEBUG = x;
}
}
#endregion
}
private static void LoadLevelsXML()
{
string LevelXMLFile = "_xml/LevelEdit/_Levels.xml";
Statics.Globals.levelInfo = new List();
try
{
// Load physics, music, and fog
XmlDocument reader = new XmlDocument();
reader.Load(LevelXMLFile);
XmlNodeList allNodes = reader.ChildNodes;
foreach (XmlNode levelNode in allNodes)
{
if (levelNode.Name == "levels")
{
XmlNodeList levelNodes = levelNode.ChildNodes;
foreach (XmlNode node in levelNodes)
{
if (node.Name == "level")
{
Statics.Globals.LevelInfo level = new Statics.Globals.LevelInfo();
level.name = node.Attributes["name"].Value;
level.filepath = node.Attributes["xmlFile"].Value;
level.description = node.Attributes["description"].Value;
level.eventFilepath = node.Attributes["eventFile"].Value;
Statics.Globals.levelInfo.Add(level);
}
}
}
}
}
catch
{
System.Diagnostics.Debug.WriteLine("Error reading xml");
//throw new Exception("Error reading XML");
}
}
///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///
/// Provides a snapshot of timing values.
protected override void Update(GameTime gameTime)
{
// Checks if any controllers Added or Removed
controllerManager.Update();
if (Statics.Globals.engineState == NoName.Statics.Enums.EngineState.MENU_MODE)
{
menuSystem.Update();
// Update all MenuControllers
menuController = controllerManager.GetControllers(typeof(MenuController));
for (int i = 0; i < menuController.Count; i++)
{
menuController[i].Update(ref args);
}
}
if (Statics.Globals.engineState == NoName.Statics.Enums.EngineState.GAME_MODE)
{
if (!gameTimeStarted)
{
gameTimer.Start();
gameTimeStarted = true;
//GameEventHandler.CurrentGameConditions.lastNodeVisited = "start";
}
#region HUD Update
// Update the Game Time
int seconds = ((int)gameTimer.Elapsed.TotalSeconds) % 60;
int minutes = ((int)gameTimer.Elapsed.TotalSeconds) / 60;
string formatedSeconds = seconds.ToString();
if (seconds < 10)
{
formatedSeconds = "0" + seconds.ToString();
}
string timing = minutes.ToString() + ":" + formatedSeconds;
Statics.Globals.hud.textDisplays[Statics.Globals.timerIndex].text = timing;
Statics.Globals.hud.UpdateDialogue();
#endregion
// Update all GameControllers
gameController = controllerManager.GetControllers(typeof(GameController));
for (int i = 0; i < gameController.Count; i++)
{
gameController[i].Update(ref args);
}
base.Update(gameTime);
}
}
///
/// This is called when the game should draw itself.
///
/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
Statics.Globals.graphics.GraphicsDevice.Clear(backColor);
if (Statics.Globals.engineState == NoName.Statics.Enums.EngineState.MENU_MODE)
{
menuSystem.Draw(Statics.Globals.spriteBatch);
}
base.Draw(gameTime);
}
}
}