/// <summary> /// Execute some change of object in a frame in battlefield /// </summary> /// <param name="objects">Objects are battlefield</param> /// <returns>Result of that frame</returns> public override TypeResult NextFrame(List<ObjectGame> objects) { if (NewResult == TypeResult.BeDestroyed) { return TypeResult.BeDestroyed; } DetectedEnemy(objects); if (EnemyTank != null) { NewResult = TypeResult.Detected; } if (Instruction == null || Instruction.Interruptible) { SetListInstructions(); while (Instruction == null) { if (ListInstructions.Count == 0) { break; } else { if (ListInstructions[0].Condition == null || ListInstructions[0].Condition.GetResult(this, EnemyTank, EnemyBullet)) { Instruction = ListInstructions[0].Clone(); } else { ListInstructions.RemoveAt(0); } } } } NewResult = TypeResult.Normal; ExecuteInstruction(objects); EnemyTank = null; EnemyBullet = null; return TypeResult.Nothing; }
/// <summary> /// Set instructions in each frame (according to LastResult and NewResult) /// </summary> public void SetListInstructions() { if (ListInstructions.Count == 0 || LastResult != NewResult) { Instruction = null; _damageCur = 0; switch (NewResult) { case TypeResult.Normal: ListInstructions = new List<Instruction>(ActionNormal); break; case TypeResult.CannotMoveForward: ListInstructions = new List<Instruction>(ActionCannotMoveForward); break; case TypeResult.CannotMoveBackward: ListInstructions = new List<Instruction>(ActionCannotMoveBackward); break; case TypeResult.Detected: ListInstructions = new List<Instruction>(ActionDetected); break; case TypeResult.BeAttacked: ListInstructions = new List<Instruction>(ActionBeAttacked); break; default: break; } LastResult = NewResult; } }
public void ExecuteInstruction(List<ObjectGame> objects) { if (Instruction == null) { return; } const float epsilon = 1e-6f; float value; var oldPosition = Position; switch (Instruction.Type) { case TypeInstruction.MoveForward: value = Math.Min(Instruction.Value, SpeedMove); Instruction.Value -= value; MoveForward(value); if (IsInvalidPosition(objects)) { Position = oldPosition; NewResult = TypeResult.CannotMoveForward; } break; case TypeInstruction.MoveBackward: value = Math.Min(Instruction.Value, SpeedMove); Instruction.Value -= value; MoveBackward(value); if (IsInvalidPosition(objects)) { Position = oldPosition; NewResult = TypeResult.CannotMoveBackward; } break; case TypeInstruction.RotateRight: value = Math.Min(Instruction.Value, SpeedRotate); Instruction.Value -= value; RotateRight(value); break; case TypeInstruction.RotateLeft: value = Math.Min(Instruction.Value, SpeedRotate); Instruction.Value -= value; RotateLeft(value); break; case TypeInstruction.Fire: value = Math.Min(Instruction.Value, SpeedFire); Instruction.Value -= value; _damageCur += value; if (Math.Abs(Instruction.Value) < epsilon) { var bullet = new Bullet(Properties.Resources.Bullet_A) { Position = MathProcessor.CalPointPosition(Position, 0.5f * Image.Width, Direction), Direction = Direction, Damage = _damageCur }; _damageCur = 0; objects.Add(bullet); } break; default: break; } if (Math.Abs(Instruction.Value) < epsilon) { Instruction = null; ListInstructions.RemoveAt(0); } }