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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 : Moveable, ICharacter // 负责人LHR摆烂终了
  11. {
  12. #region 装弹、攻击相关的基本属性及方法
  13. protected readonly object beAttackedLock = new();
  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 readonly BulletType OriBulletOfPlayer;
  36. private BulletType bulletOfPlayer;
  37. public BulletType BulletOfPlayer
  38. {
  39. get => bulletOfPlayer;
  40. set
  41. {
  42. lock (gameObjLock)
  43. {
  44. bulletOfPlayer = value;
  45. OrgCD = (BulletFactory.BulletCD(value));
  46. CD = 0;
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// 进行一次攻击
  52. /// </summary>
  53. /// <returns>攻击操作发出的子弹</returns>
  54. public Bullet? Attack(XY pos, PlaceType place)
  55. {
  56. if (TrySubBulletNum())
  57. return BulletFactory.GetBullet(this, place, pos);
  58. else
  59. return null;
  60. }
  61. /// <summary>
  62. /// 尝试将子弹数量减1
  63. /// </summary>
  64. /// <returns>减操作是否成功</returns>
  65. private bool TrySubBulletNum()
  66. {
  67. lock (gameObjLock)
  68. {
  69. if (bulletNum > 0)
  70. {
  71. --bulletNum;
  72. return true;
  73. }
  74. return false;
  75. }
  76. }
  77. /// <summary>
  78. /// 尝试将子弹数量加1
  79. /// </summary>
  80. /// <returns>加操作是否成功</returns>
  81. public bool TryAddBulletNum()
  82. {
  83. lock (gameObjLock)
  84. {
  85. if (bulletNum < maxBulletNum)
  86. {
  87. ++bulletNum;
  88. return true;
  89. }
  90. return false;
  91. }
  92. }
  93. /*
  94. /// <summary>
  95. /// 攻击被反弹,反弹伤害不会再被反弹
  96. /// </summary>
  97. /// <param name="subHP"></param>
  98. /// <param name="hasSpear"></param>
  99. /// <param name="bouncer">反弹伤害者</param>
  100. /// <returns>是否因反弹伤害而死</returns>
  101. private bool BeBounced(int subHP, bool hasSpear, Character? bouncer)
  102. {
  103. lock (beAttackedLock)
  104. {
  105. if (hp <= 0)
  106. return false;
  107. if (!(bouncer?.TeamID == this.TeamID))
  108. {
  109. if (hasSpear || !HasShield)
  110. _ = TrySubHp(subHP);
  111. if (hp <= 0)
  112. TryActivatingLIFE();
  113. }
  114. return hp <= 0;
  115. }
  116. }*/
  117. #endregion
  118. #region 感知相关的基本属性及方法
  119. private Dictionary<BgmType, double> bgmDictionary = new();
  120. public Dictionary<BgmType, double> BgmDictionary
  121. {
  122. get => bgmDictionary;
  123. private set
  124. {
  125. lock (gameObjLock)
  126. {
  127. bgmDictionary = value;
  128. }
  129. }
  130. }
  131. public void AddBgm(BgmType bgm, double value)
  132. {
  133. if (BgmDictionary.ContainsKey(bgm))
  134. BgmDictionary[bgm] = value;
  135. else BgmDictionary.Add(bgm, value);
  136. }
  137. private int alertnessRadius;
  138. public int AlertnessRadius
  139. {
  140. get => alertnessRadius;
  141. set
  142. {
  143. lock (gameObjLock)
  144. {
  145. alertnessRadius = value;
  146. }
  147. }
  148. }
  149. private double concealment;
  150. public double Concealment
  151. {
  152. get => concealment;
  153. set
  154. {
  155. lock (gameObjLock)
  156. {
  157. concealment = value;
  158. }
  159. }
  160. }
  161. private int viewRange;
  162. public int ViewRange
  163. {
  164. get => viewRange;
  165. set
  166. {
  167. lock (gameObjLock)
  168. {
  169. viewRange = (value > 0) ? value : 0;
  170. }
  171. }
  172. }
  173. #endregion
  174. #region 交互相关的基本属性及方法
  175. private int speedOfOpeningOrLocking;
  176. public int SpeedOfOpeningOrLocking
  177. {
  178. get => speedOfOpeningOrLocking;
  179. set
  180. {
  181. lock (gameObjLock)
  182. {
  183. speedOfOpeningOrLocking = value;
  184. }
  185. }
  186. }
  187. private int speedOfClimbingThroughWindows;
  188. public int SpeedOfClimbingThroughWindows
  189. {
  190. get => speedOfClimbingThroughWindows;
  191. set
  192. {
  193. lock (gameObjLock)
  194. {
  195. speedOfClimbingThroughWindows = value;
  196. }
  197. }
  198. }
  199. private int speedOfOpenChest;
  200. public int SpeedOfOpenChest
  201. {
  202. get => speedOfOpenChest;
  203. set
  204. {
  205. lock (gameObjLock)
  206. {
  207. speedOfOpenChest = value;
  208. }
  209. }
  210. }
  211. #endregion
  212. #region 血量相关的基本属性及方法
  213. public int MaxHp { get; protected set; } // 最大血量
  214. protected int hp;
  215. public int HP
  216. {
  217. get => hp;
  218. set
  219. {
  220. if (value > 0)
  221. {
  222. lock (gameObjLock)
  223. hp = value <= MaxHp ? value : MaxHp;
  224. }
  225. else
  226. lock (gameObjLock)
  227. hp = 0;
  228. }
  229. }
  230. /// <summary>
  231. /// 尝试加血
  232. /// </summary>
  233. /// <param name="add">欲加量</param>
  234. /// <returns>加操作是否成功</returns>
  235. public bool TryAddHp(int add)
  236. {
  237. if (hp < MaxHp)
  238. {
  239. lock (gameObjLock)
  240. hp = MaxHp > hp + add ? hp + add : MaxHp;
  241. Debugger.Output(this, " hp has added to: " + hp.ToString());
  242. return true;
  243. }
  244. return false;
  245. }
  246. /// <summary>
  247. /// 尝试减血
  248. /// </summary>
  249. /// <param name="sub">减血量</param>
  250. /// <returns>减操作是否成功</returns>
  251. public int TrySubHp(int sub)
  252. {
  253. int previousHp = hp;
  254. lock (gameObjLock)
  255. hp = hp <= sub ? 0 : hp - sub;
  256. Debugger.Output(this, " hp has subed to: " + hp.ToString());
  257. return previousHp - hp;
  258. }
  259. private double vampire = 0; // 回血率:0-1之间
  260. public double Vampire
  261. {
  262. get => vampire;
  263. set
  264. {
  265. if (value > 1)
  266. lock (gameObjLock)
  267. vampire = 1;
  268. else if (value < 0)
  269. lock (gameObjLock)
  270. vampire = 0;
  271. else
  272. lock (gameObjLock)
  273. vampire = value;
  274. }
  275. }
  276. private double oriVampire = 0;
  277. public double OriVampire
  278. {
  279. get => oriVampire;
  280. set
  281. {
  282. if (value > 1)
  283. lock (gameObjLock)
  284. vampire = 1;
  285. else if (value < 0)
  286. lock (gameObjLock)
  287. vampire = 0;
  288. else
  289. lock (gameObjLock)
  290. vampire = value;
  291. }
  292. }
  293. #endregion
  294. private PlayerStateType playerState = PlayerStateType.Null;
  295. public PlayerStateType PlayerState
  296. {
  297. get
  298. {
  299. if (playerState == PlayerStateType.Null && IsMoving) return PlayerStateType.Moving;
  300. return playerState;
  301. }
  302. set
  303. {
  304. if (value != PlayerStateType.Moving)
  305. lock (gameObjLock)
  306. IsMoving = false;
  307. lock (gameObjLock) playerState = (value == PlayerStateType.Moving) ? PlayerStateType.Null : value;
  308. }
  309. }
  310. public bool NoHp() => (playerState == PlayerStateType.Deceased || playerState == PlayerStateType.Escaped
  311. || playerState == PlayerStateType.Addicted || playerState == PlayerStateType.Rescued);
  312. public bool Commandable() => (playerState != PlayerStateType.Deceased && playerState != PlayerStateType.Escaped
  313. && playerState != PlayerStateType.Addicted && playerState != PlayerStateType.Rescued
  314. && playerState != PlayerStateType.Swinging && playerState != PlayerStateType.TryingToAttack
  315. && playerState != PlayerStateType.ClimbingThroughWindows && playerState != PlayerStateType.Stunned);
  316. public bool InteractingWithMapWithoutMoving() => (playerState == PlayerStateType.LockingOrOpeningTheDoor || playerState == PlayerStateType.Fixing || playerState == PlayerStateType.OpeningTheChest);
  317. public bool NullOrMoving() => (playerState == PlayerStateType.Null || playerState == PlayerStateType.Moving);
  318. public bool CanBeAwed() => !(playerState == PlayerStateType.Deceased || playerState == PlayerStateType.Escaped
  319. || playerState == PlayerStateType.Addicted || playerState == PlayerStateType.Rescued
  320. || playerState == PlayerStateType.Treated || playerState != PlayerStateType.Stunned
  321. || playerState == PlayerStateType.Null || playerState == PlayerStateType.Moving);
  322. private int score = 0;
  323. public int Score
  324. {
  325. get => score;
  326. }
  327. /// <summary>
  328. /// 加分
  329. /// </summary>
  330. /// <param name="add">增加量</param>
  331. public void AddScore(int add)
  332. {
  333. lock (gameObjLock)
  334. {
  335. score += add;
  336. //Debugger.Output(this, " 's score has been added to: " + score.ToString());
  337. }
  338. }
  339. /// <summary>
  340. /// 减分
  341. /// </summary>
  342. /// <param name="sub">减少量</param>
  343. public void SubScore(int sub)
  344. {
  345. lock (gameObjLock)
  346. {
  347. score -= sub;
  348. Debugger.Output(this, " 's score has been subed to: " + score.ToString());
  349. }
  350. }
  351. /// <summary>
  352. /// 角色所属队伍ID
  353. /// </summary>
  354. private long teamID = long.MaxValue;
  355. public long TeamID
  356. {
  357. get => teamID;
  358. set
  359. {
  360. lock (gameObjLock)
  361. {
  362. teamID = value;
  363. Debugger.Output(this, " joins in the team: " + value.ToString());
  364. }
  365. }
  366. }
  367. private long playerID = long.MaxValue;
  368. public long PlayerID
  369. {
  370. get => playerID;
  371. set
  372. {
  373. lock (gameObjLock)
  374. {
  375. playerID = value;
  376. }
  377. }
  378. }
  379. /// <summary>
  380. /// 角色携带的信息
  381. /// </summary>
  382. private string message = "THUAI6";
  383. public string Message
  384. {
  385. get => message;
  386. set
  387. {
  388. lock (gameObjLock)
  389. {
  390. message = value;
  391. }
  392. }
  393. }
  394. #region 道具和buff相关属性、方法
  395. private Prop[] propInventory = new Prop[GameData.maxNumOfPropInPropInventory]
  396. {new NullProp(), new NullProp(),new NullProp() };
  397. public Prop[] PropInventory
  398. {
  399. get => propInventory;
  400. set
  401. {
  402. lock (gameObjLock)
  403. {
  404. propInventory = value;
  405. Debugger.Output(this, " prop becomes " + (PropInventory == null ? "null" : PropInventory.ToString()));
  406. }
  407. }
  408. }
  409. /// <summary>
  410. /// 使用物品栏中的道具
  411. /// </summary>
  412. /// <returns>被使用的道具</returns>
  413. public Prop UseProp(int indexing)
  414. {
  415. if (indexing < 0 || indexing >= GameData.maxNumOfPropInPropInventory)
  416. return new NullProp();
  417. lock (gameObjLock)
  418. {
  419. Prop prop = propInventory[indexing];
  420. PropInventory[indexing] = new NullProp();
  421. return prop;
  422. }
  423. }
  424. public Prop UseProp(PropType propType)
  425. {
  426. lock (gameObjLock)
  427. {
  428. if (propType == PropType.Null)
  429. {
  430. for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  431. {
  432. if (PropInventory[indexing].GetPropType() != PropType.Null)
  433. {
  434. Prop prop = PropInventory[indexing];
  435. PropInventory[indexing] = new NullProp();
  436. return prop;
  437. }
  438. }
  439. }
  440. else
  441. for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  442. {
  443. if (PropInventory[indexing].GetPropType() == propType)
  444. {
  445. Prop prop = PropInventory[indexing];
  446. PropInventory[indexing] = new NullProp();
  447. return prop;
  448. }
  449. }
  450. return new NullProp();
  451. }
  452. }
  453. /// <summary>
  454. /// 如果indexing==GameData.maxNumOfPropInPropInventory表明道具栏为满
  455. /// </summary>
  456. public int IndexingOfAddProp()
  457. {
  458. int indexing = 0;
  459. for (; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  460. if (PropInventory[indexing].GetPropType() == PropType.Null)
  461. break;
  462. return indexing;
  463. }
  464. public void AddMoveSpeed(int buffTime, double add = 2.0) => buffManager.AddMoveSpeed(add, buffTime, newVal =>
  465. { MoveSpeed = newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed; },
  466. OrgMoveSpeed);
  467. public bool HasFasterSpeed => buffManager.HasFasterSpeed;
  468. public void AddShield(int shieldTime) => buffManager.AddShield(shieldTime);
  469. public bool HasShield => buffManager.HasShield;
  470. public void AddLIFE(int LIFETime) => buffManager.AddLIFE(LIFETime);
  471. public bool HasLIFE => buffManager.HasLIFE;
  472. public void AddAp(int time) => buffManager.AddAp(time);
  473. public bool HasAp => buffManager.HasAp;
  474. public void AddSpear(int spearTime) => buffManager.AddSpear(spearTime);
  475. public bool HasSpear => buffManager.HasSpear;
  476. public void AddClairaudience(int time) => buffManager.AddClairaudience(time);
  477. public bool HasClairaudience => buffManager.HasClairaudience;
  478. public void AddInvisible(int time) => buffManager.AddInvisible(time);
  479. public bool HasInvisible => buffManager.HasInvisible;
  480. private Array buffTypeArray = Enum.GetValues(typeof(BuffType));
  481. public Dictionary<BuffType, bool> Buff
  482. {
  483. get
  484. {
  485. Dictionary<BuffType, bool> buff = new Dictionary<BuffType, bool>();
  486. foreach (BuffType type in buffTypeArray)
  487. {
  488. if (type != BuffType.Null)
  489. buff.Add(type, GetBuffStatus(type));
  490. }
  491. return buff;
  492. }
  493. }
  494. private bool GetBuffStatus(BuffType type)
  495. {
  496. switch (type)
  497. {
  498. case BuffType.Spear:
  499. return this.HasSpear;
  500. case BuffType.AddSpeed:
  501. return this.HasFasterSpeed;
  502. case BuffType.Shield:
  503. return this.HasShield;
  504. case BuffType.AddLIFE:
  505. return this.HasLIFE;
  506. default:
  507. return false;
  508. }
  509. }
  510. protected void TryActivatingLIFE()
  511. {
  512. if (buffManager.TryActivatingLIFE())
  513. {
  514. AddScore(GameData.ScorePropRemainHp);
  515. hp = GameData.RemainHpWhenAddLife;
  516. }
  517. }
  518. public bool TryAddAp()
  519. {
  520. if (buffManager.TryAddAp())
  521. {
  522. AddScore(GameData.ScorePropAddAp);
  523. return true;
  524. }
  525. return false;
  526. }
  527. public bool TryUseSpear()
  528. {
  529. return buffManager.TryUseSpear();
  530. }
  531. public bool TryUseShield()
  532. {
  533. if (buffManager.TryUseShield())
  534. {
  535. AddScore(GameData.ScorePropUseShield);
  536. return true;
  537. }
  538. return false;
  539. }
  540. #endregion
  541. /* public override void Reset() // 要加锁吗?
  542. {
  543. lock (gameObjLock)
  544. {
  545. // _ = AddDeathCount();
  546. base.Reset();
  547. this.MoveSpeed = OrgMoveSpeed;
  548. HP = MaxHp;
  549. PropInventory = null;
  550. BulletOfPlayer = OriBulletOfPlayer;
  551. lock (gameObjLock)
  552. bulletNum = maxBulletNum;
  553. buffManager.ClearAll();
  554. IsInvisible = false;
  555. this.Vampire = this.OriVampire;
  556. }
  557. }*/
  558. public void Die(PlayerStateType playerStateType)
  559. {
  560. lock (gameObjLock)
  561. {
  562. playerState = playerStateType;
  563. CanMove = false;
  564. IsResetting = true;
  565. Position = GameData.PosWhoDie;
  566. place = PlaceType.Grass;
  567. }
  568. }
  569. public override bool IsRigid => true;
  570. public override ShapeType Shape => ShapeType.Circle;
  571. protected override bool IgnoreCollideExecutor(IGameObj targetObj)
  572. {
  573. if (IsResetting)
  574. return true;
  575. if (targetObj.Type == GameObjType.Prop) // 自己队的地雷忽略碰撞
  576. {
  577. return true;
  578. }
  579. return false;
  580. }
  581. }
  582. }