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

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