PlayerStates.gml 12 KB

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