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. 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. if (playerState == PlayerStateType.Null && IsMoving) return PlayerStateType.Moving;
  294. return playerState;
  295. }
  296. }
  297. public bool NoHp() => (playerState == PlayerStateType.Deceased || playerState == PlayerStateType.Escaped
  298. || playerState == PlayerStateType.Addicted || playerState == PlayerStateType.Rescued);
  299. public bool Commandable() => (playerState != PlayerStateType.Deceased && playerState != PlayerStateType.Escaped
  300. && playerState != PlayerStateType.Addicted && playerState != PlayerStateType.Rescued
  301. && playerState != PlayerStateType.Swinging && playerState != PlayerStateType.TryingToAttack
  302. && playerState != PlayerStateType.ClimbingThroughWindows && playerState != PlayerStateType.Stunned);
  303. public bool InteractingWithMapWithoutMoving() => (playerState == PlayerStateType.LockingOrOpeningTheDoor || playerState == PlayerStateType.Fixing || playerState == PlayerStateType.OpeningTheChest);
  304. public bool NullOrMoving() => (playerState == PlayerStateType.Null || playerState == PlayerStateType.Moving);
  305. public bool CanBeAwed() => !(playerState == PlayerStateType.Deceased || playerState == PlayerStateType.Escaped
  306. || playerState == PlayerStateType.Addicted || playerState == PlayerStateType.Rescued
  307. || playerState == PlayerStateType.Treated || playerState == PlayerStateType.Stunned
  308. || playerState == PlayerStateType.Null || playerState == PlayerStateType.Moving);
  309. private GameObj? whatInteractingWith = null;
  310. public GameObj? WhatInteractingWith => whatInteractingWith;
  311. public long ChangePlayerState(PlayerStateType value = PlayerStateType.Null, GameObj? gameObj = null)
  312. {
  313. lock (actionLock)
  314. {
  315. whatInteractingWith = gameObj;
  316. if (value != PlayerStateType.Moving)
  317. IsMoving = false;
  318. playerState = (value == PlayerStateType.Moving) ? PlayerStateType.Null : value;
  319. //Debugger.Output(this,playerState.ToString()+" "+IsMoving.ToString());
  320. return ++stateNum;
  321. }
  322. }
  323. public long ChangePlayerStateInOneThread(PlayerStateType value = PlayerStateType.Null, GameObj? gameObj = null)
  324. {
  325. lock (actionLock)
  326. {
  327. whatInteractingWith = gameObj;
  328. if (value != PlayerStateType.Moving)
  329. IsMoving = false;
  330. playerState = (value == PlayerStateType.Moving) ? PlayerStateType.Null : value;
  331. //Debugger.Output(this,playerState.ToString()+" "+IsMoving.ToString());
  332. return stateNum;
  333. }
  334. }
  335. public long SetPlayerStateNaturally()
  336. {
  337. lock (actionLock)
  338. {
  339. whatInteractingWith = null;
  340. IsMoving = false;
  341. playerState = PlayerStateType.Null;
  342. return ++stateNum;
  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. isRemoved = 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 (IsRemoved)
  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. }