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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. using Preparation.Interface;
  2. using Preparation.Utility;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. namespace GameClass.GameObj
  8. {
  9. public partial class Character : GameObj, ICharacter // 负责人LHR摆烂终了
  10. {
  11. private readonly object beAttackedLock = new();
  12. #region 角色的基本属性及方法,包括与道具的交互方法
  13. /// <summary>
  14. /// 装弹冷却
  15. /// </summary>
  16. protected int cd;
  17. public int CD
  18. {
  19. get => cd;
  20. private
  21. 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;
  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 bool TrySubHp(int sub)
  257. {
  258. if (hp > 0)
  259. {
  260. lock (gameObjLock)
  261. hp = 0 >= hp - sub ? 0 : hp - sub;
  262. Debugger.Output(this, " hp has subed to: " + hp.ToString());
  263. return true;
  264. }
  265. return false;
  266. }
  267. /* /// <summary>
  268. /// 增加死亡次数
  269. /// </summary>
  270. /// <returns>当前死亡次数</returns>
  271. private int AddDeathCount()
  272. {
  273. lock (gameObjLock)
  274. {
  275. ++deathCount;
  276. return deathCount;
  277. }
  278. }*/
  279. /// <summary>
  280. /// 加分
  281. /// </summary>
  282. /// <param name="add">增加量</param>
  283. public void AddScore(int add)
  284. {
  285. lock (gameObjLock)
  286. {
  287. score += add;
  288. Debugger.Output(this, " 's score has been added to: " + score.ToString());
  289. }
  290. }
  291. /// <summary>
  292. /// 减分
  293. /// </summary>
  294. /// <param name="sub">减少量</param>
  295. public void SubScore(int sub)
  296. {
  297. lock (gameObjLock)
  298. {
  299. score -= sub;
  300. Debugger.Output(this, " 's score has been subed to: " + score.ToString());
  301. }
  302. }
  303. /// <summary>
  304. /// 遭受攻击
  305. /// </summary>
  306. /// <param name="subHP"></param>
  307. /// <param name="hasSpear"></param>
  308. /// <param name="attacker">伤害来源</param>
  309. /// <returns>人物在受到攻击后死了吗</returns>
  310. public bool BeAttack(Bullet bullet)
  311. {
  312. lock (beAttackedLock)
  313. {
  314. if (hp <= 0)
  315. return false; // 原来已经死了
  316. if (bullet.Parent.TeamID != this.TeamID)
  317. {
  318. if (HasShield)
  319. {
  320. if (bullet.HasSpear)
  321. _ = TrySubHp(bullet.AP);
  322. else
  323. return false;
  324. }
  325. else
  326. {
  327. TrySubHp(bullet.AP);
  328. }
  329. #if DEBUG
  330. Console.WriteLine($"PlayerID:{ID} is being shot! Now his hp is {hp}.");
  331. #endif
  332. if (hp <= 0)
  333. TryActivatingLIFE(); // 如果有复活甲
  334. }
  335. return hp <= 0;
  336. }
  337. }
  338. /// <summary>
  339. /// 攻击被反弹,反弹伤害不会再被反弹
  340. /// </summary>
  341. /// <param name="subHP"></param>
  342. /// <param name="hasSpear"></param>
  343. /// <param name="bouncer">反弹伤害者</param>
  344. /// <returns>是否因反弹伤害而死</returns>
  345. private bool BeBounced(int subHP, bool hasSpear, Character? bouncer)
  346. {
  347. lock (beAttackedLock)
  348. {
  349. if (hp <= 0)
  350. return false;
  351. if (!(bouncer?.TeamID == this.TeamID))
  352. {
  353. if (hasSpear || !HasShield)
  354. _ = TrySubHp(subHP);
  355. if (hp <= 0)
  356. TryActivatingLIFE();
  357. }
  358. return hp <= 0;
  359. }
  360. }
  361. /// <summary>
  362. /// 角色所属队伍ID
  363. /// </summary>
  364. private long teamID = long.MaxValue;
  365. public long TeamID
  366. {
  367. get => teamID;
  368. set
  369. {
  370. lock (gameObjLock)
  371. {
  372. teamID = value;
  373. Debugger.Output(this, " joins in the team: " + value.ToString());
  374. }
  375. }
  376. }
  377. private long playerID = long.MaxValue;
  378. public long PlayerID
  379. {
  380. get => playerID;
  381. set
  382. {
  383. lock (gameObjLock)
  384. {
  385. playerID = value;
  386. }
  387. }
  388. }
  389. /// <summary>
  390. /// 角色携带的信息
  391. /// </summary>
  392. private string message = "THUAI6";
  393. public string Message
  394. {
  395. get => message;
  396. set
  397. {
  398. lock (gameObjLock)
  399. {
  400. message = value;
  401. }
  402. }
  403. }
  404. #endregion
  405. #region 角色拥有的buff相关属性、方法
  406. public void AddMoveSpeed(int buffTime, double add = 2.0) => buffManeger.AddMoveSpeed(add, buffTime, newVal =>
  407. { MoveSpeed = newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed; },
  408. OrgMoveSpeed);
  409. public bool HasFasterSpeed => buffManeger.HasFasterSpeed;
  410. public void AddShield(int shieldTime) => buffManeger.AddShield(shieldTime);
  411. public bool HasShield => buffManeger.HasShield;
  412. public void AddLIFE(int LIFETime) => buffManeger.AddLIFE(LIFETime);
  413. public bool HasLIFE => buffManeger.HasLIFE;
  414. public void AddSpear(int spearTime) => buffManeger.AddSpear(spearTime);
  415. public bool HasSpear => buffManeger.HasSpear;
  416. private Array buffTypeArray = Enum.GetValues(typeof(BuffType));
  417. public Dictionary<BuffType, bool> Buff
  418. {
  419. get
  420. {
  421. Dictionary<BuffType, bool> buff = new Dictionary<BuffType, bool>();
  422. foreach (BuffType type in buffTypeArray)
  423. {
  424. if (type != BuffType.Null)
  425. buff.Add(type, GetBuffStatus(type));
  426. }
  427. return buff;
  428. }
  429. }
  430. private bool GetBuffStatus(BuffType type)
  431. {
  432. switch (type)
  433. {
  434. case BuffType.Spear:
  435. return this.HasSpear;
  436. case BuffType.AddSpeed:
  437. return this.HasFasterSpeed;
  438. case BuffType.Shield:
  439. return this.HasShield;
  440. case BuffType.AddLIFE:
  441. return this.HasLIFE;
  442. default:
  443. return false;
  444. }
  445. }
  446. private void TryActivatingLIFE()
  447. {
  448. if (buffManeger.TryActivatingLIFE())
  449. {
  450. hp = MaxHp;
  451. }
  452. }
  453. #endregion
  454. public override void Reset() // 要加锁吗?
  455. {
  456. lock (gameObjLock)
  457. {
  458. // _ = AddDeathCount();
  459. base.Reset();
  460. this.MoveSpeed = OrgMoveSpeed;
  461. HP = MaxHp;
  462. PropInventory = null;
  463. BulletOfPlayer = OriBulletOfPlayer;
  464. lock (gameObjLock)
  465. bulletNum = maxBulletNum;
  466. buffManeger.ClearAll();
  467. IsInvisible = false;
  468. this.Vampire = this.OriVampire;
  469. }
  470. }
  471. public void Escape()
  472. {
  473. lock (gameObjLock)
  474. IsResetting=IsEscaped=true;
  475. }
  476. public override bool IsRigid => true;
  477. public override ShapeType Shape => ShapeType.Circle;
  478. protected override bool IgnoreCollideExecutor(IGameObj targetObj)
  479. {
  480. if (targetObj.Type == GameObjType.BirthPoint)
  481. {
  482. if (object.ReferenceEquals(((BirthPoint)targetObj).Parent, this)) // 自己的出生点可以忽略碰撞
  483. {
  484. return true;
  485. }
  486. }
  487. else if (targetObj.Type == GameObjType.Prop) // 自己队的地雷忽略碰撞
  488. {
  489. return true;
  490. }
  491. return false;
  492. }
  493. }
  494. }