PlayerStates.gml 11 KB

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