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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. using Preparation.Interface;
  2. using Preparation.Utility;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Numerics;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. namespace GameClass.GameObj
  9. {
  10. public partial class Character : Moveable, ICharacter // 负责人LHR摆烂终了
  11. {
  12. #region 装弹、攻击相关的基本属性及方法
  13. protected readonly object beAttackedLock = new();
  14. /// <summary>
  15. /// 装弹冷却
  16. /// </summary>
  17. protected int cd;
  18. public int CD
  19. {
  20. get => cd;
  21. private set
  22. {
  23. lock (gameObjLock)
  24. {
  25. cd = value;
  26. Debugger.Output(this, string.Format("'s CD has been set to: {0}.", value));
  27. }
  28. }
  29. }
  30. public int OrgCD { get; protected set; }
  31. protected int maxBulletNum;
  32. public int MaxBulletNum => maxBulletNum; // 人物最大子弹数
  33. protected int bulletNum;
  34. public int BulletNum => bulletNum; // 目前持有的子弹数
  35. public readonly BulletType OriBulletOfPlayer;
  36. private BulletType bulletOfPlayer;
  37. public BulletType BulletOfPlayer
  38. {
  39. get => bulletOfPlayer;
  40. set
  41. {
  42. lock (gameObjLock)
  43. bulletOfPlayer = value;
  44. }
  45. }
  46. /// <summary>
  47. /// 进行一次攻击
  48. /// </summary>
  49. /// <returns>攻击操作发出的子弹</returns>
  50. public Bullet? Attack(XY pos, PlaceType place)
  51. {
  52. if (TrySubBulletNum())
  53. return BulletFactory.GetBullet(this, place, pos);
  54. else
  55. return null;
  56. }
  57. /// <summary>
  58. /// 尝试将子弹数量减1
  59. /// </summary>
  60. /// <returns>减操作是否成功</returns>
  61. private bool TrySubBulletNum()
  62. {
  63. lock (gameObjLock)
  64. {
  65. if (bulletNum > 0)
  66. {
  67. --bulletNum;
  68. return true;
  69. }
  70. return false;
  71. }
  72. }
  73. /// <summary>
  74. /// 尝试将子弹数量加1
  75. /// </summary>
  76. /// <returns>加操作是否成功</returns>
  77. public bool TryAddBulletNum()
  78. {
  79. lock (gameObjLock)
  80. {
  81. if (bulletNum < maxBulletNum)
  82. {
  83. ++bulletNum;
  84. return true;
  85. }
  86. return false;
  87. }
  88. }
  89. /*
  90. /// <summary>
  91. /// 攻击被反弹,反弹伤害不会再被反弹
  92. /// </summary>
  93. /// <param name="subHP"></param>
  94. /// <param name="hasSpear"></param>
  95. /// <param name="bouncer">反弹伤害者</param>
  96. /// <returns>是否因反弹伤害而死</returns>
  97. private bool BeBounced(int subHP, bool hasSpear, Character? bouncer)
  98. {
  99. lock (beAttackedLock)
  100. {
  101. if (hp <= 0)
  102. return false;
  103. if (!(bouncer?.TeamID == this.TeamID))
  104. {
  105. if (hasSpear || !HasShield)
  106. _ = TrySubHp(subHP);
  107. if (hp <= 0)
  108. TryActivatingLIFE();
  109. }
  110. return hp <= 0;
  111. }
  112. }*/
  113. #endregion
  114. #region 感知相关的基本属性及方法
  115. /// <summary>
  116. /// 是否在隐身
  117. /// </summary>
  118. private bool isInvisible = false;
  119. public bool IsInvisible
  120. {
  121. get => isInvisible;
  122. set
  123. {
  124. lock (gameObjLock)
  125. {
  126. isInvisible = value;
  127. }
  128. }
  129. }
  130. private Dictionary<BgmType, double> bgmDictionary = new();
  131. public Dictionary<BgmType, double> BgmDictionary
  132. {
  133. get => bgmDictionary;
  134. private set
  135. {
  136. lock (gameObjLock)
  137. {
  138. bgmDictionary = value;
  139. }
  140. }
  141. }
  142. public void AddBgm(BgmType bgm, double value)
  143. {
  144. if (BgmDictionary.ContainsKey(bgm))
  145. BgmDictionary[bgm] = value;
  146. else BgmDictionary.Add(bgm, value);
  147. }
  148. private int alertnessRadius;
  149. public int AlertnessRadius
  150. {
  151. get => alertnessRadius;
  152. set
  153. {
  154. lock (gameObjLock)
  155. {
  156. alertnessRadius = value;
  157. }
  158. }
  159. }
  160. private double concealment;
  161. public double Concealment
  162. {
  163. get => concealment;
  164. set
  165. {
  166. lock (gameObjLock)
  167. {
  168. concealment = value;
  169. }
  170. }
  171. }
  172. private int viewRange;
  173. public int ViewRange
  174. {
  175. get => viewRange;
  176. set
  177. {
  178. lock (gameObjLock)
  179. {
  180. viewRange = (value > 0) ? value : 0;
  181. }
  182. }
  183. }
  184. #endregion
  185. #region 交互相关的基本属性及方法
  186. private int speedOfOpeningOrLocking;
  187. public int SpeedOfOpeningOrLocking
  188. {
  189. get => speedOfOpeningOrLocking;
  190. set
  191. {
  192. lock (gameObjLock)
  193. {
  194. speedOfOpeningOrLocking = value;
  195. }
  196. }
  197. }
  198. private int speedOfClimbingThroughWindows;
  199. public int SpeedOfClimbingThroughWindows
  200. {
  201. get => speedOfClimbingThroughWindows;
  202. set
  203. {
  204. lock (gameObjLock)
  205. {
  206. speedOfClimbingThroughWindows = value;
  207. }
  208. }
  209. }
  210. private int speedOfOpenChest;
  211. public int SpeedOfOpenChest
  212. {
  213. get => speedOfOpenChest;
  214. set
  215. {
  216. lock (gameObjLock)
  217. {
  218. speedOfOpenChest = value;
  219. }
  220. }
  221. }
  222. #endregion
  223. #region 血量相关的基本属性及方法
  224. public int MaxHp { get; protected set; } // 最大血量
  225. protected int hp;
  226. public int HP
  227. {
  228. get => hp;
  229. set
  230. {
  231. if (value > 0)
  232. {
  233. lock (gameObjLock)
  234. hp = value <= MaxHp ? value : MaxHp;
  235. }
  236. else
  237. lock (gameObjLock)
  238. hp = 0;
  239. }
  240. }
  241. /// <summary>
  242. /// 尝试加血
  243. /// </summary>
  244. /// <param name="add">欲加量</param>
  245. /// <returns>加操作是否成功</returns>
  246. public bool TryAddHp(int add)
  247. {
  248. if (hp < MaxHp)
  249. {
  250. lock (gameObjLock)
  251. hp = MaxHp > hp + add ? hp + add : MaxHp;
  252. Debugger.Output(this, " hp has added to: " + hp.ToString());
  253. return true;
  254. }
  255. return false;
  256. }
  257. /// <summary>
  258. /// 尝试减血
  259. /// </summary>
  260. /// <param name="sub">减血量</param>
  261. /// <returns>减操作是否成功</returns>
  262. public int TrySubHp(int sub)
  263. {
  264. int previousHp = hp;
  265. lock (gameObjLock)
  266. hp = hp >= sub ? 0 : hp - sub;
  267. Debugger.Output(this, " hp has subed to: " + hp.ToString());
  268. return previousHp - hp;
  269. }
  270. private double vampire = 0; // 回血率:0-1之间
  271. public double Vampire
  272. {
  273. get => vampire;
  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. private double oriVampire = 0;
  288. public double OriVampire
  289. {
  290. get => oriVampire;
  291. set
  292. {
  293. if (value > 1)
  294. lock (gameObjLock)
  295. vampire = 1;
  296. else if (value < 0)
  297. lock (gameObjLock)
  298. vampire = 0;
  299. else
  300. lock (gameObjLock)
  301. vampire = value;
  302. }
  303. }
  304. #endregion
  305. private PlayerStateType playerState = PlayerStateType.Null;
  306. public PlayerStateType PlayerState
  307. {
  308. get
  309. {
  310. if (playerState == PlayerStateType.Null && IsMoving) return PlayerStateType.Moving;
  311. return playerState;
  312. }
  313. set
  314. {
  315. if (value != PlayerStateType.Moving)
  316. lock (gameObjLock)
  317. IsMoving = false;
  318. lock (gameObjLock) playerState = (value == PlayerStateType.Moving) ? PlayerStateType.Null : value;
  319. }
  320. }
  321. public bool NoHp() => (playerState != PlayerStateType.Deceased && playerState != PlayerStateType.Escaped
  322. && playerState != PlayerStateType.Addicted && playerState != PlayerStateType.Rescued);
  323. public bool Commandable() => (playerState != PlayerStateType.Deceased && playerState != PlayerStateType.Escaped
  324. && playerState != PlayerStateType.Addicted && playerState != PlayerStateType.Rescued
  325. && playerState != PlayerStateType.Swinging && playerState != PlayerStateType.TryingToAttack
  326. && playerState != PlayerStateType.ClimbingThroughWindows && playerState != PlayerStateType.Stunned);
  327. public bool InteractingWithMapWithoutMoving() => (playerState == PlayerStateType.LockingOrOpeningTheDoor || playerState == PlayerStateType.Fixing || playerState == PlayerStateType.OpeningTheChest);
  328. public bool NullOrMoving() => (playerState == PlayerStateType.Null || playerState == PlayerStateType.Moving);
  329. public bool CanBeAwed() => !(playerState == PlayerStateType.Deceased || playerState == PlayerStateType.Escaped
  330. || playerState == PlayerStateType.Addicted || playerState == PlayerStateType.Rescued
  331. || playerState == PlayerStateType.Treated || playerState != PlayerStateType.Stunned
  332. || playerState == PlayerStateType.Null || playerState == PlayerStateType.Moving);
  333. private int score = 0;
  334. public int Score
  335. {
  336. get => score;
  337. }
  338. /// <summary>
  339. /// 加分
  340. /// </summary>
  341. /// <param name="add">增加量</param>
  342. public void AddScore(int add)
  343. {
  344. lock (gameObjLock)
  345. {
  346. score += add;
  347. Debugger.Output(this, " 's score has been added to: " + score.ToString());
  348. }
  349. }
  350. /// <summary>
  351. /// 减分
  352. /// </summary>
  353. /// <param name="sub">减少量</param>
  354. public void SubScore(int sub)
  355. {
  356. lock (gameObjLock)
  357. {
  358. score -= sub;
  359. Debugger.Output(this, " 's score has been subed to: " + score.ToString());
  360. }
  361. }
  362. /// <summary>
  363. /// 角色所属队伍ID
  364. /// </summary>
  365. private long teamID = long.MaxValue;
  366. public long TeamID
  367. {
  368. get => teamID;
  369. set
  370. {
  371. lock (gameObjLock)
  372. {
  373. teamID = value;
  374. Debugger.Output(this, " joins in the team: " + value.ToString());
  375. }
  376. }
  377. }
  378. private long playerID = long.MaxValue;
  379. public long PlayerID
  380. {
  381. get => playerID;
  382. set
  383. {
  384. lock (gameObjLock)
  385. {
  386. playerID = value;
  387. }
  388. }
  389. }
  390. /// <summary>
  391. /// 角色携带的信息
  392. /// </summary>
  393. private string message = "THUAI6";
  394. public string Message
  395. {
  396. get => message;
  397. set
  398. {
  399. lock (gameObjLock)
  400. {
  401. message = value;
  402. }
  403. }
  404. }
  405. #region 道具和buff相关属性、方法
  406. private Prop[] propInventory = new Prop[GameData.maxNumOfPropInPropInventory]
  407. {new NullProp(), new NullProp(),new NullProp() };
  408. public Prop[] PropInventory
  409. {
  410. get => propInventory;
  411. set
  412. {
  413. lock (gameObjLock)
  414. {
  415. propInventory = value;
  416. Debugger.Output(this, " prop becomes " + (PropInventory == null ? "null" : PropInventory.ToString()));
  417. }
  418. }
  419. }
  420. /// <summary>
  421. /// 使用物品栏中的道具
  422. /// </summary>
  423. /// <returns>被使用的道具</returns>
  424. public Prop UseProp(int indexing)
  425. {
  426. if (indexing < 0 || indexing >= GameData.maxNumOfPropInPropInventory)
  427. return new NullProp();
  428. lock (gameObjLock)
  429. {
  430. Prop prop = propInventory[indexing];
  431. PropInventory[indexing] = new NullProp();
  432. return prop;
  433. }
  434. }
  435. /// <summary>
  436. /// 如果indexing==GameData.maxNumOfPropInPropInventory表明道具栏为满
  437. /// </summary>
  438. public int IndexingOfAddProp()
  439. {
  440. int indexing = 0;
  441. for (; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
  442. if (PropInventory[indexing].GetPropType() == PropType.Null)
  443. break;
  444. return indexing;
  445. }
  446. public void AddMoveSpeed(int buffTime, double add = 2.0) => buffManager.AddMoveSpeed(add, buffTime, newVal =>
  447. { MoveSpeed = newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed; },
  448. OrgMoveSpeed);
  449. public bool HasFasterSpeed => buffManager.HasFasterSpeed;
  450. public void AddShield(int shieldTime) => buffManager.AddShield(shieldTime);
  451. public bool HasShield => buffManager.HasShield;
  452. public void AddLIFE(int LIFETime) => buffManager.AddLIFE(LIFETime);
  453. public bool HasLIFE => buffManager.HasLIFE;
  454. public void AddSpear(int spearTime) => buffManager.AddSpear(spearTime);
  455. public bool HasSpear => buffManager.HasSpear;
  456. private Array buffTypeArray = Enum.GetValues(typeof(BuffType));
  457. public Dictionary<BuffType, bool> Buff
  458. {
  459. get
  460. {
  461. Dictionary<BuffType, bool> buff = new Dictionary<BuffType, bool>();
  462. foreach (BuffType type in buffTypeArray)
  463. {
  464. if (type != BuffType.Null)
  465. buff.Add(type, GetBuffStatus(type));
  466. }
  467. return buff;
  468. }
  469. }
  470. private bool GetBuffStatus(BuffType type)
  471. {
  472. switch (type)
  473. {
  474. case BuffType.Spear:
  475. return this.HasSpear;
  476. case BuffType.AddSpeed:
  477. return this.HasFasterSpeed;
  478. case BuffType.Shield:
  479. return this.HasShield;
  480. case BuffType.AddLIFE:
  481. return this.HasLIFE;
  482. default:
  483. return false;
  484. }
  485. }
  486. protected void TryActivatingLIFE()
  487. {
  488. if (buffManager.TryActivatingLIFE())
  489. {
  490. hp = MaxHp;
  491. }
  492. }
  493. #endregion
  494. /* public override void Reset() // 要加锁吗?
  495. {
  496. lock (gameObjLock)
  497. {
  498. // _ = AddDeathCount();
  499. base.Reset();
  500. this.MoveSpeed = OrgMoveSpeed;
  501. HP = MaxHp;
  502. PropInventory = null;
  503. BulletOfPlayer = OriBulletOfPlayer;
  504. lock (gameObjLock)
  505. bulletNum = maxBulletNum;
  506. buffManager.ClearAll();
  507. IsInvisible = false;
  508. this.Vampire = this.OriVampire;
  509. }
  510. }*/
  511. public void Die(PlayerStateType playerStateType)
  512. {
  513. lock (gameObjLock)
  514. {
  515. playerState = playerStateType;
  516. CanMove = false;
  517. IsResetting = true;
  518. Position = GameData.PosWhoDie;
  519. place = PlaceType.Grass;
  520. }
  521. }
  522. public override bool IsRigid => true;
  523. public override ShapeType Shape => ShapeType.Circle;
  524. protected override bool IgnoreCollideExecutor(IGameObj targetObj)
  525. {
  526. if (IsResetting)
  527. return true;
  528. if (targetObj.Type == GameObjType.Prop) // 自己队的地雷忽略碰撞
  529. {
  530. return true;
  531. }
  532. return false;
  533. }
  534. }
  535. }