public override State HandleInput(Player player, StateInput input) { if (input == StateInput.KeyUp) { hDir = HorizDir.Null; vDir = VertDir.Null; } switch (input) { case StateInput.LeftKeyPress: hDir = HorizDir.Left; break; case StateInput.RightKeyPress: hDir = HorizDir.Right; break; case StateInput.UpKeyPress: vDir = VertDir.Up; break; case StateInput.DownKeyPress: vDir = VertDir.Down; break; case StateInput.Hit: player.TakeHit(1); break; case StateInput.Null: return new Idle(); default: break; } if (player.velocity == Vector2.Zero) animState = AnimState.Idle; else animState = AnimState.Walking; return base.HandleInput(player, input, this); }
public void Move(HorizDir aHorizDir, VertDir aVertDir) { Move(aHorizDir, aVertDir, Speed.Normal); }
public void Move(HorizDir aHorizDir, VertDir aVertDir, Speed aSpeed) { // Device doesnt stop motor at limits, will just keep grinding gears away. // So we have to prevent the user from issuing commands that would cause this. if ((CurrentPosition.Horizontal == HorizPos.LeftLimit && aHorizDir == HorizDir.Left) || (CurrentPosition.Horizontal == HorizPos.RightLimit && aHorizDir == HorizDir.Right)) { aHorizDir = HorizDir.None; } if ((CurrentPosition.Vertical == VertPos.UpLimit && aVertDir == VertDir.Up) || (CurrentPosition.Vertical == VertPos.DownLimit && aVertDir == VertDir.Down)) { aVertDir = VertDir.None; } byte xCommand = Command.Noop; switch (aHorizDir) { case HorizDir.None: switch (aVertDir) { case VertDir.None: xCommand = Command.Stop; aSpeed = Speed.Normal; break; case VertDir.Up: xCommand = aSpeed == Speed.Normal ? Command.Up : Command.UpSlow; break; case VertDir.Down: xCommand = aSpeed == Speed.Normal ? Command.Down : Command.DownSlow; break; } break; case HorizDir.Left: switch (aVertDir) { case VertDir.None: xCommand = aSpeed == Speed.Normal ? Command.Left : Command.LeftSlow; break; case VertDir.Up: xCommand = Command.UpAndLeft; aSpeed = Speed.Normal; break; case VertDir.Down: xCommand = Command.DownAndLeft; aSpeed = Speed.Normal; break; } break; case HorizDir.Right: switch (aVertDir) { case VertDir.None: xCommand = aSpeed == Speed.Normal ? Command.Right : Command.RightSlow; break; case VertDir.Up: xCommand = Command.UpAndRight; aSpeed = Speed.Normal; break; case VertDir.Down: xCommand = Command.DownAndRight; aSpeed = Speed.Normal; break; } break; } CurrentMotion.Horizontal = aHorizDir; CurrentMotion.Vertical = aVertDir; CurrentMotion.Speed = aSpeed; if (CurrentMotion.Firing) { // performs fire in Unattended mode SendCmd(64, true); SendCmd(16, true); SendCmd(64, true); SendCmd(64, true); SendCmd(32, true); } else { SendCmd(xCommand); if (_moveMode == MoveMode.StepbyStep) { // after a movement, sent stop if the move mode is step by step xCommand = Command.Stop; SendCmd(xCommand); } } // calculate position var positionDif = aSpeed == Speed.Slow ? 1 : 2; switch (aHorizDir) { case HorizDir.Left: CurrentPosition.PositionX = CurrentPosition.PositionX - positionDif; break; case HorizDir.Right: CurrentPosition.PositionX = CurrentPosition.PositionX + positionDif; break; } switch (aVertDir) { case VertDir.Down: CurrentPosition.PositionY = CurrentPosition.PositionY - positionDif; break; case VertDir.Up: CurrentPosition.PositionY = CurrentPosition.PositionY + positionDif; break; } Debug.Print(GetCurrentPosition()); }
public void Move(HorizDir aHorizDir, VertDir aVertDir, Speed aSpeed) { // Device doesnt stop motor at limits, will just keep grinding gears away. // So we have to prevent the user from issuing commands that would cause this. if ((CurrentPosition.Horizontal == HorizPos.LeftLimit && aHorizDir == HorizDir.Left) || (CurrentPosition.Horizontal == HorizPos.RightLimit && aHorizDir == HorizDir.Right)) { aHorizDir = HorizDir.None; } if ((CurrentPosition.Vertical == VertPos.UpLimit && aVertDir == VertDir.Up) || (CurrentPosition.Vertical == VertPos.DownLimit && aVertDir == VertDir.Down)) { aVertDir = VertDir.None; } byte xCommand = Command.Noop; switch (aHorizDir) { case HorizDir.None: switch (aVertDir) { case VertDir.None: xCommand = Command.Stop; aSpeed = Speed.Normal; break; case VertDir.Up: xCommand = aSpeed == Speed.Normal ? Command.Up : Command.UpSlow; break; case VertDir.Down: xCommand = aSpeed == Speed.Normal ? Command.Down : Command.DownSlow; break; } break; case HorizDir.Left: switch (aVertDir) { case VertDir.None: xCommand = aSpeed == Speed.Normal ? Command.Left : Command.LeftSlow; break; case VertDir.Up: xCommand = Command.UpAndLeft; aSpeed = Speed.Normal; break; case VertDir.Down: xCommand = Command.DownAndLeft; aSpeed = Speed.Normal; break; } break; case HorizDir.Right: switch (aVertDir) { case VertDir.None: xCommand = aSpeed == Speed.Normal ? Command.Right : Command.RightSlow; break; case VertDir.Up: xCommand = Command.UpAndRight; aSpeed = Speed.Normal; break; case VertDir.Down: xCommand = Command.DownAndRight; aSpeed = Speed.Normal; break; } break; } CurrentMotion.mHorizontal = aHorizDir; CurrentMotion.mVertical = aVertDir; CurrentMotion.mSpeed = aSpeed; if (CurrentMotion.mFiring) { if (xCommand == Command.Noop) { xCommand = Command.Stop; } xCommand = (byte)(xCommand + Command.Fire); } SendCmd(xCommand); }
protected void SpecifiedDevice_DataRecieved(object aSender, DataRecievedEventArgs aData) { //Firing //0 128 //0 0 //0 128 //0 0 // This occurs at fire - first one at end of prime? bool xTriggerLimitReachedEvent = false; var xData = aData.data; if (xData.Length != 9) { return; } if (true) { DebugReceivedData(xData); } HorizDir xHorizDir = CurrentMotion.Horizontal; VertDir xVertDir = CurrentMotion.Vertical; // Check for vertical status int xVertStatus = xData[1]; if (xVertStatus == 64) { CurrentPosition.mVertical = VertPos.DownLimit; // Dont combine this with: // if (xVertStatus == 64) { // else when its first run and Vertical = unknown, it can allow a quick move if (CurrentMotion.Vertical == VertDir.Down) { xTriggerLimitReachedEvent = true; xVertDir = VertDir.None; } } else if (xVertStatus == 128) { CurrentPosition.mVertical = VertPos.UpLimit; if (CurrentMotion.Vertical == VertDir.Up) { xTriggerLimitReachedEvent = true; xVertDir = VertDir.None; } } else if (xVertStatus == 0) { // Don't rely on else alone - above checks also check direction, else alone will cause bug // Old ifs used to read: // } else if (xVertStatus == 128 && CurrentMotion.Vertical == VertDir.Up) { // But could fall through to here if starting (ie no direction) and then allow cracking of gears // Should not be possble now that logic has changed, however 0 is the only valid value for middle // and we should stick to that and not assume any value can be middle. CurrentPosition.mVertical = VertPos.Middle; } // Check for horiontal status int xHorizStatus = xData[2] & 15; // Important - Fire sets bit and can be combined (tested) if (xHorizStatus == 4) { CurrentPosition.mHorizontal = HorizPos.LeftLimit; if (CurrentMotion.Horizontal == HorizDir.Left) { xTriggerLimitReachedEvent = true; xHorizDir = HorizDir.None; } } else if (xHorizStatus == 8) { CurrentPosition.mHorizontal = HorizPos.RightLimit; if (CurrentMotion.Horizontal == HorizDir.Right) { xTriggerLimitReachedEvent = true; xHorizDir = HorizDir.None; } } else if (xHorizStatus == 0) { CurrentPosition.mHorizontal = HorizPos.Middle; } // Program can start up as 0 128 if shut down that way! // Because of this we need to check previous as well. if (mLastData[2] > 127 && xData[2] < 127) { OnMissileFired(); if (mFiringAutoStop) { StopFiring(); } } // Take comiled directions, and if they are different than current motions, modify them if (xHorizDir != CurrentMotion.Horizontal || xVertDir != CurrentMotion.Vertical) { Move(xHorizDir, xVertDir, CurrentMotion.Speed); } if (xTriggerLimitReachedEvent) { OnLimitReached(); } xData.CopyTo(mLastData, 0); }