You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Character.cs 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. using Preparation.Interface;
  2. using Preparation.Utility;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Numerics;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. namespace GameClass.GameObj
  9. {
  10. public partial class Character : GameObj, ICharacter // 负责人LHR摆烂终了
  11. {
  12. private readonly object beAttackedLock = new();
  13. #region 角色的基本属性及方法,包括与道具的交互方法
  14. /// <summary>
  15. /// 装弹冷却
  16. /// </summary>
  17. protected int cd;
  18. public int CD
  19. {
  20. get => cd;
  21. private set
  22. {
  23. lock (gameObjLock)
  24. {
  25. cd = value;
  26. Debugger.Output(this, string.Format("'s CD has been set to: {0}.", value));
  27. }
  28. }
  29. }
  30. public int OrgCD { get; protected set; }
  31. protected int maxBulletNum;
  32. public int MaxBulletNum => maxBulletNum; // 人物最大子弹数
  33. protected int bulletNum;
  34. public int BulletNum => bulletNum; // 目前持有的子弹数
  35. public int MaxHp { get; protected set; } // 最大血量
  36. protected int hp;
  37. public int HP
  38. {
  39. get => hp;
  40. set
  41. {
  42. if (value > 0)
  43. {
  44. lock (gameObjLock)
  45. hp = value <= MaxHp ? value : MaxHp;
  46. }
  47. else
  48. lock (gameObjLock)
  49. hp = 0;
  50. }
  51. }
  52. private PlayerStateType playerState = PlayerStateType.Null;
  53. public PlayerStateType PlayerState
  54. {
  55. get
  56. {
  57. if (IsResetting) return PlayerStateType.IsResetting;
  58. if (IsMoving) return PlayerStateType.IsMoving;
  59. return playerState;
  60. }
  61. set
  62. {
  63. if (!(value == PlayerStateType.IsMoving || value == PlayerStateType.Null))
  64. lock (gameObjLock)
  65. IsMoving = false;
  66. lock (gameObjLock) playerState = (value == PlayerStateType.IsMoving) ? PlayerStateType.Null : value;
  67. }
  68. }
  69. // private int deathCount = 0;
  70. // public int DeathCount => deathCount; // 玩家的死亡次数
  71. private int score = 0;
  72. public int Score
  73. {
  74. get => score;
  75. }
  76. // public double AttackRange => BulletFactory.BulletAttackRange(this.BulletOfPlayer);
  77. private double vampire = 0; // 回血率:0-1之间
  78. public double Vampire
  79. {
  80. get => vampire;
  81. set
  82. {
  83. if (value > 1)
  84. lock (gameObjLock)
  85. vampire = 1;
  86. else if (value < 0)
  87. lock (gameObjLock)
  88. vampire = 0;
  89. else
  90. lock (gameObjLock)
  91. vampire = value;
  92. }
  93. }
  94. private double oriVampire = 0;
  95. public double OriVampire
  96. {
  97. get => oriVampire;
  98. set
  99. {
  100. if (value > 1)
  101. lock (gameObjLock)
  102. vampire = 1;
  103. else if (value < 0)
  104. lock (gameObjLock)
  105. vampire = 0;
  106. else
  107. lock (gameObjLock)
  108. vampire = value;
  109. }
  110. }
  111. public readonly BulletType OriBulletOfPlayer;
  112. private BulletType bulletOfPlayer;
  113. public BulletType BulletOfPlayer
  114. {
  115. get => bulletOfPlayer;
  116. set
  117. {
  118. lock (gameObjLock)
  119. bulletOfPlayer = value;
  120. }
  121. }
  122. private Prop? propInventory;
  123. public Prop? PropInventory // 持有的道具
  124. {
  125. get => propInventory;
  126. set
  127. {
  128. lock (gameObjLock)
  129. {
  130. propInventory = value;
  131. Debugger.Output(this, " prop becomes " + (PropInventory == null ? "null" : PropInventory.ToString()));
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// 使用物品栏中的道具
  137. /// </summary>
  138. /// <returns>被使用的道具</returns>
  139. public Prop? UseProp()
  140. {
  141. lock (gameObjLock)
  142. {
  143. var oldProp = PropInventory;
  144. PropInventory = null;
  145. return oldProp;
  146. }
  147. }
  148. /// <summary>
  149. /// 是否在隐身
  150. /// </summary>
  151. private bool isInvisible = false;
  152. public bool IsInvisible
  153. {
  154. get => isInvisible;
  155. set
  156. {
  157. lock (gameObjLock)
  158. {
  159. isInvisible = value;
  160. }
  161. }
  162. }
  163. /// <summary>
  164. /// 进行一次远程攻击
  165. /// </summary>
  166. /// <param name="posOffset">子弹初始位置偏差值</param>
  167. /// <returns>攻击操作发出的子弹</returns>
  168. public Bullet? RemoteAttack(XY posOffset)
  169. {
  170. if (TrySubBulletNum())
  171. return ProduceOneBullet(this.Position + posOffset);
  172. else
  173. return null;
  174. }
  175. protected Bullet? ProduceOneBullet(XY initPos)
  176. {
  177. var newBullet = BulletFactory.GetBullet(this);
  178. newBullet?.SetPosition(initPos);
  179. return newBullet;
  180. }
  181. /// <summary>
  182. /// 尝试将子弹数量减1
  183. /// </summary>
  184. /// <returns>减操作是否成功</returns>
  185. private bool TrySubBulletNum()
  186. {
  187. lock (gameObjLock)
  188. {
  189. if (bulletNum > 0)
  190. {
  191. --bulletNum;
  192. return true;
  193. }
  194. return false;
  195. }
  196. }
  197. /// <summary>
  198. /// 尝试将子弹数量加1
  199. /// </summary>
  200. /// <returns>加操作是否成功</returns>
  201. public bool TryAddBulletNum()
  202. {
  203. lock (gameObjLock)
  204. {
  205. if (bulletNum < maxBulletNum)
  206. {
  207. ++bulletNum;
  208. return true;
  209. }
  210. return false;
  211. }
  212. }
  213. /// <summary>
  214. /// 尝试加血
  215. /// </summary>
  216. /// <param name="add">欲加量</param>
  217. /// <returns>加操作是否成功</returns>
  218. public bool TryAddHp(int add)
  219. {
  220. if (hp < MaxHp)
  221. {
  222. lock (gameObjLock)
  223. hp = MaxHp > hp + add ? hp + add : MaxHp;
  224. Debugger.Output(this, " hp has added to: " + hp.ToString());
  225. return true;
  226. }
  227. return false;
  228. }
  229. /// <summary>
  230. /// 尝试减血
  231. /// </summary>
  232. /// <param name="sub">减血量</param>
  233. /// <returns>减操作是否成功</returns>
  234. public int TrySubHp(int sub)
  235. {
  236. int previousHp = hp;
  237. lock (gameObjLock)
  238. hp = hp >= sub ? 0 : hp - sub;
  239. Debugger.Output(this, " hp has subed to: " + hp.ToString());
  240. return previousHp - hp;
  241. }
  242. /* /// <summary>
  243. /// 增加死亡次数
  244. /// </summary>
  245. /// <returns>当前死亡次数</returns>
  246. private int AddDeathCount()
  247. {
  248. lock (gameObjLock)
  249. {
  250. ++deathCount;
  251. return deathCount;
  252. }
  253. }*/
  254. /// <summary>
  255. /// 加分
  256. /// </summary>
  257. /// <param name="add">增加量</param>
  258. public void AddScore(int add)
  259. {
  260. lock (gameObjLock)
  261. {
  262. score += add;
  263. Debugger.Output(this, " 's score has been added to: " + score.ToString());
  264. }
  265. }
  266. /// <summary>
  267. /// 减分
  268. /// </summary>
  269. /// <param name="sub">减少量</param>
  270. public void SubScore(int sub)
  271. {
  272. lock (gameObjLock)
  273. {
  274. score -= sub;
  275. Debugger.Output(this, " 's score has been subed to: " + score.ToString());
  276. }
  277. }
  278. /// <summary>
  279. /// 遭受攻击
  280. /// </summary>
  281. /// <param name="subHP"></param>
  282. /// <param name="hasSpear"></param>
  283. /// <param name="attacker">伤害来源</param>
  284. /// <returns>人物在受到攻击后死了吗</returns>
  285. public bool BeAttacked(Bullet bullet)
  286. {
  287. lock (beAttackedLock)
  288. {
  289. if (hp <= 0)
  290. return false; // 原来已经死了
  291. if (bullet.Parent.TeamID != this.TeamID)
  292. {
  293. if (HasShield)
  294. {
  295. if (bullet.HasSpear)
  296. _ = TrySubHp(bullet.AP);
  297. else
  298. return false;
  299. }
  300. else
  301. {
  302. bullet.Parent.HP = (int)(bullet.Parent.HP + (bullet.Parent.Vampire * TrySubHp(bullet.AP)));
  303. }
  304. #if DEBUG
  305. Console.WriteLine($"PlayerID:{ID} is being shot! Now his hp is {hp}.");
  306. #endif
  307. if (hp <= 0)
  308. TryActivatingLIFE(); // 如果有复活甲
  309. }
  310. return hp <= 0;
  311. }
  312. }
  313. /// <summary>
  314. /// 攻击被反弹,反弹伤害不会再被反弹
  315. /// </summary>
  316. /// <param name="subHP"></param>
  317. /// <param name="hasSpear"></param>
  318. /// <param name="bouncer">反弹伤害者</param>
  319. /// <returns>是否因反弹伤害而死</returns>
  320. private bool BeBounced(int subHP, bool hasSpear, Character? bouncer)
  321. {
  322. lock (beAttackedLock)
  323. {
  324. if (hp <= 0)
  325. return false;
  326. if (!(bouncer?.TeamID == this.TeamID))
  327. {
  328. if (hasSpear || !HasShield)
  329. _ = TrySubHp(subHP);
  330. if (hp <= 0)
  331. TryActivatingLIFE();
  332. }
  333. return hp <= 0;
  334. }
  335. }
  336. /// <summary>
  337. /// 角色所属队伍ID
  338. /// </summary>
  339. private long teamID = long.MaxValue;
  340. public long TeamID
  341. {
  342. get => teamID;
  343. set
  344. {
  345. lock (gameObjLock)
  346. {
  347. teamID = value;
  348. Debugger.Output(this, " joins in the team: " + value.ToString());
  349. }
  350. }
  351. }
  352. private long playerID = long.MaxValue;
  353. public long PlayerID
  354. {
  355. get => playerID;
  356. set
  357. {
  358. lock (gameObjLock)
  359. {
  360. playerID = value;
  361. }
  362. }
  363. }
  364. /// <summary>
  365. /// 角色携带的信息
  366. /// </summary>
  367. private string message = "THUAI6";
  368. public string Message
  369. {
  370. get => message;
  371. set
  372. {
  373. lock (gameObjLock)
  374. {
  375. message = value;
  376. }
  377. }
  378. }
  379. #endregion
  380. #region 角色拥有的buff相关属性、方法
  381. public void AddMoveSpeed(int buffTime, double add = 2.0) => buffManager.AddMoveSpeed(add, buffTime, newVal =>
  382. { MoveSpeed = newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed; },
  383. OrgMoveSpeed);
  384. public bool HasFasterSpeed => buffManager.HasFasterSpeed;
  385. public void AddShield(int shieldTime) => buffManager.AddShield(shieldTime);
  386. public bool HasShield => buffManager.HasShield;
  387. public void AddLIFE(int LIFETime) => buffManager.AddLIFE(LIFETime);
  388. public bool HasLIFE => buffManager.HasLIFE;
  389. public void AddSpear(int spearTime) => buffManager.AddSpear(spearTime);
  390. public bool HasSpear => buffManager.HasSpear;
  391. private Array buffTypeArray = Enum.GetValues(typeof(BuffType));
  392. public Dictionary<BuffType, bool> Buff
  393. {
  394. get
  395. {
  396. Dictionary<BuffType, bool> buff = new Dictionary<BuffType, bool>();
  397. foreach (BuffType type in buffTypeArray)
  398. {
  399. if (type != BuffType.Null)
  400. buff.Add(type, GetBuffStatus(type));
  401. }
  402. return buff;
  403. }
  404. }
  405. private bool GetBuffStatus(BuffType type)
  406. {
  407. switch (type)
  408. {
  409. case BuffType.Spear:
  410. return this.HasSpear;
  411. case BuffType.AddSpeed:
  412. return this.HasFasterSpeed;
  413. case BuffType.Shield:
  414. return this.HasShield;
  415. case BuffType.AddLIFE:
  416. return this.HasLIFE;
  417. default:
  418. return false;
  419. }
  420. }
  421. private void TryActivatingLIFE()
  422. {
  423. if (buffManager.TryActivatingLIFE())
  424. {
  425. hp = MaxHp;
  426. }
  427. }
  428. #endregion
  429. public override void Reset() // 要加锁吗?
  430. {
  431. lock (gameObjLock)
  432. {
  433. // _ = AddDeathCount();
  434. base.Reset();
  435. this.MoveSpeed = OrgMoveSpeed;
  436. HP = MaxHp;
  437. PropInventory = null;
  438. BulletOfPlayer = OriBulletOfPlayer;
  439. lock (gameObjLock)
  440. bulletNum = maxBulletNum;
  441. buffManager.ClearAll();
  442. IsInvisible = false;
  443. this.Vampire = this.OriVampire;
  444. }
  445. }
  446. public void Die(PlayerStateType playerStateType)
  447. {
  448. lock (gameObjLock)
  449. {
  450. playerState = playerStateType;
  451. CanMove = false;
  452. IsResetting = true;
  453. }
  454. }
  455. public override bool IsRigid => true;
  456. public override ShapeType Shape => ShapeType.Circle;
  457. protected override bool IgnoreCollideExecutor(IGameObj targetObj)
  458. {
  459. if (IsResetting)
  460. return true;
  461. if (targetObj.Type == GameObjType.Prop) // 自己队的地雷忽略碰撞
  462. {
  463. return true;
  464. }
  465. return false;
  466. }
  467. }
  468. }