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 16 kB

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