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

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