/* * MoveTurnRoutine.cs * Authors: * 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. */ namespace StarJack { public class MoveTurnRoutine:TimedTurnRoutine { private float mElapsedTime; private GameObject mMoving; public GameObject Moving { get { return mMoving; } set { mMoving = value; } } private bool mOrginalTileMode; private int[] mNewPos; private float[] dxdy; private float[] cxcy; private int[] origPos; private bool cleanup; public MoveTurnRoutine(GameObject go, int[] newPos) :base() { origPos = new int[] { 0, 0 }; cxcy = new float[] { 0, 0 }; mElapsedTime = 0.0f; mNewPos = newPos; mMoving = go; mMoving.Facing = mMoving.Facing; origPos[0] = mMoving.X ; origPos[1] = mMoving.Y; if (newPos[0] < 0 || newPos[0] > Room.TILECOUNT || newPos[1] < 0 || newPos[1] > Room.TILECOUNT) { cleanup = true; } if (mMoving.MyAnimation != null && mMoving.MyAnimation.CurrentAnimation != null) { mMoving.MyAnimation.FPS = Animation.CalculateFPS(Room.ROOM_TURN_TIME, mMoving.MyAnimation.CurrentAnimation.FrameCount, 1f); } mOrginalTileMode = mMoving.TileMode; mMoving.TileMode = false; dxdy = new float[2]; int dx = (newPos[0] - mMoving.Position[0]); int dy = (newPos[1] - mMoving.Position[1]); if (dx != 0) { dxdy[0] = ((float)(dx * mMoving.Width + 0.0f) / (float)(Room.ROOM_TURN_TIME + 0.0f)); } if (dy != 0) { dxdy[1] = (float)(dy * mMoving.Height + 0.0f) / (float)(Room.ROOM_TURN_TIME + 0.0f); } } public override void Update(Microsoft.Xna.Framework.GameTime time) { if (State == TurnState.Ready) { State = TurnState.Processing; } mElapsedTime += time.ElapsedGameTime.Milliseconds; if (mElapsedTime >= Room.ROOM_TURN_TIME) { if (cleanup) { State = TurnState.Finished; mMoving.RemoveFromRoom(); } else { State = TurnState.Finished; mMoving.TileMode = true; mMoving.Position = mNewPos; if (mMoving.MyAnimation != null) { mMoving.MyAction = GameObject.Action.Standing; mMoving.MyAnimation.Stop(); mMoving.MyAnimation.State = Animation.PlayState.None; mMoving.Facing = mMoving.Facing; } } } else { cxcy[0] += dxdy[0] * time.ElapsedGameTime.Milliseconds; cxcy[1] += dxdy[1] * time.ElapsedGameTime.Milliseconds; float x = origPos[0] + cxcy[0]; float y = origPos[1] + cxcy[1]; mMoving.X = (int)x; mMoving.Y = (int)y; if (mMoving.MyAnimation != null) { mMoving.MyAnimation.Update(time); } } } } }