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

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