//---------------------------------------------------------------------------------------------------------------------------------------------------
//
// Copyright (C)2007 DarkWynter Studios. All rights reserved.
//
//---------------------------------------------------------------------------------------------------------------------------------------------------
// {Contact : darkwynter.com for licensing information
//---------------------------------------------------------------------------------------------------------------------------------------------------
namespace DarkWynter.Game
{
#region Using Statements
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
using DarkWynter.Engine.Globals;
using DarkWynter.Engine.ObjectLib;
using DarkWynter.Engine.Menus;
using DarkWynter.Engine.Controllers;
using DarkWynter.Stream;
#endregion
///
/// Event Publisher
/// Game level classes observer and subscribe to these events
///
public class Controller
{
///
/// PlayerIndex this controller checks for
///
public PlayerIndex playerIndex = 0;
///
/// Integer player index associated with this controller
///
public int playerNumber = 0;
///
/// Keyboard State
///
public static KeyboardState keyboardState;
///
/// Game Pad State
///
public static GamePadState gamePadState;
///
/// Mouse State
///
public static MouseState mouseState;
List keyboardControls = new List();
List xboxAnalogControls = new List();
List xboxDigitalControls = new List();
List mouseControls = new List();
///
/// Constructor
///
/// Player index to associate with the controller
public Controller(int player)
{
playerNumber = player;
playerIndex = (PlayerIndex) player;
mouseState = new MouseState();
keyboardState = new KeyboardState();
gamePadState = new GamePadState();
}
///
/// Add a Keyboard control
///
/// Keyboard control
public void Add(KeyboardControl control)
{
keyboardControls.Add(control);
}
///
/// Add a Xbox Analog control
///
/// Xbox Analog control
public void Add(XboxAnalogControl control)
{
xboxAnalogControls.Add(control);
}
///
/// Add a Xbox Digital control
///
/// Xbox Digital control
public void Add(XboxDigitalControl control)
{
xboxDigitalControls.Add(control);
}
///
/// Add a Mouse control
///
/// Mouse control
public void Add(MouseControl control)
{
mouseControls.Add(control);
}
///
/// Update Method
///
/// Object Library
/// Menu System
public void Update(ref ObjectLibrary objectLibrary, ref MenuSystem menuSystem)
{
gamePadState = GamePad.GetState(playerIndex);
keyboardState = Keyboard.GetState();
mouseState = Mouse.GetState();
// Update Controls if not paused
if (Statics_Engine.SystemSettings.gameState != Enums_Engine.EngineState.GAME_PAUSE)
{
// PC
for (int i = 0; i < keyboardControls.Count; i++)
{
keyboardControls[i].Update(keyboardState, ref objectLibrary, ref menuSystem);
}
for (int i = 0; i < mouseControls.Count; i++)
{
mouseControls[i].Update(mouseState, ref objectLibrary, ref menuSystem);
}
// XBOX
for (int i = 0; i < xboxAnalogControls.Count; i++)
{
xboxAnalogControls[i].Update(gamePadState, ref objectLibrary, ref menuSystem);
}
for (int i = 0; i < xboxDigitalControls.Count; i++)
{
xboxDigitalControls[i].Update(gamePadState, ref objectLibrary, ref menuSystem);
}
}
else
{
// ======= Check for unpause button =========
}
}
///
/// Checks to see if a new controller has been plugged in
///
/// ObjectLibrary
public static void CheckForControllers(ObjectLibrary objectLibrary)
{
int index = -1;
Statics_Engine.SystemSettings.TOTAL_PLAYERS = 0;
for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four && index + 1 < Statics_Engine.GameSettings.numberOfHumansActive; i++)
{
index++;
// Check if our gamepad is connected
if (GamePad.GetState(i).IsConnected)
{
// This player has not been initialized so increment number of players and create the new player.
Statics_Engine.SystemSettings.TOTAL_PLAYERS++;
Renderer.ResetViewports(Statics_Engine.SystemSettings.TOTAL_PLAYERS);
DarkWynterGame.gameController = new GameController((int)i);
// Initialize the player as alive
objectLibrary.humans[index].health = 100;
objectLibrary.humans[index].manna = 100;
}
}
#if !XBOX360
// This part is to setup the default keyboard player in case there are no controllers connected
if (Statics_Engine.SystemSettings.TOTAL_PLAYERS == 0)
{
// We have one player the keyboard player
Statics_Engine.SystemSettings.TOTAL_PLAYERS++;
// Create the player, PlayerIndex.One is passed as a default value and not used
Renderer.ResetViewports(Statics_Engine.SystemSettings.TOTAL_PLAYERS);
// If we are player 1 then we also have pc controls
DarkWynterGame.gameController = new GameController(0);
// Initialize the player as alive
objectLibrary.humans[0].health = 100;
objectLibrary.humans[0].manna = 100;
}
// If Player 1 has a controller make sure they are given the keyboard too
else if (Statics_Engine.SystemSettings.TOTAL_PLAYERS >= 1)
{
//if (DarkWynterGame.playerController.playerControllerType == Enums_Engine.ControllerType.XBOX_ONLY)
//{
// DarkWynterGame.playerController = new Controller(Enums_Engine.ControllerType.PC_AND_XBOX, PlayerIndex.One);
//}
//else
if (GameController.playerControllerType == Enums_Engine.ControllerType.NONE)
{
// Create the player, PlayerIndex.One is passed as a default value and not used
Renderer.ResetViewports(Statics_Engine.SystemSettings.TOTAL_PLAYERS);
// If we are player 1 then we also have pc controls
DarkWynterGame.gameController = new GameController(0);
// Initialize the player as alive
objectLibrary.humans[0].health = 100;
objectLibrary.humans[0].manna = 100;
}
}
#endif
}
///
/// Kills the rumble on all controllers
///
/// ObjectLibrary
public static void StopRumble(ObjectLibrary objectLibrary)
{
if (objectLibrary != null && objectLibrary.humans != null && objectLibrary.bots != null)
{
for (int i = 0; i < objectLibrary.humans.Count; i++)
{
if (objectLibrary.humans[i] != null)
{
objectLibrary.humans[i].stopRumble = true;
}
}
}
}
}
}