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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. using Preparation.Interface;
  2. using Preparation.Utility;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. namespace GameClass.GameObj
  7. {
  8. public partial class Character : Moveable, ICharacter // 负责人LHR摆烂终了
  9. {
  10. #region 装弹、攻击相关的基本属性及方法
  11. private readonly object attackLock = new();
  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. private int orgCD;
  27. public int OrgCD
  28. {
  29. get
  30. {
  31. lock (attackLock)
  32. return orgCD;
  33. }
  34. }
  35. public readonly BulletType OriBulletOfPlayer;
  36. private BulletType bulletOfPlayer;
  37. public BulletType BulletOfPlayer
  38. {
  39. get
  40. {
  41. lock (attackLock)
  42. {
  43. return bulletOfPlayer;
  44. }
  45. }
  46. set
  47. {
  48. lock (attackLock)
  49. {
  50. bulletOfPlayer = value;
  51. cd = orgCD = (BulletFactory.BulletCD(value));
  52. Debugger.Output(this, string.Format("'s CD has been set to: {0}.", cd));
  53. maxBulletNum = bulletNum = (BulletFactory.BulletNum(value));
  54. }
  55. }
  56. }
  57. protected int maxBulletNum;
  58. public int MaxBulletNum
  59. {
  60. get
  61. {
  62. lock (attackLock)
  63. {
  64. return maxBulletNum;
  65. }
  66. }
  67. }
  68. private int bulletNum;
  69. private int updateTimeOfBulletNum = 0;
  70. public int UpdateBulletNum(int time)//通过该函数获取真正的bulletNum
  71. {
  72. lock (attackLock)
  73. {
  74. if (bulletNum < maxBulletNum && time - updateTimeOfBulletNum >= cd)
  75. {
  76. int add = Math.Min(maxBulletNum - bulletNum, (time - updateTimeOfBulletNum) / cd);
  77. updateTimeOfBulletNum += add * cd;
  78. return (bulletNum += add);
  79. }
  80. return bulletNum;
  81. }
  82. }
  83. /// <summary>
  84. /// 进行一次攻击
  85. /// </summary>
  86. /// <returns>攻击操作发出的子弹</returns>
  87. public Bullet? Attack(double angle, int time)
  88. {
  89. lock (attackLock)
  90. {
  91. if (bulletOfPlayer == BulletType.Null)
  92. return null;
  93. if (UpdateBulletNum(time) > 0)
  94. {
  95. if (bulletNum == maxBulletNum) updateTimeOfBulletNum = time;
  96. --bulletNum;
  97. XY res = Position + new XY // 子弹紧贴人物生成。
  98. (
  99. (int)(Math.Abs((Radius + BulletFactory.BulletRadius(bulletOfPlayer)) * Math.Cos(angle))) * Math.Sign(Math.Cos(angle)),
  100. (int)(Math.Abs((Radius + BulletFactory.BulletRadius(bulletOfPlayer)) * Math.Sin(angle))) * Math.Sign(Math.Sin(angle))
  101. );
  102. Bullet? bullet = BulletFactory.GetBullet(this, res, this.bulletOfPlayer);
  103. if (bullet == null) return null;
  104. if (TryAddAp()) bullet.AddAP(GameData.ApPropAdd);
  105. FacingDirection = new(angle, bullet.AttackDistance);
  106. return bullet;
  107. }
  108. else
  109. return null;
  110. }
  111. }
  112. /*
  113. /// <summary>
  114. /// 攻击被反弹,反弹伤害不会再被反弹
  115. /// </summary>
  116. /// <param name="subHP"></param>
  117. /// <param name="hasSpear"></param>
  118. /// <param name="bouncer">反弹伤害者</param>
  119. /// <returns>是否因反弹伤害而死</returns>
  120. private bool BeBounced(int subHP, bool hasSpear, Character? bouncer)
  121. {
  122. lock (beAttackedLock)
  123. {
  124. if (hp <= 0)
  125. return false;
  126. if (!(bouncer?.TeamID == this.TeamID))
  127. {
  128. if (hasSpear || !HasShield)
  129. _ = TrySubHp(subHP);
  130. if (hp <= 0)
  131. TryActivatingLIFE();
  132. }
  133. return hp <= 0;
  134. }
  135. }*/
  136. #endregion
  137. #region 感知相关的基本属性及方法
  138. private readonly object bgmLock = new();
  139. private Dictionary<BgmType, double> bgmDictionary = new() { { BgmType.GhostIsComing, 0 }, { BgmType.StudentIsApproaching, 0 }, { BgmType.GeneratorIsBeingFixed, 0 } };
  140. public Dictionary<BgmType, double> BgmDictionary
  141. {
  142. get
  143. {
  144. lock (bgmLock)
  145. return bgmDictionary;
  146. }
  147. }
  148. public void AddBgm(BgmType bgm, double value)
  149. {
  150. lock (bgmLock)
  151. bgmDictionary[bgm] = value;
  152. }
  153. private readonly int alertnessRadius;
  154. public int AlertnessRadius => alertnessRadius;
  155. private readonly double concealment;
  156. public double Concealment => concealment;
  157. private readonly int viewRange;
  158. public int ViewRange => viewRange;
  159. #endregion
  160. #region 交互相关的基本属性及方法
  161. private readonly int speedOfOpeningOrLocking;
  162. public int SpeedOfOpeningOrLocking
  163. {
  164. get => speedOfOpeningOrLocking;
  165. }
  166. private readonly int speedOfClimbingThroughWindows;
  167. public int SpeedOfClimbingThroughWindows
  168. {
  169. get => speedOfClimbingThroughWindows;
  170. }
  171. private readonly int speedOfOpenChest;
  172. public int SpeedOfOpenChest
  173. {
  174. get => speedOfOpenChest;
  175. }
  176. #endregion
  177. #region 血量相关的基本属性及方法
  178. private readonly ReaderWriterLockSlim hpReaderWriterLock = new();
  179. public ReaderWriterLockSlim HPReadWriterLock => hpReaderWriterLock;
  180. private long maxHp;
  181. public long MaxHp
  182. {
  183. get
  184. {
  185. HPReadWriterLock.EnterReadLock();
  186. try
  187. {
  188. return maxHp;
  189. }
  190. finally
  191. {
  192. HPReadWriterLock.ExitReadLock();
  193. }
  194. }
  195. protected set
  196. {
  197. HPReadWriterLock.EnterWriteLock();
  198. try
  199. {
  200. maxHp = value;
  201. if (hp > maxHp) hp = maxHp;
  202. }
  203. finally
  204. {
  205. HPReadWriterLock.ExitWriteLock();
  206. }
  207. }
  208. } // 最大血量
  209. protected long hp;
  210. public long HP
  211. {
  212. get
  213. {
  214. HPReadWriterLock.EnterReadLock();
  215. try
  216. {
  217. return hp;
  218. }
  219. finally
  220. {
  221. HPReadWriterLock.ExitReadLock();
  222. }
  223. }
  224. set
  225. {
  226. HPReadWriterLock.EnterWriteLock();
  227. try
  228. {
  229. if (value > 0)
  230. {
  231. hp = value <= maxHp ? value : maxHp;
  232. }
  233. else
  234. hp = 0;
  235. }
  236. finally
  237. {
  238. HPReadWriterLock.ExitWriteLock();
  239. }
  240. }
  241. }
  242. /// <summary>
  243. /// 尝试减血
  244. /// </summary>
  245. /// <param name="sub">减血量</param>
  246. public long TrySubHp(long sub)
  247. {
  248. HPReadWriterLock.EnterWriteLock();
  249. try
  250. {
  251. long previousHp = hp;
  252. if (hp <= sub)
  253. {
  254. hp = 0;
  255. return hp;
  256. }
  257. else
  258. {
  259. hp -= sub;
  260. return sub;
  261. }
  262. }
  263. finally
  264. {
  265. HPReadWriterLock.ExitWriteLock();
  266. }
  267. }
  268. private readonly object vampireLock = new();
  269. public object VampireLock => vampire;
  270. private double vampire = 0; // 回血率:0-1之间
  271. public double Vampire
  272. {
  273. get
  274. {
  275. lock (vampireLock)
  276. return vampire;
  277. }
  278. set
  279. {
  280. lock (vampireLock)
  281. {
  282. if (value > 1)
  283. vampire = 1;
  284. else if (value < 0)
  285. vampire = 0;
  286. else
  287. vampire = value;
  288. }
  289. }
  290. }
  291. public double OriVampire { get; protected set; }
  292. private readonly object treatLock = new();
  293. private int degreeOfTreatment = 0;
  294. public int DegreeOfTreatment
  295. {
  296. get
  297. {
  298. HPReadWriterLock.EnterReadLock();
  299. try
  300. {
  301. return degreeOfTreatment;
  302. }
  303. finally
  304. {
  305. HPReadWriterLock.ExitReadLock();
  306. }
  307. }
  308. }
  309. public void SetDegreeOfTreatment0()
  310. {
  311. HPReadWriterLock.EnterWriteLock();
  312. try
  313. {
  314. degreeOfTreatment = 0;
  315. }
  316. finally
  317. {
  318. HPReadWriterLock.ExitWriteLock();
  319. }
  320. }
  321. public bool AddDegreeOfTreatment(int value, Student whoTreatYou)
  322. {
  323. HPReadWriterLock.EnterWriteLock();
  324. try
  325. {
  326. if (value >= maxHp - hp)
  327. {
  328. whoTreatYou.AddScore(GameData.StudentScoreTreat(maxHp - hp));
  329. hp = maxHp;
  330. degreeOfTreatment = 0;
  331. return true;
  332. }
  333. if (value >= GameData.basicTreatmentDegree)
  334. {
  335. whoTreatYou.AddScore(GameData.StudentScoreTreat(GameData.basicTreatmentDegree));
  336. hp += GameData.basicTreatmentDegree;
  337. degreeOfTreatment = 0;
  338. return true;
  339. }
  340. degreeOfTreatment = value;
  341. }
  342. finally
  343. {
  344. HPReadWriterLock.ExitWriteLock();
  345. }
  346. return false;
  347. }
  348. #endregion
  349. #region 查询状态相关的基本属性与方法
  350. private PlayerStateType playerState = PlayerStateType.Null;
  351. public PlayerStateType PlayerState
  352. {
  353. get
  354. {
  355. lock (actionLock)
  356. {
  357. if (playerState == PlayerStateType.Moving)
  358. return (IsMoving) ? PlayerStateType.Moving : PlayerStateType.Null;
  359. return playerState;
  360. }
  361. }
  362. }
  363. public bool NoHp()
  364. {
  365. lock (actionLock)
  366. return (playerState == PlayerStateType.Deceased || playerState == PlayerStateType.Escaped || playerState == PlayerStateType.Addicted || playerState == PlayerStateType.Rescued);
  367. }
  368. public bool Commandable()
  369. {
  370. lock (actionLock)
  371. {
  372. return (playerState != PlayerStateType.Deceased && playerState != PlayerStateType.Escaped
  373. && playerState != PlayerStateType.Addicted && playerState != PlayerStateType.Rescued
  374. && playerState != PlayerStateType.Swinging && playerState != PlayerStateType.TryingToAttack
  375. && playerState != PlayerStateType.ClimbingThroughWindows
  376. && playerState != PlayerStateType.Stunned && playerState != PlayerStateType.Charmed);
  377. }
  378. }
  379. public bool CanPinDown()
  380. {
  381. lock (actionLock)
  382. {
  383. return (playerState != PlayerStateType.Deceased && playerState != PlayerStateType.Escaped
  384. && playerState != PlayerStateType.Addicted && playerState != PlayerStateType.Rescued
  385. && playerState != PlayerStateType.Stunned && playerState != PlayerStateType.Charmed);
  386. }
  387. }
  388. public bool InteractingWithMapWithoutMoving()
  389. {
  390. lock (actionLock)
  391. {
  392. return (playerState == PlayerStateType.LockingTheDoor || playerState == PlayerStateType.OpeningTheDoor
  393. || playerState == PlayerStateType.Fixing || playerState == PlayerStateType.OpeningTheChest);
  394. }
  395. }
  396. public bool NullOrMoving()
  397. {
  398. lock (actionLock)
  399. {
  400. return (playerState == PlayerStateType.Null || playerState == PlayerStateType.Moving);
  401. }
  402. }
  403. public bool CanBeAwed()
  404. {
  405. lock (actionLock)
  406. return !(playerState == PlayerStateType.Deceased || playerState == PlayerStateType.Escaped
  407. || playerState == PlayerStateType.Addicted
  408. || playerState == PlayerStateType.Rescued || playerState == PlayerStateType.Treated
  409. || playerState == PlayerStateType.Stunned || playerState == PlayerStateType.Charmed
  410. || playerState == PlayerStateType.Null || playerState == PlayerStateType.Moving);
  411. }
  412. #endregion
  413. #region 更改状态相关的属性和方法
  414. private GameObj? whatInteractingWith = null;
  415. public GameObj? WhatInteractingWith
  416. {
  417. get
  418. {
  419. lock (actionLock)
  420. {
  421. return whatInteractingWith;
  422. }
  423. }
  424. }
  425. private long ChangePlayerState(RunningStateType running, PlayerStateType value = PlayerStateType.Null, GameObj? gameObj = null)
  426. {
  427. //只能被SetPlayerState引用
  428. if (runningState == RunningStateType.RunningSleepily)
  429. {
  430. ThreadNum.Release();
  431. }
  432. runningState = running;
  433. whatInteractingWith = gameObj;
  434. playerState = value;
  435. return ++stateNum;
  436. }
  437. private long ChangePlayerStateInOneThread(RunningStateType running, PlayerStateType value = PlayerStateType.Null, GameObj? gameObj = null)
  438. {
  439. if (runningState == RunningStateType.RunningSleepily)
  440. {
  441. ThreadNum.Release();
  442. }
  443. runningState = running;
  444. //只能被SetPlayerState引用
  445. whatInteractingWith = gameObj;
  446. playerState = value;
  447. return stateNum;
  448. }
  449. public long SetPlayerState(RunningStateType runningState, PlayerStateType value = PlayerStateType.Null, IGameObj? obj = null)
  450. {
  451. GameObj? gameObj = (GameObj?)obj;
  452. lock (actionLock)
  453. {
  454. PlayerStateType nowPlayerState = PlayerState;
  455. if (nowPlayerState == value && value != PlayerStateType.UsingSkill) return -1;
  456. GameObj? lastObj = whatInteractingWith;
  457. switch (nowPlayerState)
  458. {
  459. case PlayerStateType.Escaped:
  460. case PlayerStateType.Deceased:
  461. return -1;
  462. case PlayerStateType.Addicted:
  463. if (value == PlayerStateType.Rescued)
  464. return ChangePlayerStateInOneThread(runningState, value, gameObj);
  465. else if (value == PlayerStateType.Null || value == PlayerStateType.Deceased)
  466. return ChangePlayerState(runningState, value, gameObj);
  467. else return -1;
  468. case PlayerStateType.Rescued:
  469. if (value == PlayerStateType.Addicted)
  470. return ChangePlayerStateInOneThread(runningState, value, gameObj);
  471. else if (value == PlayerStateType.Null || value == PlayerStateType.Deceased)
  472. return ChangePlayerState(runningState, value, gameObj);
  473. else return -1;
  474. case PlayerStateType.TryingToAttack:
  475. if (value == PlayerStateType.Addicted || value == PlayerStateType.Swinging
  476. || value == PlayerStateType.Deceased || value == PlayerStateType.Stunned
  477. || value == PlayerStateType.Charmed || value == PlayerStateType.Null)
  478. return ChangePlayerState(runningState, value, gameObj);
  479. else return -1;
  480. case PlayerStateType.Stunned:
  481. case PlayerStateType.Charmed:
  482. if (value == PlayerStateType.Addicted || value == PlayerStateType.Deceased
  483. || value == PlayerStateType.Null)
  484. return ChangePlayerState(runningState, value, gameObj);
  485. else return -1;
  486. case PlayerStateType.Swinging:
  487. if (value == PlayerStateType.Addicted
  488. || value == PlayerStateType.Deceased || value == PlayerStateType.Stunned
  489. || value == PlayerStateType.Charmed || value == PlayerStateType.Null)
  490. return ChangePlayerState(runningState, value, gameObj);
  491. else return -1;
  492. case PlayerStateType.ClimbingThroughWindows:
  493. if (value == PlayerStateType.Addicted
  494. || value == PlayerStateType.Deceased || value == PlayerStateType.Stunned
  495. || value == PlayerStateType.Charmed || value == PlayerStateType.Null)
  496. {
  497. Window window = (Window)lastObj!;
  498. if (window.Stage.x != 0) ReSetPos(window.Stage);
  499. window.FinishClimbing();
  500. return ChangePlayerState(runningState, value, gameObj);
  501. }
  502. else return -1;
  503. case PlayerStateType.OpeningTheChest:
  504. if (value == PlayerStateType.Rescued) return -1;
  505. ((Chest)lastObj!).StopOpen();
  506. return ChangePlayerState(runningState, value, gameObj);
  507. case PlayerStateType.OpeningTheDoorway:
  508. if (value == PlayerStateType.Rescued) return -1;
  509. Doorway doorway = (Doorway)lastObj!;
  510. doorway.StopOpenning();
  511. return ChangePlayerState(runningState, value, gameObj);
  512. case PlayerStateType.OpeningTheDoor:
  513. if (value == PlayerStateType.Rescued) return -1;
  514. Door door = (Door)lastObj!;
  515. door.StopOpen();
  516. ReleaseTool(door.DoorNum switch
  517. {
  518. 3 => PropType.Key3,
  519. 5 => PropType.Key5,
  520. _ => PropType.Key6,
  521. }
  522. );
  523. return ChangePlayerState(runningState, value, gameObj);
  524. case PlayerStateType.UsingSkill:
  525. {
  526. if (value == PlayerStateType.Rescued) return -1;
  527. switch (CharacterType)
  528. {
  529. case CharacterType.TechOtaku:
  530. {
  531. if (typeof(CraftingBench).IsInstanceOfType(lastObj))
  532. {
  533. ((CraftingBench)lastObj!).StopSkill();
  534. return ChangePlayerState(runningState, value, gameObj);
  535. }
  536. else
  537. {
  538. if (value != PlayerStateType.UsingSkill)
  539. ((UseRobot)FindActiveSkill(ActiveSkillType.UseRobot)).NowPlayerID = (int)playerID;
  540. return ChangePlayerState(runningState, value, gameObj);
  541. }
  542. }
  543. case CharacterType.Assassin:
  544. if (value == PlayerStateType.Moving) return StateNum;
  545. else
  546. {
  547. TryDeleteInvisible();
  548. return ChangePlayerState(runningState, value, gameObj);
  549. }
  550. default:
  551. return ChangePlayerState(runningState, value, gameObj);
  552. }
  553. }
  554. default:
  555. if (value == PlayerStateType.Rescued) return -1;
  556. return ChangePlayerState(runningState, value, gameObj);
  557. }
  558. }
  559. }
  560. public long SetPlayerStateNaturally()
  561. {
  562. lock (actionLock)
  563. {
  564. runningState = RunningStateType.Null;
  565. whatInteractingWith = null;
  566. playerState = PlayerStateType.Null;
  567. return ++stateNum;
  568. }
  569. }
  570. public bool ResetPlayerState(long state, RunningStateType running = RunningStateType.Null, PlayerStateType value = PlayerStateType.Null, IGameObj? obj = null)
  571. {
  572. lock (actionLock)
  573. {
  574. if (state != stateNum) return false;
  575. this.runningState = running;
  576. whatInteractingWith = (GameObj?)obj;
  577. playerState = value;
  578. ++stateNum;
  579. return true;
  580. }
  581. }
  582. public bool ResetPlayerStateInOneThread(long state, RunningStateType running = RunningStateType.Null, PlayerStateType value = PlayerStateType.Null, IGameObj? obj = null)
  583. {
  584. lock (actionLock)
  585. {
  586. if (state != stateNum) return false;
  587. this.runningState = running;
  588. whatInteractingWith = (GameObj?)obj;
  589. playerState = value;
  590. return true;
  591. }
  592. }
  593. public bool StartThread(long stateNum, RunningStateType runningState)
  594. {
  595. lock (ActionLock)
  596. {
  597. if (this.StateNum == stateNum)
  598. {
  599. this.runningState = runningState;
  600. return true;
  601. }
  602. }
  603. return false;
  604. }
  605. public bool TryToRemoveFromGame(PlayerStateType playerStateType)
  606. {
  607. lock (actionLock)
  608. {
  609. if (SetPlayerState(RunningStateType.RunningForcibly, playerStateType) == -1) return false;
  610. TryToRemove();
  611. ReSetCanMove(false);
  612. position = GameData.PosWhoDie;
  613. }
  614. return true;
  615. }
  616. #endregion
  617. private long score = 0;
  618. public long Score
  619. {
  620. get => Interlocked.Read(ref score);
  621. }
  622. /// <summary>
  623. /// 加分
  624. /// </summary>
  625. /// <param name="add">增加量</param>
  626. public virtual void AddScore(long add)
  627. {
  628. Interlocked.Add(ref score, add);
  629. //Debugger.Output(this, " 's score has been added to: " + score.ToString());
  630. }
  631. /// <summary>
  632. /// 角色所属队伍ID
  633. /// </summary>
  634. private long teamID = long.MaxValue;
  635. public long TeamID
  636. {
  637. get => Interlocked.Read(ref teamID);
  638. set => Interlocked.Exchange(ref teamID, value);
  639. }
  640. private long playerID = long.MaxValue;
  641. public long PlayerID
  642. {
  643. get => Interlocked.Read(ref playerID);
  644. set => Interlocked.Exchange(ref playerID, value);
  645. }
  646. #region 道具和buff相关属性、方法
  647. private readonly object inventoryLock = new();
  648. public object InventoryLock => inventoryLock;
  649. private Gadget[] propInventory = new Gadget[GameData.maxNumOfPropInPropInventory]
  650. {new NullProp(), new NullProp(),new NullProp() };
  651. public Gadget[] PropInventory
  652. {
  653. get
  654. {
  655. lock (inventoryLock)
  656. return propInventory;
  657. }
  658. set
  659. {
  660. lock (inventoryLock)
  661. propInventory = value;
  662. }
  663. }
  664. /// <summary>
  665. /// 使用物品栏中的道具
  666. /// </summary>
  667. /// <returns>被使用的道具</returns>
  668. public Gadget ConsumeProp(int indexing)
  669. {
  670. if (indexing < 0 || indexing >= GameData.maxNumOfPropInPropInventory)
  671. return new NullProp();
  672. lock (inventoryLock)
  673. {
  674. Gadget prop = propInventory[indexing];
  675. if (!prop.IsUsable()) return new NullProp();
  676. PropInventory[indexing] = new NullProp();
  677. return prop;
  678. }
  679. }
  680. public Gadget ConsumeProp(PropType propType)
  681. {
  682. if (propType == PropType.Null)
  683. {
  684. lock (inventoryLock)
  685. {
  686. for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  687. {
  688. if (PropInventory[indexing].IsUsable())
  689. {
  690. Gadget prop = PropInventory[indexing];
  691. PropInventory[indexing] = new NullProp();
  692. return prop;
  693. }
  694. }
  695. }
  696. }
  697. else
  698. {
  699. lock (inventoryLock)
  700. {
  701. for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  702. {
  703. if (PropInventory[indexing].GetPropType() == propType && PropInventory[indexing].IsUsable())
  704. {
  705. Gadget prop = PropInventory[indexing];
  706. PropInventory[indexing] = new NullProp();
  707. return prop;
  708. }
  709. }
  710. }
  711. }
  712. return new NullProp();
  713. }
  714. public bool UseTool(PropType propType)//占用道具,使其不能重复使用和被消耗
  715. {
  716. lock (inventoryLock)
  717. {
  718. for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  719. {
  720. if (PropInventory[indexing].GetPropType() == propType && PropInventory[indexing].IsUsable())
  721. {
  722. return ((Tool)PropInventory[indexing]).IsUsed = true;
  723. }
  724. }
  725. }
  726. return false;
  727. }
  728. public void ReleaseTool(PropType propType)
  729. {
  730. lock (inventoryLock)
  731. {
  732. for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  733. {
  734. if (PropInventory[indexing].GetPropType() == propType && ((Tool)PropInventory[indexing]).IsUsed)
  735. {
  736. ((Tool)PropInventory[indexing]).IsUsed = false;
  737. break;
  738. }
  739. }
  740. }
  741. }
  742. /// <summary>
  743. /// 如果indexing==GameData.maxNumOfPropInPropInventory表明道具栏为满
  744. /// </summary>
  745. public int IndexingOfAddProp()
  746. {
  747. int indexing = 0;
  748. lock (inventoryLock)
  749. for (; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  750. if (propInventory[indexing].GetPropType() == PropType.Null)
  751. break;
  752. return indexing;
  753. }
  754. public void AddMoveSpeed(int buffTime, double add = 1.0) => buffManager.AddMoveSpeed(add, buffTime, newVal =>
  755. { MoveSpeed = newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed; },
  756. OrgMoveSpeed);
  757. public bool HasFasterSpeed => buffManager.HasFasterSpeed;
  758. public void AddShield(int shieldTime) => buffManager.AddShield(shieldTime);
  759. public bool HasShield => buffManager.HasShield;
  760. public void AddLife(int LIFETime) => buffManager.AddLife(LIFETime);
  761. public bool HasLIFE => buffManager.HasLIFE;
  762. public void AddAp(int time) => buffManager.AddAp(time);
  763. public bool HasAp => buffManager.HasAp;
  764. public void AddSpear(int spearTime) => buffManager.AddSpear(spearTime);
  765. public bool HasSpear => buffManager.HasSpear;
  766. public void AddClairaudience(int time) => buffManager.AddClairaudience(time);
  767. public bool HasClairaudience => buffManager.HasClairaudience;
  768. public void AddInvisible(int time) => buffManager.AddInvisible(time);
  769. public bool HasInvisible => buffManager.HasInvisible;
  770. private Array buffTypeArray = Enum.GetValues(typeof(BuffType));
  771. public Dictionary<BuffType, bool> Buff
  772. {
  773. get
  774. {
  775. Dictionary<BuffType, bool> buff = new Dictionary<BuffType, bool>();
  776. foreach (BuffType type in buffTypeArray)
  777. {
  778. if (type != BuffType.Null)
  779. buff.Add(type, GetBuffStatus(type));
  780. }
  781. return buff;
  782. }
  783. }
  784. private bool GetBuffStatus(BuffType type)
  785. {
  786. switch (type)
  787. {
  788. case BuffType.Spear:
  789. return this.HasSpear;
  790. case BuffType.AddSpeed:
  791. return this.HasFasterSpeed;
  792. case BuffType.Shield:
  793. return this.HasShield;
  794. case BuffType.AddLife:
  795. return this.HasLIFE;
  796. case BuffType.AddAp:
  797. return this.HasAp;
  798. case BuffType.Clairaudience:
  799. return this.HasClairaudience;
  800. case BuffType.Invisible:
  801. return this.HasInvisible;
  802. default:
  803. return false;
  804. }
  805. }
  806. public void TryActivatingLIFE()
  807. {
  808. if (buffManager.TryActivatingLIFE())
  809. {
  810. AddScore(GameData.ScorePropRemainHp);
  811. hp = GameData.RemainHpWhenAddLife;
  812. }
  813. }
  814. public bool TryAddAp()
  815. {
  816. if (buffManager.TryAddAp())
  817. {
  818. AddScore(GameData.ScorePropAddAp);
  819. return true;
  820. }
  821. return false;
  822. }
  823. public bool TryUseSpear()
  824. {
  825. return buffManager.TryUseSpear();
  826. }
  827. public bool TryDeleteInvisible()
  828. {
  829. return buffManager.TryDeleteInvisible();
  830. }
  831. public bool TryUseShield()
  832. {
  833. if (buffManager.TryUseShield())
  834. {
  835. AddScore(GameData.ScorePropUseShield);
  836. return true;
  837. }
  838. return false;
  839. }
  840. #endregion
  841. /* public override void Reset() // 要加锁吗?
  842. {
  843. lock (gameObjLock)
  844. {
  845. // _ = AddDeathCount();
  846. base.Reset();
  847. this.MoveSpeed = OrgMoveSpeed;
  848. HP = MaxHp;
  849. PropInventory = null;
  850. BulletOfPlayer = OriBulletOfPlayer;
  851. lock (gameObjLock)
  852. bulletNum = maxBulletNum;
  853. buffManager.ClearAll();
  854. IsInvisible = false;
  855. this.Vampire = this.OriVampire;
  856. }
  857. }*/
  858. public override bool IsRigid => true;
  859. public override ShapeType Shape => ShapeType.Circle;
  860. public override bool IgnoreCollideExecutor(IGameObj targetObj)
  861. {
  862. if (IsRemoved)
  863. return true;
  864. if (targetObj.Type == GameObjType.Gadget)
  865. {
  866. return true;
  867. }
  868. if (targetObj.Type == GameObjType.Character && XY.DistanceCeil3(targetObj.Position, this.Position) < this.Radius + targetObj.Radius - GameData.adjustLength)
  869. return true;
  870. return false;
  871. }
  872. }
  873. }