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

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