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

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