|
- using System;
- using System.Threading;
- using System.Collections.Generic;
- using GameClass.GameObj;
- using Preparation.Utility;
- using GameEngine;
- using Preparation.Interface;
- using Timothy.FrameRateTask;
- using System.Numerics;
-
- namespace Gaming
- {
- public partial class Game
- {
- private readonly AttackManager attackManager;
- private class AttackManager
- {
- readonly Map gameMap;
- readonly MoveEngine moveEngine;
- public AttackManager(Map gameMap)
- {
- this.gameMap = gameMap;
- this.moveEngine = new MoveEngine(
- gameMap: gameMap,
- OnCollision: (obj, collisionObj, moveVec) =>
- {
- //BulletBomb((Bullet)obj, (GameObj)collisionObj);
- return MoveEngine.AfterCollision.Destroyed;
- },
- EndMove: obj =>
- {
- #if DEBUG
- Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
-
- #endif
- BulletBomb((Bullet)obj, null);
- }
- );
- }
-
- private void BeAddictedToGame(Student player)
- {
- new Thread
- (() =>
- {
- if (player.GamingAddiction > GameData.BeginGamingAddiction && player.GamingAddiction < GameData.MidGamingAddiction)
- player.GamingAddiction = GameData.MidGamingAddiction;
- player.PlayerState = PlayerStateType.Addicted;
- new FrameRateTaskExecutor<int>(
- () => player.PlayerState == PlayerStateType.Addicted && player.GamingAddiction < player.MaxGamingAddiction && gameMap.Timer.IsGaming,
- () =>
- {
- player.GamingAddiction += GameData.frameDuration;
- },
- timeInterval: GameData.frameDuration,
- () =>
- {
- if (player.GamingAddiction == player.MaxGamingAddiction && gameMap.Timer.IsGaming)
- {
- Die(player);
- }
- return 0;
- }
- )
- .Start();
- }
- )
- { IsBackground = true }.Start();
- }
-
- public void BeStunned(Character player, int time)
- {
- new Thread
- (() =>
- {
- player.PlayerState = PlayerStateType.Stunned;
- new FrameRateTaskExecutor<int>(
- () => player.PlayerState == PlayerStateType.Stunned && gameMap.Timer.IsGaming,
- () =>
- {
- },
- timeInterval: GameData.frameDuration,
- () =>
- {
- if (player.PlayerState == PlayerStateType.Stunned)
- player.PlayerState = PlayerStateType.Null;
- return 0;
- },
- maxTotalDuration: time
- )
- .Start();
- }
- )
- { IsBackground = true }.Start();
- }
-
- private void Die(Character player)
- {
-
- player.Die(PlayerStateType.Deceased);
-
- for (int i = 0; i < GameData.maxNumOfPropInPropInventory; i++)
- {
- Prop? prop = player.UseProp(i);
- if (prop != null)
- {
- prop.ReSetPos(player.Position, gameMap.GetPlaceType(player.Position));
- gameMap.Add(prop);
- }
- }
- --gameMap.NumOfSurvivingStudent;
- if (gameMap.NumOfSurvivingStudent == 1)
- {
- gameMap.GameObjLockDict[GameObjType.EmergencyExit].EnterReadLock();
- try
- {
- foreach (EmergencyExit emergencyExit in gameMap.GameObjDict[GameObjType.EmergencyExit])
- if (emergencyExit.CanOpen)
- {
- emergencyExit.IsOpen = true;
- break;
- }
- }
- finally
- {
- gameMap.GameObjLockDict[GameObjType.EmergencyExit].ExitReadLock();
- }
- }
-
- // player.Reset();
- // ((Character?)bullet.Parent)?.AddScore(GameData.addScoreWhenKillOneLevelPlayer); // 给击杀者加分
-
- }
-
- private void BombObj(Bullet bullet, GameObj objBeingShot)
- {
- switch (objBeingShot.Type)
- {
- case GameObjType.Character:
-
- if ((!((Character)objBeingShot).IsGhost()) && bullet.Parent.IsGhost())
- if (((Student)objBeingShot).BeAttacked(bullet))
- {
- BeAddictedToGame((Student)objBeingShot);
- }
- // if (((Character)objBeingShot).IsGhost() && !bullet.Parent.IsGhost() && bullet.TypeOfBullet == BulletType.Ram)
- // BeStunned((Character)objBeingShot, bullet.AP);
- break;
- default:
- break;
- }
- }
-
- private void BulletBomb(Bullet bullet, GameObj? objBeingShot)
- {
- #if DEBUG
- Debugger.Output(bullet, "bombed!");
- #endif
- bullet.CanMove = false;
-
- if (gameMap.Remove(bullet) && bullet.IsToBomb)
- gameMap.Add(new BombedBullet(bullet));
-
- if (!bullet.IsToBomb)
- {
- if (objBeingShot == null)
- {
- if (bullet.Backswing > 0)
- {
- bullet.Parent.PlayerState = PlayerStateType.Swinging;
-
- new Thread
- (() =>
- {
- Thread.Sleep(bullet.Backswing);
-
- if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
- {
- bullet.Parent.PlayerState = PlayerStateType.Null;
- }
- }
- )
- { IsBackground = true }.Start();
- }
- return;
- }
-
-
- BombObj(bullet, objBeingShot);
- if (bullet.RecoveryFromHit > 0)
- {
- bullet.Parent.PlayerState = PlayerStateType.Swinging;
-
- new Thread
- (() =>
- {
-
- Thread.Sleep(bullet.RecoveryFromHit);
-
- if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
- {
- bullet.Parent.PlayerState = PlayerStateType.Null;
- }
- }
- )
- { IsBackground = true }.Start();
- }
- return;
- }
-
- /*if (objBeingShot != null)
- {
- else if (objBeingShot is Bullet) //子弹不能相互引爆,若要更改这一设定,取消注释即可。
- {
- new Thread(() => { BulletBomb((Bullet)objBeingShot, null); }) { IsBackground = true }.Start();
- }
- }*/
-
- // 子弹爆炸会发生的事↓↓↓
- var beAttackedList = new List<IGameObj>();
-
- foreach (var kvp in gameMap.GameObjDict)
- {
- if (bullet.CanBeBombed(kvp.Key))
- {
- gameMap.GameObjLockDict[kvp.Key].EnterReadLock();
- try
- {
- foreach (var item in gameMap.GameObjDict[kvp.Key])
- if (bullet.CanAttack((GameObj)item))
- {
- beAttackedList.Add(item);
- }
-
- }
- finally
- {
- gameMap.GameObjLockDict[kvp.Key].ExitReadLock();
- }
- }
- }
-
- foreach (GameObj beAttackedObj in beAttackedList)
- {
- BombObj(bullet, beAttackedObj);
- }
- if (objBeingShot == null)
- {
- if (bullet.Backswing > 0)
- {
- bullet.Parent.PlayerState = PlayerStateType.Swinging;
-
- new Thread
- (() =>
- {
- Thread.Sleep(bullet.Backswing);
-
- if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
- {
- bullet.Parent.PlayerState = PlayerStateType.Null;
- }
- }
- )
- { IsBackground = true }.Start();
- }
- }
- else
- {
- if (bullet.RecoveryFromHit > 0)
- {
- bullet.Parent.PlayerState = PlayerStateType.Swinging;
-
- new Thread
- (() =>
- {
-
- Thread.Sleep(bullet.RecoveryFromHit);
-
- if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
- {
- bullet.Parent.PlayerState = PlayerStateType.Null;
- }
- }
- )
- { IsBackground = true }.Start();
- }
- }
- beAttackedList.Clear();
- }
-
- public bool Attack(Character? player, double angle) // 射出去的子弹泼出去的水(狗头)
- { // 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
- if (player == null)
- {
- #if DEBUG
- Console.WriteLine("the player who will attack is NULL!");
- #endif
- return false;
- }
-
- if (!player.Commandable())
- return false;
-
- XY res = new XY // 子弹紧贴人物生成。
- (
- (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Cos(angle)),
- (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Sin(angle))
- );
-
- Bullet? bullet = player.Attack(res, gameMap.GetPlaceType(res));
-
- if (bullet != null)
- {
- bullet.CanMove = true;
- gameMap.Add(bullet);
- moveEngine.MoveObj(bullet, (int)((bullet.BulletAttackRange - player.Radius - BulletFactory.BulletRadius(player.BulletOfPlayer)) * 1000 / bullet.MoveSpeed), angle); // 这里时间参数除出来的单位要是ms
-
-
- if (bullet.CastTime > 0)
- {
- player.PlayerState = PlayerStateType.TryingToAttack;
-
- new Thread
- (() =>
- {
- new FrameRateTaskExecutor<int>(
- loopCondition: () => player.PlayerState == PlayerStateType.TryingToAttack && gameMap.Timer.IsGaming,
- loopToDo: () =>
- {
- },
- timeInterval: GameData.frameDuration,
- finallyReturn: () => 0,
- maxTotalDuration: bullet.CastTime
- )
-
- .Start();
-
- if (gameMap.Timer.IsGaming)
- {
- if (player.PlayerState == PlayerStateType.TryingToAttack)
- {
- player.PlayerState = PlayerStateType.Null;
- }
- else
- bullet.IsMoving = false;
- gameMap.Remove(bullet);
- }
- }
- )
- { IsBackground = true }.Start();
- }
- }
- if (bullet != null)
- {
- #if DEBUG
- Console.WriteLine($"playerID:{player.ID} successfully attacked!");
- #endif
- return true;
- }
- else
- {
- #if DEBUG
- Console.WriteLine($"playerID:{player.ID} has no bullets so that he can't attack!");
- #endif
- return false;
- }
- }
- }
- }
- }
|