/*
* GameObject.cs
* Authors: Karl Orosz
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace StarJack
{
public class GameObject : Entity, TimedObject
{
//Dteremines how position is calculated
protected bool mTileMode;
public bool TileMode
{
get { return mTileMode; }
set { mTileMode = value; }
}
protected bool mDetected;
public bool Detected
{
get { return mDetected; }
set { mDetected = value; }
}
protected bool mDetectable = true;
public bool Detectable
{
get { return mDetectable; }
set { mDetectable = value; }
}
int[] mPos;
///
/// Sets the
///
public int[] Position
{
get
{
return mPos;
}
set
{
mPos = value;
if (mTileMode)
{
this.X = mPos[0] * Width;
this.Y = mPos[1] * Height;
}
else
{
base.X = value[0];
base.Y = value[1];
}
}
}
public enum Direction
{
Up,
Down,
Left,
Right,
Other,
None
}
protected Direction mFacing;
public virtual Direction Facing
{
get { return mFacing; }
set
{
mFacing = value;
if (MyAnimation != null && mAction != Action.Standing)
{
switch (mFacing)
{
case Direction.Left:
MyAnimation.SetAnimation("MoveLeft");
MyAnimation.Start();
break;
case Direction.Right:
MyAnimation.SetAnimation("MoveRight");
MyAnimation.Start();
break;
case Direction.Down:
MyAnimation.SetAnimation("MoveDown");
MyAnimation.Start();
break;
case Direction.Up:
MyAnimation.SetAnimation("MoveNorth");
MyAnimation.Start();
break;
}
}
else if (mFacing != Direction.None && mGroupTextures != null)
{
switch (mFacing)
{
case Direction.Up:
Texture = mGroupTextures["Up"];
break;
case Direction.Down:
Texture = mGroupTextures["Down"];
break;
case Direction.Left:
Texture = mGroupTextures["Left"];
break;
case Direction.Right:
Texture = mGroupTextures["Right"];
break;
}
}
}
}
public enum Action
{
Moving,
Standing
}
private Action mAction = Action.Standing;
public Action MyAction
{
get { return mAction; }
set { mAction = value; }
}
private Dictionary> mAI;
public void AddAI(AI newAI)
{
mAI[newAI.curPriority].Add(newAI);
}
protected Room mRoom;
public Room MyRoom
{
get
{
return mRoom;
}
set
{
mRoom = value;
}
}
private String mObjName;
public String ObjectName
{
get
{
return mObjName;
}
set
{
mObjName = value;
}
}
private bool mAIEnable;
public bool AIEnable
{
get { return mAIEnable; }
set { mAIEnable = value; }
}
private List mTurnRoutines;
public List TurnRoutines
{
get { return mTurnRoutines; }
}
public GameObject()
{
//empty on purpose
}
public GameObject(String name, bool tileMode)
: base(name)
{
mName = name;
//mRoom = myRoom;
mTileMode = tileMode;
if (mTileMode)
{
Height = Tile.HEIGHT;
Width = Tile.WIDTH;
}
Facing = Direction.Down;
Position = new int[2];
Depth = Room.Depth.GameObject;
mTurnRoutines = new List();
IniAI();
}
private void IniAI()
{
mAI = new Dictionary>();
mAI.Add(AI.Priority.High, new List());
mAI.Add(AI.Priority.Medium, new List());
mAI.Add(AI.Priority.Low, new List());
mAIEnable = true; ;
}
public void ResetAI()
{
foreach (AI hiAI in mAI[AI.Priority.High])
{
hiAI.Reset();
}
foreach (AI hiAI in mAI[AI.Priority.Medium])
{
hiAI.Reset();
}
foreach (AI hiAI in mAI[AI.Priority.Low])
{
hiAI.Reset();
}
}
///
/// This will take a turn in all the AI routines
///
///
public virtual void TakeTurn(int pTurn, bool deffered)
{
if (mAIEnable)
{
foreach (AI hiAI in mAI[AI.Priority.High])
{
if (deffered && hiAI is DefferedTurn)
{
hiAI.TakeTurn(pTurn,deffered);
}
else if(!deffered)
{
hiAI.TakeTurn(pTurn,deffered);
}
}
foreach (AI hiAI in mAI[AI.Priority.Medium])
{
if (deffered && hiAI is DefferedTurn)
{
hiAI.TakeTurn(pTurn, deffered);
}
else if( !deffered)
{
hiAI.TakeTurn(pTurn, deffered);
}
}
foreach (AI hiAI in mAI[AI.Priority.Low])
{
if (deffered && hiAI is DefferedTurn)
{
hiAI.TakeTurn(pTurn, deffered);
}
else if (!deffered)
{
hiAI.TakeTurn(pTurn, deffered);
}
}
}
}
public virtual void TurnUpdate(GameTime time)
{
}
public int[] FacingMod()
{
int[] offSet = new int[] { 0, 0 };
switch (Facing)
{
case Direction.Up:
offSet[1]--;
break;
case Direction.Left:
offSet[0]--;
break;
case Direction.Right:
offSet[0]++;
break;
case Direction.Down:
offSet[1]++;
break;
}
return offSet;
}
///
/// Moves the object 1 space in the specified direction
///
///
///
public virtual bool Move(Direction dir, bool adjustFacing)
{
int[] newPos = new int[2];
Position.CopyTo(newPos,0);
switch (dir)
{
case Direction.Down:
newPos[1]++;
break;
case Direction.Up:
newPos[1]--;
break;
case Direction.Left:
newPos[0]--;
break;
case Direction.Right:
newPos[0]++;
break;
}
if (adjustFacing)
{
Facing = dir;
}
return Move(newPos);
}
public virtual bool Move(int[] newPos)
{
bool collision = mRoom.CheckCollision(this,newPos);
if (!collision)
{
mAction = Action.Moving;
mRoom.ChangePosition(this, newPos);
return true;
}
else
{
return false;
}
}
public override void LoadContent(ContentManager content)
{
base.LoadContent(content);
DrawableAI dAI;
foreach (AI hiAI in mAI[AI.Priority.High])
{
dAI = hiAI as DrawableAI;
if (dAI != null)
{
dAI.LoadContent(content);
}
}
foreach (AI meAI in mAI[AI.Priority.Medium])
{
dAI = meAI as DrawableAI;
if (dAI != null)
{
dAI.LoadContent(content);
}
}
foreach (AI loAI in mAI[AI.Priority.Low])
{
dAI = loAI as DrawableAI;
if (dAI != null)
{
dAI.LoadContent(content);
}
}
}
public override void Update(Microsoft.Xna.Framework.GameTime time)
{
if (mAIEnable)
{
UpdatableAI dAI;
//Updates any AI that needs time based info
foreach (AI hiAI in mAI[AI.Priority.High])
{
dAI = hiAI as UpdatableAI;
if (dAI != null)
{
dAI.Update(time);
}
}
foreach (AI meAI in mAI[AI.Priority.Medium])
{
dAI = meAI as UpdatableAI;
if (dAI != null)
{
dAI.Update(time);
}
}
foreach (AI loAI in mAI[AI.Priority.Low])
{
dAI = loAI as UpdatableAI;
if (dAI != null)
{
dAI.Update(time);
}
}
}
base.Update(time);
}
public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch pSpriteBatch)
{
base.Draw(pSpriteBatch);
if (mAIEnable)
{
//Draws any of the AI
DrawableAI dAI;
foreach (AI hiAI in mAI[AI.Priority.High])
{
dAI = hiAI as DrawableAI;
if (dAI != null)
{
dAI.Draw(pSpriteBatch);
}
}
foreach (AI meAI in mAI[AI.Priority.Medium])
{
dAI = meAI as DrawableAI;
if (dAI != null)
{
dAI.Draw(pSpriteBatch);
}
}
foreach (AI loAI in mAI[AI.Priority.Low])
{
dAI = loAI as DrawableAI;
if (dAI != null)
{
if (this.Position[0] == 13 && this.Position[1] == 4)
{
}
dAI.Draw(pSpriteBatch);
}
}
}
}
public void RemoveFromRoom()
{
mRoom.RemoveObject(this);
}
}
}