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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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
  313. && playerState != PlayerStateType.Stunned && playerState != PlayerStateType.Charmed);
  314. }
  315. }
  316. public bool CanPinDown()
  317. {
  318. lock (actionLock)
  319. {
  320. return (playerState != PlayerStateType.Deceased && playerState != PlayerStateType.Escaped
  321. && playerState != PlayerStateType.Addicted && playerState != PlayerStateType.Rescued
  322. && playerState != PlayerStateType.Stunned && playerState != PlayerStateType.Charmed);
  323. }
  324. }
  325. public bool InteractingWithMapWithoutMoving()
  326. {
  327. lock (actionLock)
  328. {
  329. return (playerState == PlayerStateType.LockingOrOpeningTheDoor || playerState == PlayerStateType.Fixing || playerState == PlayerStateType.OpeningTheChest);
  330. }
  331. }
  332. public bool NullOrMoving()
  333. {
  334. lock (actionLock)
  335. {
  336. return (playerState == PlayerStateType.Null || playerState == PlayerStateType.Moving);
  337. }
  338. }
  339. public bool CanBeAwed()
  340. {
  341. lock (actionLock)
  342. return !(playerState == PlayerStateType.Deceased || playerState == PlayerStateType.Escaped
  343. || playerState == PlayerStateType.Addicted
  344. || playerState == PlayerStateType.Rescued || playerState == PlayerStateType.Treated
  345. || playerState == PlayerStateType.Stunned || playerState == PlayerStateType.Charmed
  346. || playerState == PlayerStateType.Null || playerState == PlayerStateType.Moving);
  347. }
  348. private GameObj? whatInteractingWith = null;
  349. public GameObj? WhatInteractingWith => whatInteractingWith;
  350. public long ChangePlayerState(PlayerStateType value = PlayerStateType.Null, GameObj? gameObj = null)
  351. {
  352. //只能被SetPlayerState引用
  353. whatInteractingWith = gameObj;
  354. if (value != PlayerStateType.Moving)
  355. IsMoving = false;
  356. playerState = (value == PlayerStateType.Moving) ? PlayerStateType.Null : value;
  357. //Debugger.Output(this,playerState.ToString()+" "+IsMoving.ToString());
  358. return ++stateNum;
  359. }
  360. public long ChangePlayerStateInOneThread(PlayerStateType value = PlayerStateType.Null, GameObj? gameObj = null)
  361. {
  362. //只能被SetPlayerState引用
  363. whatInteractingWith = gameObj;
  364. if (value != PlayerStateType.Moving)
  365. IsMoving = false;
  366. playerState = (value == PlayerStateType.Moving) ? PlayerStateType.Null : value;
  367. //Debugger.Output(this,playerState.ToString()+" "+IsMoving.ToString());
  368. return stateNum;
  369. }
  370. public long SetPlayerStateNaturally()
  371. {
  372. lock (actionLock)
  373. {
  374. whatInteractingWith = null;
  375. IsMoving = false;
  376. playerState = PlayerStateType.Null;
  377. return ++stateNum;
  378. }
  379. }
  380. public void RemoveFromGame(PlayerStateType playerStateType)
  381. {
  382. MoveReaderWriterLock.EnterWriteLock();
  383. try
  384. {
  385. lock (actionLock)
  386. {
  387. playerState = playerStateType;
  388. canMove = false;
  389. isRemoved = true;
  390. position = GameData.PosWhoDie;
  391. }
  392. }
  393. finally
  394. {
  395. MoveReaderWriterLock.ExitWriteLock();
  396. }
  397. }
  398. #endregion
  399. private int score = 0;
  400. public int Score
  401. {
  402. get => score;
  403. }
  404. /// <summary>
  405. /// 加分
  406. /// </summary>
  407. /// <param name="add">增加量</param>
  408. public virtual void AddScore(int add)
  409. {
  410. lock (gameObjLock)
  411. {
  412. score += add;
  413. //Debugger.Output(this, " 's score has been added to: " + score.ToString());
  414. }
  415. }
  416. /// <summary>
  417. /// 角色所属队伍ID
  418. /// </summary>
  419. private int teamID = int.MaxValue;
  420. public int TeamID
  421. {
  422. get => teamID;
  423. set
  424. {
  425. lock (gameObjLock)
  426. {
  427. teamID = value;
  428. Debugger.Output(this, " joins in the team: " + value.ToString());
  429. }
  430. }
  431. }
  432. private int playerID = int.MaxValue;
  433. public int PlayerID
  434. {
  435. get => playerID;
  436. set
  437. {
  438. lock (gameObjLock)
  439. {
  440. playerID = value;
  441. }
  442. }
  443. }
  444. #region 道具和buff相关属性、方法
  445. private Prop[] propInventory = new Prop[GameData.maxNumOfPropInPropInventory]
  446. {new NullProp(), new NullProp(),new NullProp() };
  447. public Prop[] PropInventory
  448. {
  449. get => propInventory;
  450. set
  451. {
  452. lock (gameObjLock)
  453. {
  454. propInventory = value;
  455. Debugger.Output(this, " prop becomes " + (PropInventory == null ? "null" : PropInventory.ToString()));
  456. }
  457. }
  458. }
  459. /// <summary>
  460. /// 使用物品栏中的道具
  461. /// </summary>
  462. /// <returns>被使用的道具</returns>
  463. public Prop UseProp(int indexing)
  464. {
  465. if (indexing < 0 || indexing >= GameData.maxNumOfPropInPropInventory)
  466. return new NullProp();
  467. lock (gameObjLock)
  468. {
  469. Prop prop = propInventory[indexing];
  470. PropInventory[indexing] = new NullProp();
  471. return prop;
  472. }
  473. }
  474. public Prop UseProp(PropType propType)
  475. {
  476. lock (gameObjLock)
  477. {
  478. if (propType == PropType.Null)
  479. {
  480. for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  481. {
  482. if (PropInventory[indexing].GetPropType() != PropType.Null)
  483. {
  484. Prop prop = PropInventory[indexing];
  485. PropInventory[indexing] = new NullProp();
  486. return prop;
  487. }
  488. }
  489. }
  490. else
  491. for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  492. {
  493. if (PropInventory[indexing].GetPropType() == propType)
  494. {
  495. Prop prop = PropInventory[indexing];
  496. PropInventory[indexing] = new NullProp();
  497. return prop;
  498. }
  499. }
  500. return new NullProp();
  501. }
  502. }
  503. /// <summary>
  504. /// 如果indexing==GameData.maxNumOfPropInPropInventory表明道具栏为满
  505. /// </summary>
  506. public int IndexingOfAddProp()
  507. {
  508. int indexing = 0;
  509. for (; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  510. if (PropInventory[indexing].GetPropType() == PropType.Null)
  511. break;
  512. return indexing;
  513. }
  514. public void AddMoveSpeed(int buffTime, double add = 1.0) => buffManager.AddMoveSpeed(add, buffTime, newVal =>
  515. { MoveSpeed = newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed; },
  516. OrgMoveSpeed);
  517. public bool HasFasterSpeed => buffManager.HasFasterSpeed;
  518. public void AddShield(int shieldTime) => buffManager.AddShield(shieldTime);
  519. public bool HasShield => buffManager.HasShield;
  520. public void AddLife(int LIFETime) => buffManager.AddLife(LIFETime);
  521. public bool HasLIFE => buffManager.HasLIFE;
  522. public void AddAp(int time) => buffManager.AddAp(time);
  523. public bool HasAp => buffManager.HasAp;
  524. public void AddSpear(int spearTime) => buffManager.AddSpear(spearTime);
  525. public bool HasSpear => buffManager.HasSpear;
  526. public void AddClairaudience(int time) => buffManager.AddClairaudience(time);
  527. public bool HasClairaudience => buffManager.HasClairaudience;
  528. public void AddInvisible(int time) => buffManager.AddInvisible(time);
  529. public bool HasInvisible => buffManager.HasInvisible;
  530. private Array buffTypeArray = Enum.GetValues(typeof(BuffType));
  531. public Dictionary<BuffType, bool> Buff
  532. {
  533. get
  534. {
  535. Dictionary<BuffType, bool> buff = new Dictionary<BuffType, bool>();
  536. foreach (BuffType type in buffTypeArray)
  537. {
  538. if (type != BuffType.Null)
  539. buff.Add(type, GetBuffStatus(type));
  540. }
  541. return buff;
  542. }
  543. }
  544. private bool GetBuffStatus(BuffType type)
  545. {
  546. switch (type)
  547. {
  548. case BuffType.Spear:
  549. return this.HasSpear;
  550. case BuffType.AddSpeed:
  551. return this.HasFasterSpeed;
  552. case BuffType.Shield:
  553. return this.HasShield;
  554. case BuffType.AddLife:
  555. return this.HasLIFE;
  556. case BuffType.AddAp:
  557. return this.HasAp;
  558. case BuffType.Clairaudience:
  559. return this.HasClairaudience;
  560. case BuffType.Invisible:
  561. return this.HasInvisible;
  562. default:
  563. return false;
  564. }
  565. }
  566. public void TryActivatingLIFE()
  567. {
  568. if (buffManager.TryActivatingLIFE())
  569. {
  570. AddScore(GameData.ScorePropRemainHp);
  571. hp = GameData.RemainHpWhenAddLife;
  572. }
  573. }
  574. public bool TryAddAp()
  575. {
  576. if (buffManager.TryAddAp())
  577. {
  578. AddScore(GameData.ScorePropAddAp);
  579. return true;
  580. }
  581. return false;
  582. }
  583. public bool TryUseSpear()
  584. {
  585. return buffManager.TryUseSpear();
  586. }
  587. public bool TryUseShield()
  588. {
  589. if (buffManager.TryUseShield())
  590. {
  591. AddScore(GameData.ScorePropUseShield);
  592. return true;
  593. }
  594. return false;
  595. }
  596. #endregion
  597. /* public override void Reset() // 要加锁吗?
  598. {
  599. lock (gameObjLock)
  600. {
  601. // _ = AddDeathCount();
  602. base.Reset();
  603. this.MoveSpeed = OrgMoveSpeed;
  604. HP = MaxHp;
  605. PropInventory = null;
  606. BulletOfPlayer = OriBulletOfPlayer;
  607. lock (gameObjLock)
  608. bulletNum = maxBulletNum;
  609. buffManager.ClearAll();
  610. IsInvisible = false;
  611. this.Vampire = this.OriVampire;
  612. }
  613. }*/
  614. public override bool IsRigid => true;
  615. public override ShapeType Shape => ShapeType.Circle;
  616. public override bool IgnoreCollideExecutor(IGameObj targetObj)
  617. {
  618. if (IsRemoved)
  619. return true;
  620. if (targetObj.Type == GameObjType.Prop)
  621. {
  622. return true;
  623. }
  624. if (targetObj.Type == GameObjType.Character && XY.DistanceCeil3(targetObj.Position, this.Position) < this.Radius + targetObj.Radius - GameData.adjustLength)
  625. return true;
  626. return false;
  627. }
  628. }
  629. }