PlayerStates.gml 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. function player_move_and_collide()
  2. {
  3. if place_meeting(x, y + y_spd, oParentSolid)
  4. {
  5. while !place_meeting(x, y + sign(y_spd), oParentSolid)
  6. y += sign(y_spd);
  7. y_spd = 0;
  8. }
  9. y += y_spd;
  10. if place_meeting(x + x_spd, y, oParentSolid)
  11. {
  12. var _max_step = (y_spd == 0) ? 16 : 8; //前者地面容错,后者空中容错
  13. var _stepped = false;
  14. for(var i = 0; i < _max_step; i++)
  15. if !place_meeting(x + x_spd, y - i, oParentSolid)
  16. {
  17. y -= i;
  18. _stepped = true;
  19. break;
  20. }
  21. if !_stepped
  22. {
  23. while !place_meeting(x + sign(x_spd), y, oParentSolid)
  24. x += sign(x_spd);
  25. x_spd = 0;
  26. }
  27. }
  28. x += x_spd;
  29. function player_state_update()
  30. {
  31. current_attacker = instance_place(x, y, oEnemyHitbox);
  32. if oInput._dash
  33. dash_buffer = dash_buffer_max;
  34. if oInput._attack
  35. attack_buffer = attack_buffer_max;
  36. _move_dir = oInput._right - oInput._left;
  37. _on_ground = place_meeting(x, y + 1, oParentSolid)
  38. _on_wall = place_meeting(x + 1, y, oBlockClimbable)
  39. - place_meeting(x - 1, y, oBlockClimbable);
  40. if _on_ground || (_on_wall != 0)
  41. can_dash = true;
  42. if _on_ground
  43. {
  44. jump_cnt = 0;
  45. coyote_timer = coyote_max;
  46. }
  47. else if coyote_timer <= 0 && jump_cnt == 0
  48. jump_cnt = 1;
  49. if dash_cooldown > 0
  50. dash_cooldown--;
  51. if dash_buffer > 0
  52. dash_buffer--;
  53. if attack_buffer > 0
  54. attack_buffer--;
  55. if move_lock_timer > 0
  56. move_lock_timer--;
  57. if coyote_timer > 0
  58. coyote_timer--;
  59. }
  60. function state_free()
  61. {
  62. y_spd += global.g;
  63. if _on_wall != 0 && y_spd > 0
  64. y_spd = min(y_spd, 3);
  65. /*下面是动画与视觉*/
  66. var _current_sprite = sprite_index;
  67. image_speed = 1;
  68. if _move_dir != 0
  69. {
  70. facing = _move_dir;
  71. if _on_ground
  72. sprite_index = sPlayerWalk;
  73. }
  74. else sprite_index = sPlayerIdle;
  75. if !_on_ground
  76. {
  77. image_speed = 0;
  78. if _on_wall == facing
  79. sprite_index = sPlayerWall;
  80. else
  81. {
  82. sprite_index = sPlayerJump;
  83. image_index = (y_spd < 0) ? 0 : 1;
  84. }
  85. }
  86. if _current_sprite != sprite_index
  87. image_index = 0;
  88. /**/
  89. player_check_dodge_y();
  90. if state == player_dodge_y return;
  91. player_check_movement();
  92. player_check_dash();
  93. player_check_jump();
  94. player_check_attack();
  95. }
  96. function state_dash()
  97. {
  98. y_spd += 0.2 * global.g;
  99. icl(oPlayerAfterImage);
  100. if !_on_ground
  101. jump_cnt = max(jump_cnt, 1);
  102. if animation_end()
  103. {
  104. state = state_free;
  105. player_check_attack();
  106. }
  107. player_check_dodge_y();
  108. }
  109. function state_attack()
  110. {
  111. player_check_movement();
  112. player_check_jump();
  113. y_spd += global.g;
  114. if image_index == 1
  115. {
  116. var _up = oInput._up;
  117. var _down = oInput._down;
  118. var _hb = icd(oPlayerHitboxRec);
  119. _hb.owner = id;
  120. var _base = 2;
  121. if _up
  122. {
  123. _hb.y -= 64;
  124. _hb.image_xscale = 72 / _base;
  125. _hb.image_yscale = 224 / _base;
  126. _hb.kb_dir_x = 0;
  127. _hb.kb_dir_y = -1;
  128. }
  129. else if _down && !_on_ground
  130. {
  131. _hb.y += 64;
  132. _hb.image_xscale = 72 / _base;
  133. _hb.image_yscale = 224 / _base;
  134. _hb.kb_dir_x = 0;
  135. _hb.kb_dir_y = 1;
  136. }
  137. else
  138. {
  139. _hb.x += 72 * facing;
  140. _hb.image_xscale = 144 / _base * facing;
  141. _hb.image_yscale = 96 / _base;
  142. _hb.kb_dir_x = facing;
  143. _hb.kb_dir_y = 0;
  144. }
  145. //攻击碰撞箱跟随
  146. _hb.x_offset = _hb.x - x;
  147. _hb.y_offset = _hb.y - y;
  148. }
  149. // 冲刺预输入 & 预读帧数
  150. if image_index >= 2
  151. {
  152. with(oPlayerHitboxRec)
  153. {
  154. instance_destroy();
  155. }
  156. player_check_dash();
  157. }
  158. // 攻击后摇结束
  159. if animation_end()
  160. state = state_free;
  161. }
  162. function player_check_dash()
  163. {
  164. if dash_buffer > 0 && dash_cooldown <= 0 && can_dash
  165. {
  166. dash_buffer = 0;
  167. state = state_dash;
  168. sprite_index = sPlayerDash;
  169. image_index = 0;
  170. image_speed = 1;
  171. dash_cooldown = 36;
  172. x_spd = image_xscale * walk_spd * 3;
  173. y_spd = 0;
  174. can_dash = false;
  175. }
  176. }
  177. function player_check_movement()
  178. {
  179. if move_lock_timer <= 0
  180. x_spd = _move_dir * walk_spd;
  181. }
  182. function player_check_attacked()
  183. {
  184. if current_attacker == noone || is_invincible
  185. || state == player_dodge_y
  186. return;
  187. global.playerHP -= current_attacker.damage;
  188. is_invincible = true;
  189. alarm[0] = 80;
  190. var _dir = sign(x - current_attacker.x);
  191. if _dir == 0
  192. _dir = facing;
  193. global.hitstop = 12;
  194. x_spd = _dir * 30;
  195. y_spd = -5;
  196. state = player_hitstun;
  197. sprite_index = sPlayerHitstun;
  198. image_speed = 1;
  199. image_index = 0;
  200. //effects
  201. }
  202. function player_check_jump()
  203. {
  204. if oInput.jump_buffer_timer > 0
  205. {
  206. if coyote_timer > 0
  207. {
  208. y_spd = jump_spd;
  209. jump_cnt = 1;
  210. oInput.jump_buffer_timer = 0;
  211. coyote_timer = 0;
  212. }
  213. else if _on_wall != 0
  214. {
  215. y_spd = jump_spd;
  216. x_spd = -_on_wall * walk_spd * 1.5;
  217. jump_cnt = 1;
  218. move_lock_timer = 12;
  219. oInput.jump_buffer_timer = 0;
  220. }
  221. else if jump_cnt < jump_max
  222. {
  223. icl(oDoubleJumpEffect);
  224. y_spd = jump_spd * 0.9;
  225. jump_cnt++;
  226. oInput.jump_buffer_timer = 0;
  227. }
  228. }
  229. if oInput._jump_r && y_spd < 0
  230. y_spd *= 0.4;
  231. }
  232. function player_check_attack()
  233. {
  234. if attack_buffer > 0
  235. {
  236. attack_buffer = 0;
  237. state = state_attack;
  238. sprite_index = sPlayerAttack;
  239. image_speed = 1;
  240. image_index = 0;
  241. }
  242. }
  243. function player_perform_pogo()
  244. {
  245. y_spd = jump_spd * 0.8;
  246. jump_cnt = 1;
  247. can_dash = true;
  248. coyote_timer = 0;
  249. //hitstop = 3;
  250. }
  251. function player_perform_recoil()
  252. {
  253. x_spd = -image_xscale * 3;
  254. move_lock_timer = 6;
  255. //hitstop = 3;
  256. }
  257. function player_hitstun()
  258. {
  259. x_spd = lerp(x_spd, 0, 0.1);
  260. y_spd += global.g;
  261. if animation_end()
  262. state = state_free;
  263. }
  264. function player_check_dodge_y()
  265. {
  266. if (oInput._down || oInput._up) && _on_ground
  267. && (oInput._dash || state == state_dash)
  268. {
  269. state = player_dodge_y;
  270. sprite_index = sPlayerDodgeYWait;
  271. image_index = 0;
  272. image_speed = 1;
  273. dodge_phase = "WAIT";
  274. dodge_timer = 0;
  275. marked_target = noone;
  276. is_marked = false;
  277. is_perfect = false;
  278. }
  279. }
  280. function player_dodge_y()
  281. {
  282. if dodge_phase == "WAIT"
  283. {
  284. x_spd = 0;
  285. if !is_marked
  286. player_check_mark_y();
  287. if current_attacker != noone || animation_end()
  288. {
  289. y_spd = jump_spd;
  290. dodge_phase = "DODGE";
  291. sprite_index = sPlayerDodgeYDodge;
  292. image_index = 0;
  293. if current_attacker != noone && is_marked
  294. {
  295. is_perfect = true;
  296. is_invincible = true;
  297. global.time_scale = 0.2;
  298. global.hitstop = 20;
  299. alarm[0] = 210; //之后实机测试计算
  300. }
  301. }
  302. }
  303. else if dodge_phase == "DODGE"
  304. {
  305. y_spd += global.g;
  306. if is_perfect icl(oPlayerAfterImage);
  307. if y_spd > 0
  308. {
  309. timer = 0;
  310. y_spd = 0;
  311. if is_perfect
  312. {
  313. state = player_arc_slash;
  314. sprite_index = sPlayerArcSlash;
  315. image_index = 0;
  316. image_speed = 1;
  317. }
  318. else
  319. {
  320. state = player_dodge_y_ending;
  321. sprite_index = sPlayerDodgeYEnding;
  322. image_index = 0;
  323. }
  324. }
  325. }
  326. }
  327. function player_arc_slash()
  328. {
  329. if image_index == 7
  330. {
  331. var _hb = icd(oPlayerHitboxArc);
  332. _hb.owner = id;
  333. if facing == 1
  334. _hb.image_xscale = 1;
  335. else
  336. _hb.image_xscale = -1;
  337. }
  338. else if animation_end()
  339. {
  340. global.time_scale = 1.0;
  341. state = state_free;
  342. }
  343. }
  344. function player_dodge_y_ending()
  345. {
  346. if animation_end()
  347. state = state_free;
  348. }
  349. function player_check_mark_y()
  350. {
  351. // 定义矩形相对于玩家中心的偏移和尺寸
  352. var _width = 2 * (72 + 16);
  353. var _height = 64;
  354. var _x_offset = facing * (36 + 16);
  355. var _y_offset = 0;
  356. // 计算出矩形的四个边界坐标
  357. var x1 = x + _x_offset - (_width / 2);
  358. var y1 = y + _y_offset;
  359. var x2 = x + _x_offset + (_width / 2);
  360. var y2 = y + _y_offset + _height;
  361. // 将这些坐标存入变量
  362. debug_hitbox_x1 = x1;
  363. debug_hitbox_y1 = y1;
  364. debug_hitbox_x2 = x2;
  365. debug_hitbox_y2 = y2;
  366. // 执行核心碰撞检测
  367. var _target = collision_rectangle(x1, y1, x2, y2, oParentEnemy, false, true);
  368. if _target != noone
  369. {
  370. is_marked = true;
  371. marked_target = _target;
  372. // animation
  373. // instance_create_layer(_target.x, _target.y, "FX", oFx_Marked);
  374. }
  375. }