player.gd 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. extends "res://entities/base_movable.gd"
  2. export var color = ""
  3. onready var timer = get_node("Timer")
  4. enum STATE {ACTIVE, INACTIVE, FINISHED, IN_FINISH_AREA}
  5. var current_state = STATE.INACTIVE
  6. func set_state(state):
  7. current_state = state
  8. var glow_state = 0
  9. if state == STATE.ACTIVE:
  10. glow_state = 1
  11. if mesh_instance.get_surface_material(0).has_method("set_shader_param"):
  12. mesh_instance.get_surface_material(0).set_shader_param("Glowing", glow_state)
  13. func _ready():
  14. pass
  15. func get_color():
  16. return color
  17. func _process(delta):
  18. handle_player_input()
  19. #if a short input comes, it keeps the direction until the timer is finished
  20. # and makes sure that the player moves after moving a box when input is too short
  21. #else the player still detects the box and stops moving
  22. func start_timer_after_impulse():
  23. if timer.is_stopped():
  24. timer.start()
  25. func get_keys_pressed_amount():
  26. var keys = 0
  27. if Input.is_action_pressed("ui_right"):
  28. keys += 1
  29. if Input.is_action_pressed("ui_left"):
  30. keys += 1
  31. if Input.is_action_pressed("ui_up"):
  32. keys += 1
  33. if Input.is_action_pressed("ui_down"):
  34. keys += 1
  35. return keys
  36. func handle_player_input():
  37. var just_moved = false
  38. if current_state == STATE.ACTIVE:
  39. if Input.is_action_just_pressed("ui_right"):
  40. direction = DIRECTIONS.RIGHT
  41. just_moved = true
  42. start_timer_after_impulse()
  43. elif Input.is_action_just_pressed("ui_left"):
  44. direction = DIRECTIONS.LEFT
  45. just_moved = true
  46. start_timer_after_impulse()
  47. elif Input.is_action_just_pressed("ui_up"):
  48. direction = DIRECTIONS.UP
  49. just_moved = true
  50. start_timer_after_impulse()
  51. elif Input.is_action_just_pressed("ui_down"):
  52. direction = DIRECTIONS.DOWN
  53. just_moved = true
  54. start_timer_after_impulse()
  55. elif Input.is_action_pressed("ui_right") and direction == DIRECTIONS.RIGHT:
  56. direction = DIRECTIONS.RIGHT
  57. elif Input.is_action_pressed("ui_left") and direction == DIRECTIONS.LEFT:
  58. direction = DIRECTIONS.LEFT
  59. elif Input.is_action_pressed("ui_up") and direction == DIRECTIONS.UP:
  60. direction = DIRECTIONS.UP
  61. elif Input.is_action_pressed("ui_down") and direction == DIRECTIONS.DOWN:
  62. direction = DIRECTIONS.DOWN
  63. elif Input.is_action_pressed("ui_right"):
  64. direction = DIRECTIONS.RIGHT
  65. elif Input.is_action_pressed("ui_left"):
  66. direction = DIRECTIONS.LEFT
  67. elif Input.is_action_pressed("ui_up"):
  68. direction = DIRECTIONS.UP
  69. elif Input.is_action_pressed("ui_down"):
  70. direction = DIRECTIONS.DOWN
  71. else:
  72. if timer.get_time_left() == 0:
  73. direction = DIRECTIONS.NONE
  74. if direction != DIRECTIONS.NONE and ray_side.is_colliding():
  75. #print (target_position)
  76. var collidingObject = ray_side.get_collider()
  77. #var keys_pressed = get_keys_pressed_amount()
  78. #print (keys_pressed)
  79. if collidingObject.is_in_group("group_box") and target_position == collidingObject.position_without_height:
  80. #print ("moving and colliding!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  81. if collidingObject.color == "none" or color == collidingObject.color:
  82. #print ("push!!!!!!!!!!!!!!!!!")
  83. if direction == DIRECTIONS.UP or direction == DIRECTIONS.DOWN:
  84. if abs(translation.x - collidingObject.translation.x) < 0.1:
  85. #print ("pushing up or down")
  86. collidingObject.push(direction)
  87. #else:
  88. # print ("error in pushing")
  89. if direction == DIRECTIONS.LEFT or direction == DIRECTIONS.RIGHT:
  90. #if position_without_height.distance_to(Vector3(last_position.x, 0, last_position.z)) >= tile_size - speed * delta:
  91. if abs(translation.z - collidingObject.translation.z) < 0.1:
  92. #print ("pushing left or right")
  93. collidingObject.push(direction)
  94. #else:
  95. # print ("error in pushing")
  96. if translation.y < -1.9:
  97. #FIXME: add a timer before restarting the level
  98. get_tree().reload_current_scene()
  99. else:
  100. direction = DIRECTIONS.NONE