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

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