Guest User

Cogwheels Pixel Movement

a guest
Jun 29th, 2017
148
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # http://members.jcom.home.ne.jp/cogwheel/
  2. #==============================================================================
  3. # Game_Player
  4. #==============================================================================
  5. class Game_Player < Game_Character
  6. UP = 0
  7. DOWN = 0
  8. SIDE = 0
  9. SLANT = false
  10. #--------------------------------------------------------------------------
  11. attr_reader :event
  12. attr_accessor :move_speed
  13. #--------------------------------------------------------------------------
  14. alias :update_original :update
  15. def update
  16. @walk = 4
  17. @dash = 5
  18. @event = 4
  19. @dot_m = true
  20. unless moving? or $game_system.map_interpreter.running? or
  21. @move_route_forcing or $game_temp.message_window_showing
  22. if @walk != @dash
  23. if Input.press?(Input::C)
  24. if @move_speed != @dash
  25. @move_speed = @dash
  26. end
  27. else
  28. if @move_speed != @walk
  29. @move_speed = @walk
  30. end
  31. end
  32. end
  33. end
  34. if @revise_x == nil and @revise_y == nil
  35. @revise_x = 0
  36. @revise_y = 0
  37. end
  38. unless @dot_m
  39. update_original
  40. return
  41. end
  42. if @move_route_forcing
  43. last_moving = moving?
  44. last_real_x = @real_x
  45. last_real_y = @real_y
  46. if (@revise_x != 0 or @revise_y != 0) and not jumping? and @move == true
  47. if @revise_x != @real_x - @x * 128 or @revise_y != @real_y - @y * 128
  48. @revise_x = @real_x - @x * 128
  49. @revise_y = @real_y - @y * 128
  50. end
  51. distance1 = 2 ** @move_speed
  52. distance2 = Math.sqrt(@revise_x ** 2 + @revise_y ** 2)
  53. if distance1 > distance2
  54. @real_x = @real_x - @revise_x
  55. @real_y = @real_y - @revise_y
  56. @revise_x = 0
  57. @revise_y = 0
  58. anime_update
  59. else
  60. @real_x -= (distance1 * @revise_x / distance2).round
  61. @real_y -= (distance1 * @revise_y / distance2).round
  62. @revise_x = @real_x - @x * 128
  63. @revise_y = @real_y - @y * 128
  64. anime_update
  65. end
  66. else
  67. super
  68. end
  69. else
  70. @move = false
  71. unless moving? or $game_system.map_interpreter.running? or
  72. @move_route_forcing or $game_temp.message_window_showing
  73. @event_run = false
  74. case Input.dir8
  75. when 1
  76. move_lower_left_p
  77. when 2
  78. move_down_p
  79. when 3
  80. move_lower_right_p
  81. when 4
  82. move_left_p
  83. when 6
  84. move_right_p
  85. when 7
  86. move_upper_left_p
  87. when 8
  88. move_up_p
  89. when 9
  90. move_upper_right_p
  91. end
  92. end
  93. last_real_x = @real_x
  94. last_real_y = @real_y
  95. @real_x = @x * 128 + @revise_x
  96. @real_y = @y * 128 + @revise_y
  97. last_moving = moving?
  98. move_on
  99. if (last_real_x != @real_x or last_real_y != @real_y)
  100. @move_distance = 0 if @move_distance == nil
  101. @move_distance += Math.sqrt((last_real_x - @real_x) ** 2 +
  102. (last_real_y - @real_y) ** 2)
  103. if @move_distance >= 128
  104. @move_distance %= 128
  105. increase_steps
  106. end
  107. anime_update
  108. else
  109. @pattern = 0
  110. end
  111. end
  112. if @real_y > last_real_y and @real_y - $game_map.display_y > CENTER_Y
  113. $game_map.scroll_down(@real_y - last_real_y)
  114. end
  115. if @real_x < last_real_x and @real_x - $game_map.display_x < CENTER_X
  116. $game_map.scroll_left(last_real_x - @real_x)
  117. end
  118. if @real_x > last_real_x and @real_x - $game_map.display_x > CENTER_X
  119. $game_map.scroll_right(@real_x - last_real_x)
  120. end
  121. if @real_y < last_real_y and @real_y - $game_map.display_y < CENTER_Y
  122. $game_map.scroll_up(last_real_y - @real_y)
  123. end
  124. if last_moving
  125. result = check_event_trigger_here([1,2])
  126. if result == false
  127. unless $DEBUG and Input.press?(Input::CTRL)
  128. if @encounter_count > 0
  129. @encounter_count -= 1
  130. end
  131. end
  132. end
  133. end
  134. if Input.trigger?(Input::C)
  135. check_event_trigger_here([0])
  136. check_event_trigger_there([0,1,2])
  137. end
  138. end
  139. #--------------------------------------------------------------------------
  140. def initialize
  141. @revise_x = 0
  142. @revise_y = 0
  143. @move == false
  144. super
  145. end
  146. #--------------------------------------------------------------------------
  147. def moving?
  148. unless @dot_m
  149. result = super
  150. return result
  151. end
  152. if @move_route_forcing
  153. if @move == false
  154. return false
  155. end
  156. super
  157. else
  158. return (@x != (@real_x / 128.0).round or @y != (@real_y / 128.0).round)
  159. end
  160. end
  161. #--------------------------------------------------------------------------
  162. def moving_a?
  163. if @move == false
  164. if (@move_route.list[@move_route_index].code <= 14 or
  165. @move_route.list[@move_route_index].code == 25)
  166. @move = true
  167. end
  168. return false
  169. end
  170. moving?
  171. end
  172. #--------------------------------------------------------------------------
  173. def update_jump
  174. @jump_count -= 1
  175. @real_x = (@real_x * @jump_count + @x * 128) / (@jump_count + 1)
  176. @real_y = (@real_y * @jump_count + @y * 128) / (@jump_count + 1)
  177. if @jump_count == 0
  178. @revise_x = 0
  179. @revise_y = 0
  180. end
  181. end
  182. #--------------------------------------------------------------------------
  183. def move_type_custom
  184. unless @dot_m
  185. super
  186. return
  187. end
  188. if jumping? or moving_a?
  189. return
  190. end
  191. while @move_route_index < @move_route.list.size
  192. command = @move_route.list[@move_route_index]
  193. if command.code == 0
  194. if @move_route.repeat
  195. @move_route_index = 0
  196. end
  197. unless @move_route.repeat
  198. if @move_route_forcing and not @move_route.repeat
  199. @move_route_forcing = false
  200. @move_route = @original_move_route
  201. @move_route_index = @original_move_route_index
  202. @original_move_route = nil
  203. end
  204. @stop_count = 0
  205. end
  206. return
  207. end
  208. if command.code <= 14
  209. case command.code
  210. when 1
  211. move_down
  212. when 2
  213. move_left
  214. when 3
  215. move_right
  216. when 4
  217. move_up
  218. when 5
  219. move_lower_left
  220. when 6
  221. move_lower_right
  222. when 7
  223. move_upper_left
  224. when 8
  225. move_upper_right
  226. when 9
  227. move_random
  228. when 10
  229. move_toward_player
  230. when 11
  231. move_away_from_player
  232. when 12
  233. move_forward
  234. when 13
  235. move_backward
  236. when 14
  237. jump(command.parameters[0], command.parameters[1])
  238. end
  239. if not @move_route.skippable and not moving? and not jumping?
  240. return
  241. end
  242. @move_route_index += 1
  243. return
  244. end
  245. if command.code == 15
  246. @wait_count = command.parameters[0] * 2 - 1
  247. @move_route_index += 1
  248. return
  249. end
  250. if command.code >= 16 and command.code <= 26
  251. case command.code
  252. when 16
  253. turn_down
  254. when 17
  255. turn_left
  256. when 18
  257. turn_right
  258. when 19
  259. turn_up
  260. when 20
  261. turn_right_90
  262. when 21
  263. turn_left_90
  264. when 22
  265. turn_180
  266. when 23
  267. turn_right_or_left_90
  268. when 24
  269. turn_random
  270. when 25
  271. turn_toward_player
  272. when 26
  273. turn_away_from_player
  274. end
  275. @move_route_index += 1
  276. return
  277. end
  278. if command.code >= 27
  279. case command.code
  280. when 27
  281. $game_switches[command.parameters[0]] = true
  282. $game_map.need_refresh = true
  283. when 28
  284. $game_switches[command.parameters[0]] = false
  285. $game_map.need_refresh = true
  286. when 29
  287. @move_speed = command.parameters[0]
  288. when 30
  289. @move_frequency = command.parameters[0]
  290. when 31
  291. @walk_anime = true
  292. when 32
  293. @walk_anime = false
  294. when 33
  295. @step_anime = true
  296. when 34
  297. @step_anime = false
  298. when 35
  299. @direction_fix = true
  300. when 36
  301. @direction_fix = false
  302. when 37
  303. @through = true
  304. when 38
  305. @through = false
  306. when 39
  307. @always_on_top = true
  308. when 40
  309. @always_on_top = false
  310. when 41
  311. @tile_id = 0
  312. @character_name = command.parameters[0]
  313. @character_hue = command.parameters[1]
  314. if @original_direction != command.parameters[2]
  315. @direction = command.parameters[2]
  316. @original_direction = @direction
  317. @prelock_direction = 0
  318. end
  319. if @original_pattern != command.parameters[3]
  320. @pattern = command.parameters[3]
  321. @original_pattern = @pattern
  322. end
  323. when 42
  324. @opacity = command.parameters[0]
  325. when 43
  326. @blend_type = command.parameters[0]
  327. when 44
  328. $game_system.se_play(command.parameters[0])
  329. when 45
  330. result = eval(command.parameters[0])
  331. end
  332. @move_route_index += 1
  333. return
  334. end
  335. end
  336. end
  337. #--------------------------------------------------------------------------
  338. def move_down_p
  339. turn_down
  340. distance = 2 ** @move_speed
  341. down1(((@x * 128 + @revise_x) / 128.0).round,
  342. ((@y * 128 + @revise_y) / 128.0).round, distance, true)
  343. end
  344. #--------------------------------------------------------------------------
  345. def down1(x, y, distance, down = false)
  346. result = down2(x, y, distance)
  347. if result == false
  348. @event_run = check_event_trigger_touch(x, y+1)
  349. return result
  350. end
  351. if @revise_x < -SIDE
  352. result = down2(x, y + 1, distance, 4)
  353. result &= down2(x - 1, y, distance)
  354. if result == false
  355. if down
  356. move_lower_right_p
  357. if @revise_x > SIDE
  358. @revise_x = SIDE
  359. end
  360. end
  361. return result
  362. end
  363. elsif @revise_x > SIDE
  364. result = down2(x, y + 1, distance, 6)
  365. result &= down2(x + 1, y, distance)
  366. if result == false
  367. if down
  368. move_lower_left_p
  369. if @revise_x < -SIDE
  370. @revise_x = -SIDE
  371. end
  372. end
  373. return result
  374. end
  375. end
  376. @revise_y += distance
  377. return result
  378. end
  379. #--------------------------------------------------------------------------
  380. def down2(x, y, distance, d = 2)
  381. if @revise_y + distance > DOWN
  382. unless passable?(x, y, d)
  383. if @revise_y < DOWN
  384. @revise_y = DOWN
  385. end
  386. return false
  387. end
  388. end
  389. return true
  390. end
  391. #--------------------------------------------------------------------------
  392. def move_left_p
  393. turn_left
  394. distance = 2 ** @move_speed
  395. left1(((@x * 128 + @revise_x) / 128.0).round,
  396. ((@y * 128 + @revise_y) / 128.0).round, distance, true)
  397. end
  398. #--------------------------------------------------------------------------
  399. def left1(x, y, distance, left = false)
  400. result = left2(x, y, distance)
  401. if result == false
  402. @event_run = check_event_trigger_touch(x-1, y)
  403. return result
  404. end
  405. if @revise_y < -UP
  406. result = left2(x - 1, y, distance, 8)
  407. result &= left2(x, y - 1, distance)
  408. if result == false
  409. if left
  410. move_lower_left_p
  411. if @revise_y > DOWN
  412. @revise_y = DOWN
  413. end
  414. end
  415. return result
  416. end
  417. elsif @revise_y > DOWN
  418. result = left2(x - 1, y, distance, 2)
  419. result &= left2(x, y + 1, distance)
  420. if result == false
  421. if left
  422. move_upper_left_p
  423. if @revise_y < -UP
  424. @revise_y = -UP
  425. end
  426. end
  427. return result
  428. end
  429. end
  430. @revise_x -= distance
  431. return result
  432. end
  433. #--------------------------------------------------------------------------
  434. def left2(x, y, distance, d = 4)
  435. if @revise_x - distance < -SIDE
  436. unless passable?(x, y, d)
  437. if @revise_x > -SIDE
  438. @revise_x = -SIDE
  439. end
  440. return false
  441. end
  442. end
  443. return true
  444. end
  445. #--------------------------------------------------------------------------
  446. def move_right_p
  447. turn_right
  448. distance = 2 ** @move_speed
  449. right1(((@x * 128 + @revise_x) / 128.0).round,
  450. ((@y * 128 + @revise_y) / 128.0).round, distance, true)
  451. end
  452. #--------------------------------------------------------------------------
  453. def right1(x, y, distance, right = false)
  454. result = right2(x, y, distance)
  455. if result == false
  456. @event_run = check_event_trigger_touch(x+1, y)
  457. return result
  458. end
  459. if @revise_y < -UP
  460. result = right2(x + 1, y, distance, 8)
  461. result &= right2(x, y - 1, distance)
  462. if result == false
  463. if right
  464. move_lower_right_p
  465. if @revise_y > DOWN
  466. @revise_y = DOWN
  467. end
  468. end
  469. return result
  470. end
  471. elsif @revise_y > DOWN
  472. result = right2(x + 1, y, distance, 2)
  473. result &= right2(x, y + 1, distance)
  474. if result == false
  475. if right
  476. move_upper_right_p
  477. if @revise_y < -UP
  478. @revise_y = -UP
  479. end
  480. end
  481. return result
  482. end
  483. end
  484. @revise_x += distance
  485. return result
  486. end
  487. #--------------------------------------------------------------------------
  488. def right2(x, y, distance, d = 6)
  489. if @revise_x + distance > SIDE
  490. unless passable?(x, y, d)
  491. if @revise_x < SIDE
  492. @revise_x = SIDE
  493. end
  494. return false
  495. end
  496. end
  497. return true
  498. end
  499. #--------------------------------------------------------------------------
  500. def move_up_p
  501. turn_up
  502. distance = 2 ** @move_speed
  503. up1(((@x * 128 + @revise_x) / 128.0).round,
  504. ((@y * 128 + @revise_y) / 128.0).round, distance, true)
  505. end
  506. #--------------------------------------------------------------------------
  507. def up1(x, y, distance, up = false)
  508. result = up2(x, y, distance)
  509. if result == false
  510. @event_run = check_event_trigger_touch(x, y-1)
  511. return result
  512. end
  513. if @revise_x < -SIDE
  514. result = up2(x, y - 1, distance, 4)
  515. result &= up2(x - 1, y, distance)
  516. if result == false
  517. if up
  518. move_upper_right_p
  519. if @revise_x > SIDE
  520. @revise_x = SIDE
  521. end
  522. end
  523. return result
  524. end
  525. elsif @revise_x > SIDE
  526. result = up2(x, y - 1, distance, 6)
  527. result &= up2(x + 1, y, distance)
  528. if result == false
  529. if up
  530. move_upper_left_p
  531. if @revise_x < -SIDE
  532. @revise_x = -SIDE
  533. end
  534. end
  535. return result
  536. end
  537. end
  538. @revise_y -= distance
  539. return result
  540. end
  541. #--------------------------------------------------------------------------
  542. def up2(x, y, distance, d = 8)
  543. if @revise_y - distance < -UP
  544. unless passable?(x, y, d)
  545. if @revise_y > -UP
  546. @revise_y = -UP
  547. end
  548. return false
  549. end
  550. end
  551. return true
  552. end
  553. #--------------------------------------------------------------------------
  554. def move_lower_left_p
  555. unless @direction_fix
  556. @direction = (@direction == 6 ? 4 : @direction == 8 ? 2 : @direction)
  557. end
  558. distance = (2 ** @move_speed) / Math.sqrt(2)
  559. turn_left unless down1(((@x * 128 + @revise_x) / 128.0).round,
  560. ((@y * 128 + @revise_y) / 128.0).round, distance)
  561. turn_down if @event_run
  562. unless @event_run
  563. if last_move?(@real_x, @real_y, 2, distance)
  564. result = check_event_trigger_here([1,2], false)
  565. if result == true
  566. return
  567. end
  568. end
  569. move_on
  570. if @revise_y > DOWN and -UP > @revise_y - distance
  571. @revise_y = DOWN
  572. end
  573. turn_down unless left1(((@x * 128 + @revise_x) / 128.0).round,
  574. ((@y * 128 + @revise_y) / 128.0).round, distance)
  575. turn_left if @event_run
  576. end
  577. end
  578. #--------------------------------------------------------------------------
  579. def move_lower_right_p
  580. unless @direction_fix
  581. @direction = (@direction == 4 ? 6 : @direction == 8 ? 2 : @direction)
  582. end
  583. distance = (2 ** @move_speed) / Math.sqrt(2)
  584. turn_right unless down1(((@x * 128 + @revise_x) / 128.0).round,
  585. ((@y * 128 + @revise_y) / 128.0).round, distance)
  586. turn_down if @event_run
  587. unless @event_run
  588. if last_move?(@real_x, @real_y, 2, distance)
  589. result = check_event_trigger_here([1,2], false)
  590. if result == true
  591. return
  592. end
  593. end
  594. move_on
  595. if @revise_y > DOWN and -UP > @revise_y - distance
  596. @revise_y = DOWN
  597. end
  598. turn_down unless right1(((@x * 128 + @revise_x) / 128.0).round,
  599. ((@y * 128 + @revise_y) / 128.0).round, distance)
  600. turn_right if @event_run
  601. end
  602. end
  603. #--------------------------------------------------------------------------
  604. def move_upper_left_p
  605. unless @direction_fix
  606. @direction = (@direction == 6 ? 4 : @direction == 2 ? 8 : @direction)
  607. end
  608. distance = (2 ** @move_speed) / Math.sqrt(2)
  609. turn_left unless up1(((@x * 128 + @revise_x) / 128.0).round,
  610. ((@y * 128 + @revise_y) / 128.0).round, distance)
  611. turn_up if @event_run
  612. unless @event_run
  613. if last_move?(@real_x, @real_y, 8, distance)
  614. result = check_event_trigger_here([1,2], false)
  615. if result == true
  616. return
  617. end
  618. end
  619. move_on
  620. if @revise_y + distance > DOWN and -UP > @revise_y
  621. @revise_y = -UP
  622. end
  623. turn_up unless left1(((@x * 128 + @revise_x) / 128.0).round,
  624. ((@y * 128 + @revise_y) / 128.0).round, distance)
  625. turn_left if @event_run
  626. end
  627. end
  628. #--------------------------------------------------------------------------
  629. def move_upper_right_p
  630. unless @direction_fix
  631. @direction = (@direction == 4 ? 6 : @direction == 2 ? 8 : @direction)
  632. end
  633. distance = (2 ** @move_speed) / Math.sqrt(2)
  634. turn_right unless up1(((@x * 128 + @revise_x) / 128.0).round,
  635. ((@y * 128 + @revise_y) / 128.0).round, distance)
  636. turn_up if @event_run
  637. unless @event_run
  638. if last_move?(@real_x, @real_y, 8, distance)
  639. result = check_event_trigger_here([1,2], false)
  640. if result == true
  641. return
  642. end
  643. end
  644. move_on
  645. if @revise_y + distance > DOWN and -UP > @revise_y
  646. @revise_y = -UP
  647. end
  648. turn_up unless right1(((@x * 128 + @revise_x) / 128.0).round,
  649. ((@y * 128 + @revise_y) / 128.0).round, distance)
  650. turn_right if @event_run
  651. end
  652. end
  653. #--------------------------------------------------------------------------
  654. def check_event_trigger_here(triggers, run = true)
  655. result = false
  656. if $game_system.map_interpreter.running?
  657. return result
  658. end
  659. for event in $game_map.events.values
  660. if event.x == ((@x * 128 + @revise_x) / 128.0).round and
  661. event.y == ((@y * 128 + @revise_y) / 128.0).round and
  662. triggers.include?(event.trigger)
  663. if not event.jumping? and event.over_trigger?
  664. if event.list.size > 1
  665. if run == true
  666. event.start
  667. end
  668. result = true
  669. end
  670. end
  671. end
  672. end
  673. return result
  674. end
  675. #--------------------------------------------------------------------------
  676. def move_on
  677. if @y < (@y + @revise_y / 128.0).round
  678. @y += 1
  679. @revise_y -= 128
  680. end
  681. if @x > (@x + @revise_x / 128.0).round
  682. @x -= 1
  683. @revise_x += 128
  684. end
  685. if @x < (@x + @revise_x / 128.0).round
  686. @x += 1
  687. @revise_x -= 128
  688. end
  689. if @y > (@y + @revise_y / 128.0).round
  690. @y -= 1
  691. @revise_y += 128
  692. end
  693. end
  694. #--------------------------------------------------------------------------
  695. def anime_update
  696. if @walk_anime
  697. @anime_count += 1.5
  698. elsif @step_anime
  699. @anime_count += 1
  700. end
  701. if @anime_count > 18 - @move_speed * 2
  702. if not @step_anime and @stop_count > 0
  703. @pattern = @original_pattern
  704. else
  705. @pattern = (@pattern + 1) % 4
  706. end
  707. @anime_count = 0
  708. end
  709. end
  710. #--------------------------------------------------------------------------
  711. alias :moveto_original :moveto
  712. def moveto(x, y)
  713. @revise_x = 0
  714. @revise_y = 0
  715. moveto_original(x, y)
  716. end
  717. #--------------------------------------------------------------------------
  718. def last_move?(x, y, direction, distance)
  719. if direction == 2 or direction == 6
  720. distance *= -1
  721. end
  722. if (direction == 2 or direction == 8) and
  723. (y / 128.0).round != ((y - distance) / 128.0).round
  724. return true
  725. end
  726. if (direction == 4 or direction == 6) and
  727. (x / 128.0).round != ((x - distance) / 128.0).round
  728. return true
  729. end
  730. return false
  731. end
  732. end
  733.  
  734. #==============================================================================
  735. # Game_Character
  736. #==============================================================================
  737. class Game_Character
  738. def update_move
  739. distance = 2 ** @move_speed
  740. if @x * 128 != @real_x and @y * 128 != @real_y and Game_Player::SLANT
  741. distance /= Math.sqrt(2)
  742. end
  743. if @y * 128 > @real_y
  744. @real_y = [@real_y + distance, @y * 128].min
  745. end
  746. if @x * 128 < @real_x
  747. @real_x = [@real_x - distance, @x * 128].max
  748. end
  749. if @x * 128 > @real_x
  750. @real_x = [@real_x + distance, @x * 128].min
  751. end
  752. if @y * 128 < @real_y
  753. @real_y = [@real_y - distance, @y * 128].max
  754. end
  755. if @walk_anime
  756. @anime_count += 1.5
  757. elsif @step_anime
  758. @anime_count += 1
  759. end
  760. end
  761. end
  762. #==============================================================================
  763. # Game_Event
  764. #==============================================================================
  765. class Game_Event < Game_Character
  766. def start
  767. if @list.size > 1
  768. if $game_player.event != 0
  769. $game_player.move_speed = $game_player.event
  770. end
  771. @starting = true
  772. end
  773. end
  774. end
RAW Paste Data