base_movable.gd 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. extends KinematicBody
  2. enum DIRECTIONS {NONE, UP, DOWN, LEFT, RIGHT}
  3. var direction = null
  4. var last_position = Vector3()
  5. var target_position = Vector3()
  6. var movedir = Vector3()
  7. var tile_size = 2
  8. var position_without_height = Vector3()
  9. var was_on_ground = true
  10. onready var ray_side = $RayCastSide
  11. onready var ray_ground = $RayCastGround
  12. onready var mesh_instance = $MeshInstance
  13. export var speed = 7.5
  14. export var roll_speed = 0
  15. const DEACCELATION = 0.3
  16. const GRAVITY = -10
  17. func _ready():
  18. direction = DIRECTIONS.NONE
  19. translation = translation.snapped(Vector3(tile_size, tile_size, tile_size))
  20. last_position = Vector3(translation.x, 0, translation.z)
  21. target_position = Vector3(translation.x, 0, translation.z)
  22. #print ("init - x: " + String(translation.x) + ", y: " + String(translation.y) + ", z:" + String(translation.z))
  23. func get_movedir():
  24. if direction == DIRECTIONS.UP:
  25. movedir = Vector3(0, 0, -1)
  26. elif direction == DIRECTIONS.DOWN:
  27. movedir = Vector3(0, 0, 1)
  28. elif direction == DIRECTIONS.LEFT:
  29. movedir = Vector3(-1, 0, 0)
  30. elif direction == DIRECTIONS.RIGHT:
  31. movedir = Vector3(1, 0, 0)
  32. else:
  33. movedir = Vector3(0, 0, 0)
  34. if direction != DIRECTIONS.NONE:
  35. ray_side.cast_to = movedir
  36. func move(delta):
  37. position_without_height = Vector3(translation.x, 0, translation.z)
  38. if !ray_ground.is_colliding() and !was_on_ground:
  39. translation.y += GRAVITY * delta
  40. was_on_ground = ray_ground.is_colliding()
  41. #
  42. # print ("push!!!!!!!!!!!!!!!!!")
  43. # collidingObject.push(direction)
  44. if ray_side.is_colliding():
  45. position_without_height = Vector3(last_position.x, 0, last_position.z)
  46. target_position = Vector3(last_position.x, 0, last_position.z)
  47. else:
  48. position_without_height += speed * movedir * delta
  49. if position_without_height.distance_to(Vector3(last_position.x, 0, last_position.z)) >= tile_size - speed * delta:
  50. position_without_height = target_position
  51. if position_without_height == target_position:
  52. get_movedir()
  53. last_position = position_without_height
  54. target_position += movedir * tile_size
  55. #print ("x: " + String(translation.x) + ", y: " + String(translation.y) + ", z:" + String(translation.z))
  56. translation.x = position_without_height.x
  57. translation.z = position_without_height.z
  58. func roll(delta):
  59. #print ("???")
  60. if roll_speed == 0:
  61. print ("not rolling")
  62. return
  63. print ("rolling")
  64. if last_position.x > translation.x:
  65. mesh_instance.rotate_z(deg2rad(roll_speed) * delta)
  66. elif last_position.x < translation.x:
  67. mesh_instance.rotate_z(-deg2rad(roll_speed) * delta)
  68. elif last_position.z > translation.z:
  69. mesh_instance.rotate_x(-deg2rad(roll_speed) * delta)
  70. elif last_position.z < translation.z:
  71. mesh_instance.rotate_x(deg2rad(roll_speed) * delta)
  72. func _physics_process(delta):
  73. move(delta)
  74. roll(delta)