PlayerStates.gml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. function player_move_and_collide()
  2. {
  3. if global.hitstop > 0
  4. return;
  5. y_spd *= global.time_scale;
  6. x_spd *= global.time_scale;
  7. if place_meeting(x, y + y_spd, oParentSolid)
  8. {
  9. while !place_meeting(x, y + sign(y_spd), oParentSolid)
  10. y += sign(y_spd);
  11. y_spd = 0;
  12. }
  13. y += y_spd;
  14. if place_meeting(x + x_spd, y, oParentSolid)
  15. {
  16. var _max_step = (y_spd == 0) ? 16 : 8; //前者地面容错,后者空中容错
  17. var _stepped = false;
  18. for(var i = 0; i < _max_step; i++)
  19. if !place_meeting(x + x_spd, y - i, oParentSolid)
  20. {
  21. y -= i;
  22. _stepped = true;
  23. break;
  24. }
  25. if !_stepped
  26. {
  27. while !place_meeting(x + sign(x_spd), y, oParentSolid)
  28. x += sign(x_spd);
  29. x_spd = 0;
  30. }
  31. }
  32. x += x_spd;
  33. y_spd /= global.time_scale;
  34. x_spd /= global.time_scale;
  35. }
  36. function player_status_update()
  37. {
  38. global.playerHP = clamp(global.playerHP, 0, global.save_data.player.maxHP);
  39. global.playerINK = clamp(global.playerINK, 0, global.save_data.player.maxINK);
  40. current_attacker = instance_place(x, y, oEnemyHitbox);
  41. current_hazard = instance_place(x, y, oParentHazard);
  42. current_door = instance_place(x, y, oDoor);
  43. if state != state_attack && instance_exists(current_hb)
  44. instance_destroy(current_hb);
  45. if !instance_exists(current_hb)
  46. current_hb = noone;
  47. if state != state_focus
  48. INK_blend = 0;
  49. image_index += animation_spd;
  50. if oMain._dash
  51. dash_buffer = dash_buffer_max;
  52. if oMain._attack
  53. attack_buffer = attack_buffer_max;
  54. if oMain._jump_p
  55. jump_buffer_timer = jump_buffer_max;
  56. _move_dir = oMain._right - oMain._left;
  57. _on_ground = place_meeting(x, y + 1, oParentSolid)
  58. _on_wall = place_meeting(x + 1, y, oBlockClimbable)
  59. - place_meeting(x - 1, y, oBlockClimbable);
  60. var _no_cliff = place_meeting(x - sprite_width, y + 1, oParentSolid)
  61. && place_meeting(x + sprite_width, y + 1, oParentSolid)
  62. if _on_ground && _no_cliff &&
  63. current_hazard == noone && current_attacker == noone
  64. {
  65. last_safe_x = x;
  66. last_safe_y = y;
  67. }
  68. if _on_ground || (_on_wall != 0)
  69. can_dash = true;
  70. if _on_ground
  71. {
  72. jump_cnt = 0;
  73. coyote_timer = coyote_max;
  74. }
  75. else if coyote_timer <= 0 && jump_cnt == 0
  76. jump_cnt = 1;
  77. if dash_cooldown > 0
  78. dash_cooldown -= global.time_scale;
  79. if dash_buffer > 0
  80. dash_buffer -= global.time_scale;
  81. if attack_buffer > 0
  82. attack_buffer -= global.time_scale;
  83. if move_lock_timer > 0
  84. move_lock_timer -= global.time_scale;
  85. if coyote_timer > 0
  86. coyote_timer -= global.time_scale;
  87. if invincible_timer > 0
  88. invincible_timer -= global.time_scale;
  89. if enter_room_timer > 0
  90. enter_room_timer -= global.time_scale;
  91. if jump_buffer_timer > 0
  92. jump_buffer_timer -= global.time_scale;
  93. if flash_timer > 0
  94. flash_timer -= global.time_scale;
  95. }
  96. function player_check_dash()
  97. {
  98. if dash_buffer > 0 && dash_cooldown <= 0 && can_dash
  99. {
  100. dash_buffer = 0;
  101. state = state_dash;
  102. set_sprite(sPlayerDash);
  103. dash_cooldown = 36;
  104. x_spd = image_xscale * walk_spd * 3;
  105. y_spd = 0;
  106. can_dash = false;
  107. }
  108. }
  109. function player_check_movement()
  110. {
  111. if move_lock_timer <= 0
  112. x_spd = _move_dir * walk_spd;
  113. }
  114. function player_check_jump()
  115. {
  116. if jump_buffer_timer > 0
  117. {
  118. if coyote_timer > 0
  119. {
  120. y_spd = jump_spd;
  121. jump_cnt = 1;
  122. jump_buffer_timer = 0;
  123. coyote_timer = 0;
  124. }
  125. else if _on_wall != 0
  126. {
  127. y_spd = jump_spd;
  128. x_spd = -_on_wall * walk_spd * 1.5;
  129. jump_cnt = 1;
  130. move_lock_timer = 10;
  131. jump_buffer_timer = 0;
  132. }
  133. else if jump_cnt < jump_max
  134. {
  135. icl(oDoubleJumpEffect);
  136. y_spd = jump_spd * 0.9;
  137. jump_cnt++;
  138. jump_buffer_timer = 0;
  139. }
  140. }
  141. if oMain._jump_r && y_spd < 0
  142. y_spd *= 0.4;
  143. }
  144. function player_check_attack()
  145. {
  146. if attack_buffer > 0
  147. {
  148. attack_buffer = 0;
  149. state = state_attack;
  150. set_sprite(sPlayerAttack);
  151. }
  152. }
  153. function player_check_dodge()
  154. {
  155. if (oMain._down || oMain._up) && _on_ground
  156. && (oMain._dash || state == state_dash)
  157. {
  158. state = state_dodge;
  159. set_sprite(sPlayerDodgeWait);
  160. dodge_phase = "WAIT";
  161. dodge_timer = 0;
  162. marked_target = noone;
  163. is_marked = false;
  164. is_perfect = false;
  165. var _hb = icl(oPlayerHitboxMark, x + facing * 60, y + 40);
  166. _hb.owner = id;
  167. _hb.x_offset = 60 * facing;
  168. _hb.y_offset = 40;
  169. }
  170. }
  171. function player_check_death()
  172. {
  173. if state == state_death || state == state_locked
  174. return;
  175. if global.playerHP <= 0
  176. {
  177. /*
  178. var _drop = round(random_range(0.4 * global.save_data.player.Credit,
  179. 0.6 * global.save_data.player.Credit));
  180. global.save_data.player.Credit -= _drop;
  181. global.save_data.player.corpse.droppedCredit = _drop;
  182. global.save_data.player.corpse.targetRoom = room;
  183. global.save_data.player.corpse.xPos = x; //lastsafe
  184. global.save_data.player.corpse.yPos = y; //lastsafe
  185. */
  186. save_game_to_disk();
  187. state = state_death;
  188. set_sprite(sPlayerDeath);
  189. var _cam = view_camera[0];
  190. var _cam_x = camera_get_view_x(_cam);
  191. var _cam_y = camera_get_view_y(_cam);
  192. var _ripple = icl(oRippleDeath);
  193. _ripple.x = (x - _cam_x);
  194. _ripple.y = (y - _cam_y);
  195. }
  196. }
  197. function player_check_focus()
  198. {
  199. if oMain._focus && global.playerINK >= 9
  200. {
  201. set_sprite(sPlayerFocus);
  202. state = state_focus;
  203. repeat(40)
  204. icl(oFocusingEffect);
  205. }
  206. }
  207. function player_check_door()
  208. {
  209. if current_door == noone
  210. || state == state_enter_door
  211. || state == state_exit_door
  212. return;
  213. global.target_door_id = current_door.target_door_id;
  214. global.target_room = current_door.target_room;
  215. global.door_direction = current_door.door_direction;
  216. var _fade = icl(oFade);
  217. _fade._callback = function()
  218. {
  219. room_goto(global.target_room);
  220. camera_snap();
  221. };
  222. state = state_enter_door;
  223. x_spd = 0;
  224. y_spd = 0;
  225. }
  226. function state_enter_door()
  227. {
  228. switch(global.door_direction)
  229. {
  230. case "UP":
  231. y_spd += player_calc_gravity();
  232. set_sprite(sPlayerJump);
  233. image_index = 1;
  234. break;
  235. case "DOWN":
  236. y_spd = min(-3, y_spd);
  237. set_sprite(sPlayerJump);
  238. image_index = 0;
  239. break;
  240. case "LEFT":
  241. facing = 1;
  242. x_spd = facing * walk_spd;
  243. set_sprite(sPlayerWalk);
  244. break;
  245. case "RIGHT":
  246. facing = -1;
  247. x_spd = facing * walk_spd;
  248. set_sprite(sPlayerWalk);
  249. break;
  250. }
  251. }
  252. function state_exit_door()
  253. {
  254. y_spd += 0.5 * player_calc_gravity();
  255. switch(global.door_direction)
  256. {
  257. case "LEFT":
  258. facing = -1;
  259. x_spd = facing * walk_spd;
  260. set_sprite(sPlayerWalk);
  261. break;
  262. case "RIGHT":
  263. facing = 1;
  264. x_spd = facing * walk_spd;
  265. set_sprite(sPlayerWalk);
  266. break;
  267. }
  268. if enter_room_timer <= 0
  269. {
  270. state = state_free;
  271. global.target_door_id = -1;
  272. global.target_room = noone;
  273. global.door_direction = undefined;
  274. }
  275. }
  276. function player_check_attacked()
  277. {
  278. if current_attacker == noone || invincible_timer > 0
  279. || state == state_death
  280. //|| (state == state_dodge && dodge_phase == "WAIT")
  281. return;
  282. if state == state_focus
  283. {
  284. global.playerINK -= 9;
  285. global.hitstop = 24;
  286. INK_blend = 0;
  287. flash_duration = 24;
  288. flash_timer = 24;
  289. }
  290. else global.hitstop = 12;
  291. global.playerHP -= current_attacker.damage;
  292. invincible_timer = 80;
  293. var _dir = sign(x - current_attacker.x);
  294. if _dir == 0
  295. _dir = facing;
  296. screen_shake(10);
  297. x_spd = _dir * 30;
  298. y_spd = -5;
  299. state = state_hitstun_attacked;
  300. set_sprite(sPlayerHitstunAttacked);
  301. //effects
  302. }
  303. function player_check_hazard()
  304. {
  305. if current_hazard == noone ||
  306. state == state_hitstun_hazard || state == state_death || state == state_locked
  307. return;
  308. global.playerHP -= current_hazard.damage;
  309. invincible_timer = 150;
  310. var _dir = sign(x - current_hazard.x);
  311. if _dir == 0
  312. _dir = facing;
  313. global.hitstop = 18;
  314. screen_shake(10);
  315. x_spd = _dir * 10;
  316. y_spd = -10;
  317. state = state_hitstun_hazard;
  318. set_sprite(sPlayerHitstunHazard);
  319. }
  320. function state_hitstun_attacked()
  321. {
  322. x_spd = lerp(x_spd, 0, 0.1);
  323. y_spd += player_calc_gravity();
  324. if animation_end()
  325. state = state_free;
  326. player_check_death();
  327. }
  328. function state_hitstun_hazard()
  329. {
  330. x_spd = lerp(x_spd, 0, 0.1);
  331. y_spd += player_calc_gravity();
  332. if animation_end()
  333. {
  334. state = state_locked;
  335. locked_timer = 60;
  336. var _fade = icl(oFade);
  337. _fade._callback = function()
  338. {
  339. oPlayer.x = oPlayer.last_safe_x;
  340. oPlayer.y = oPlayer.last_safe_y;
  341. camera_snap();
  342. };
  343. }
  344. player_check_death();
  345. }
  346. function state_free()
  347. {
  348. y_spd += player_calc_gravity();
  349. if _on_wall != 0 && y_spd > 0
  350. y_spd = min(y_spd, 3);
  351. /*下面是动画与视觉*/
  352. animation_spd = 0.25;
  353. if _move_dir != 0 //有输入
  354. {
  355. facing = _move_dir;
  356. if _on_ground
  357. set_sprite(sPlayerWalk); //走路
  358. }
  359. else set_sprite(sPlayerIdle); //待机
  360. if !_on_ground
  361. {
  362. if _on_wall == facing
  363. set_sprite(sPlayerWall);
  364. else
  365. {
  366. set_sprite(sPlayerJump);
  367. image_index = (y_spd < 0) ? 0 : 1;
  368. }
  369. }
  370. /*
  371. player_check_dodge();
  372. if state == state_dodge return;
  373. */
  374. player_check_movement();
  375. player_check_dash();
  376. player_check_jump();
  377. player_check_attack();
  378. player_check_focus();
  379. }
  380. function state_dash()
  381. {
  382. y_spd += 0.2 * player_calc_gravity();
  383. icl(oPlayerAfterImage);
  384. if animation_end()
  385. {
  386. if !_on_ground
  387. jump_cnt = max(jump_cnt, 1);
  388. state = state_free;
  389. player_check_attack();
  390. }
  391. //player_check_dodge();
  392. player_check_focus();
  393. }
  394. function state_attack()
  395. {
  396. player_check_movement();
  397. player_check_jump();
  398. player_check_dash();
  399. player_check_focus();
  400. y_spd += player_calc_gravity();
  401. if image_index == 1
  402. {
  403. if oMain._down && !_on_ground
  404. current_hb = icl(oPlayerHitboxDown);
  405. else if oMain._up
  406. {
  407. current_hb = icl(oPlayerHitboxUp, x, y - 128);
  408. current_hb.y_offset = -128;
  409. }
  410. else
  411. {
  412. current_hb = icl(oPlayerHitboxHor, x, y - 64);
  413. current_hb.image_xscale = facing;
  414. current_hb.y_offset = -64;
  415. current_hb.hit_info.kbFactorX *= facing;
  416. }
  417. current_hb.owner = id;
  418. }
  419. // 攻击后摇结束
  420. if current_hb != noone
  421. if _on_ground && current_hb.object_index == oPlayerHitboxDown
  422. state = state_free;
  423. if facing = -_move_dir
  424. state = state_free;
  425. if animation_end()
  426. state = state_free;
  427. }
  428. function state_locked()
  429. {
  430. locked_timer -= global.time_scale;
  431. x_spd = 0;
  432. y_spd += player_calc_gravity();
  433. if locked_timer <= 0
  434. state = state_free;
  435. }
  436. function state_death()
  437. {
  438. x_spd = 0;
  439. y_spd = 0;
  440. invincible_timer = 150;
  441. if animation_end()
  442. {
  443. state = state_locked;
  444. invincible_timer = 150;
  445. locked_timer = 90;
  446. var _fade = icl(oFade);
  447. _fade._callback = function()
  448. {
  449. room_goto(asset_get_index(global.save_data.world.current_room));
  450. global.playerHP = global.save_data.player.maxHP;
  451. global.playerINK = 0;
  452. }
  453. }
  454. }
  455. function state_focus()
  456. {
  457. x_spd = 0;
  458. y_spd = 0;
  459. if animation_end()
  460. {
  461. player_add_INK(-9);
  462. global.playerHP += 3;
  463. global.playerHP = clamp(global.playerHP, 0, global.save_data.player.maxHP);
  464. state = state_free;
  465. flash_timer = 15;
  466. flash_duration = 15;
  467. INK_blend = 0;
  468. repeat(30)
  469. icl(oFocusEndEffect, x, y - sprite_height);
  470. }
  471. }
  472. /// @desc 返回基于玩家当前状态的重力值,注意不会自动赋值
  473. function player_calc_gravity()
  474. {
  475. var _g = global.g;
  476. if y_spd > 0
  477. _g *= 1.2; // 下坠
  478. if abs(y_spd) < 0.5
  479. _g *= 0.75; //滞空
  480. return _g; // * global.time_scale;
  481. }
  482. /*
  483. function state_dodge()
  484. {
  485. if dodge_phase == "WAIT"
  486. {
  487. x_spd = 0;
  488. if current_attacker != noone || animation_end()
  489. {
  490. y_spd = jump_spd;
  491. dodge_phase = "DODGE";
  492. set_sprite(sPlayerDodge);
  493. if current_attacker != noone && is_marked
  494. {
  495. is_perfect = true;
  496. invincible_timer = 150;
  497. global.time_scale = 0.2;
  498. global.hitstop = 20;
  499. }
  500. }
  501. }
  502. else if dodge_phase == "DODGE"
  503. {
  504. y_spd += global.g;
  505. if is_perfect icl(oPlayerAfterImage);
  506. if y_spd > 0
  507. {
  508. timer = 0;
  509. y_spd = 0;
  510. if is_perfect
  511. {
  512. state = state_arc_slash;
  513. set_sprite(sPlayerArcSlash);
  514. }
  515. else
  516. {
  517. state = state_dodge_ending;
  518. set_sprite(sPlayerDodgeEnding);
  519. }
  520. }
  521. }
  522. }
  523. function state_arc_slash()
  524. {
  525. if image_index == 12
  526. {
  527. var _hb = icd(oPlayerHitboxArc);
  528. _hb.owner = id;
  529. if facing == 1
  530. {
  531. _hb.image_xscale = 1;
  532. _hb.image_angle = 30;
  533. _hb.kb_factor_x *= 1;
  534. }
  535. else
  536. {
  537. _hb.image_xscale = -1;
  538. _hb.image_angle = -30;
  539. _hb.kb_factor_x *= -1;
  540. }
  541. }
  542. else if image_index > 12
  543. y_spd += global.g;
  544. if animation_end()
  545. {
  546. global.time_scale = 1.0;
  547. state = state_free;
  548. }
  549. }
  550. function state_dodge_ending()
  551. {
  552. if animation_end()
  553. state = state_free;
  554. }
  555. */