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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. using Preparation.Interface;
  2. using Preparation.Utility;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using System.Threading;
  7. namespace GameClass.GameObj
  8. {
  9. public partial class Character : Moveable, ICharacter // 负责人LHR摆烂终了
  10. {
  11. #region 装弹、攻击相关的基本属性及方法
  12. /// <summary>
  13. /// 装弹冷却
  14. /// </summary>
  15. protected int cd;
  16. public int CD
  17. {
  18. get
  19. {
  20. lock (actionLock)
  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 (actionLock)
  34. {
  35. return bulletOfPlayer;
  36. }
  37. }
  38. set
  39. {
  40. lock (actionLock)
  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 (actionLock)
  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 (actionLock)
  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 (actionLock)
  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.Sign(Math.Cos(angle)),
  92. (int)(Math.Abs((Radius + BulletFactory.BulletRadius(bulletOfPlayer)) * Math.Sin(angle))) * Math.Sign(Math.Sin(angle))
  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. public void ChangePlayerState(PlayerStateType value = PlayerStateType.Null, GameObj? gameObj = null)
  313. {
  314. lock (actionLock)
  315. {
  316. stateNum= Interlocked.Increment(ref stateNum);
  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 (actionLock)
  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 (actionLock)
  338. {
  339. stateNum= Interlocked.Increment(ref stateNum);
  340. whatInteractingWith = null;
  341. IsMoving = false;
  342. playerState = PlayerStateType.Null;
  343. }
  344. }
  345. public void RemoveFromGame(PlayerStateType playerStateType)
  346. {
  347. MoveReaderWriterLock.EnterWriteLock();
  348. try
  349. {
  350. lock (actionLock)
  351. {
  352. playerState = playerStateType;
  353. canMove = false;
  354. isResetting = true;
  355. position = GameData.PosWhoDie;
  356. }
  357. }
  358. finally
  359. {
  360. MoveReaderWriterLock.ExitWriteLock();
  361. }
  362. }
  363. #endregion
  364. private int score = 0;
  365. public int Score
  366. {
  367. get => score;
  368. }
  369. /// <summary>
  370. /// 加分
  371. /// </summary>
  372. /// <param name="add">增加量</param>
  373. public virtual void AddScore(int add)
  374. {
  375. lock (gameObjLock)
  376. {
  377. score += add;
  378. //Debugger.Output(this, " 's score has been added to: " + score.ToString());
  379. }
  380. }
  381. /// <summary>
  382. /// 角色所属队伍ID
  383. /// </summary>
  384. private int teamID = int.MaxValue;
  385. public int TeamID
  386. {
  387. get => teamID;
  388. set
  389. {
  390. lock (gameObjLock)
  391. {
  392. teamID = value;
  393. Debugger.Output(this, " joins in the team: " + value.ToString());
  394. }
  395. }
  396. }
  397. private int playerID = int.MaxValue;
  398. public int PlayerID
  399. {
  400. get => playerID;
  401. set
  402. {
  403. lock (gameObjLock)
  404. {
  405. playerID = value;
  406. }
  407. }
  408. }
  409. #region 道具和buff相关属性、方法
  410. private Prop[] propInventory = new Prop[GameData.maxNumOfPropInPropInventory]
  411. {new NullProp(), new NullProp(),new NullProp() };
  412. public Prop[] PropInventory
  413. {
  414. get => propInventory;
  415. set
  416. {
  417. lock (gameObjLock)
  418. {
  419. propInventory = value;
  420. Debugger.Output(this, " prop becomes " + (PropInventory == null ? "null" : PropInventory.ToString()));
  421. }
  422. }
  423. }
  424. /// <summary>
  425. /// 使用物品栏中的道具
  426. /// </summary>
  427. /// <returns>被使用的道具</returns>
  428. public Prop UseProp(int indexing)
  429. {
  430. if (indexing < 0 || indexing >= GameData.maxNumOfPropInPropInventory)
  431. return new NullProp();
  432. lock (gameObjLock)
  433. {
  434. Prop prop = propInventory[indexing];
  435. PropInventory[indexing] = new NullProp();
  436. return prop;
  437. }
  438. }
  439. public Prop UseProp(PropType propType)
  440. {
  441. lock (gameObjLock)
  442. {
  443. if (propType == PropType.Null)
  444. {
  445. for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  446. {
  447. if (PropInventory[indexing].GetPropType() != PropType.Null)
  448. {
  449. Prop prop = PropInventory[indexing];
  450. PropInventory[indexing] = new NullProp();
  451. return prop;
  452. }
  453. }
  454. }
  455. else
  456. for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  457. {
  458. if (PropInventory[indexing].GetPropType() == propType)
  459. {
  460. Prop prop = PropInventory[indexing];
  461. PropInventory[indexing] = new NullProp();
  462. return prop;
  463. }
  464. }
  465. return new NullProp();
  466. }
  467. }
  468. /// <summary>
  469. /// 如果indexing==GameData.maxNumOfPropInPropInventory表明道具栏为满
  470. /// </summary>
  471. public int IndexingOfAddProp()
  472. {
  473. int indexing = 0;
  474. for (; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  475. if (PropInventory[indexing].GetPropType() == PropType.Null)
  476. break;
  477. return indexing;
  478. }
  479. public void AddMoveSpeed(int buffTime, double add = 1.0) => buffManager.AddMoveSpeed(add, buffTime, newVal =>
  480. { MoveSpeed = newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed; },
  481. OrgMoveSpeed);
  482. public bool HasFasterSpeed => buffManager.HasFasterSpeed;
  483. public void AddShield(int shieldTime) => buffManager.AddShield(shieldTime);
  484. public bool HasShield => buffManager.HasShield;
  485. public void AddLife(int LIFETime) => buffManager.AddLife(LIFETime);
  486. public bool HasLIFE => buffManager.HasLIFE;
  487. public void AddAp(int time) => buffManager.AddAp(time);
  488. public bool HasAp => buffManager.HasAp;
  489. public void AddSpear(int spearTime) => buffManager.AddSpear(spearTime);
  490. public bool HasSpear => buffManager.HasSpear;
  491. public void AddClairaudience(int time) => buffManager.AddClairaudience(time);
  492. public bool HasClairaudience => buffManager.HasClairaudience;
  493. public void AddInvisible(int time) => buffManager.AddInvisible(time);
  494. public bool HasInvisible => buffManager.HasInvisible;
  495. private Array buffTypeArray = Enum.GetValues(typeof(BuffType));
  496. public Dictionary<BuffType, bool> Buff
  497. {
  498. get
  499. {
  500. Dictionary<BuffType, bool> buff = new Dictionary<BuffType, bool>();
  501. foreach (BuffType type in buffTypeArray)
  502. {
  503. if (type != BuffType.Null)
  504. buff.Add(type, GetBuffStatus(type));
  505. }
  506. return buff;
  507. }
  508. }
  509. private bool GetBuffStatus(BuffType type)
  510. {
  511. switch (type)
  512. {
  513. case BuffType.Spear:
  514. return this.HasSpear;
  515. case BuffType.AddSpeed:
  516. return this.HasFasterSpeed;
  517. case BuffType.Shield:
  518. return this.HasShield;
  519. case BuffType.AddLife:
  520. return this.HasLIFE;
  521. case BuffType.AddAp:
  522. return this.HasAp;
  523. case BuffType.Clairaudience:
  524. return this.HasClairaudience;
  525. case BuffType.Invisible:
  526. return this.HasInvisible;
  527. default:
  528. return false;
  529. }
  530. }
  531. public void TryActivatingLIFE()
  532. {
  533. if (buffManager.TryActivatingLIFE())
  534. {
  535. AddScore(GameData.ScorePropRemainHp);
  536. hp = GameData.RemainHpWhenAddLife;
  537. }
  538. }
  539. public bool TryAddAp()
  540. {
  541. if (buffManager.TryAddAp())
  542. {
  543. AddScore(GameData.ScorePropAddAp);
  544. return true;
  545. }
  546. return false;
  547. }
  548. public bool TryUseSpear()
  549. {
  550. return buffManager.TryUseSpear();
  551. }
  552. public bool TryUseShield()
  553. {
  554. if (buffManager.TryUseShield())
  555. {
  556. AddScore(GameData.ScorePropUseShield);
  557. return true;
  558. }
  559. return false;
  560. }
  561. #endregion
  562. /* public override void Reset() // 要加锁吗?
  563. {
  564. lock (gameObjLock)
  565. {
  566. // _ = AddDeathCount();
  567. base.Reset();
  568. this.MoveSpeed = OrgMoveSpeed;
  569. HP = MaxHp;
  570. PropInventory = null;
  571. BulletOfPlayer = OriBulletOfPlayer;
  572. lock (gameObjLock)
  573. bulletNum = maxBulletNum;
  574. buffManager.ClearAll();
  575. IsInvisible = false;
  576. this.Vampire = this.OriVampire;
  577. }
  578. }*/
  579. public override bool IsRigid => true;
  580. public override ShapeType Shape => ShapeType.Circle;
  581. public override bool IgnoreCollideExecutor(IGameObj targetObj)
  582. {
  583. if (IsResetting)
  584. return true;
  585. if (targetObj.Type == GameObjType.Prop)
  586. {
  587. return true;
  588. }
  589. if (targetObj.Type == GameObjType.Character && XY.DistanceCeil3(targetObj.Position, this.Position) < this.Radius + targetObj.Radius - GameData.adjustLength)
  590. return true;
  591. return false;
  592. }
  593. }
  594. }