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

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