12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- extends Area2D
- # Nodes
- export (NodePath) var gridNode
- var grid:TileMap
- var tween
- var rayFront
- var rayLeft
- var rayRight
- # Misc
- var cell_size
- var cell_half_size
- # Enum
- const DIR_UP = 0
- const DIR_RIGHT = 1
- const DIR_DOWN = 2
- const DIR_LEFT = 3
- # Movement
- var posix
- var posiy
- var dirx = 0
- var diry = 0
- export var dire = DIR_UP
- var dire_delta = 0
- var is_moving = false
- var target_pos
- func _ready():
- grid = get_node(gridNode)
- cell_size = int (grid.get_cell_size().x)
- cell_half_size = cell_size / 2
- posix = int (position.x / 64)
- posiy = int (position.y / 64)
- turn(dire)
- tween = $Tween
- tween.connect_into(self)
- rayFront = $RayFront
- rayLeft = $RayLeft
- rayRight = $RayRight
- func _physics_process(delta):
- if Input.is_action_just_pressed("ui_left"):
- dire_delta = -1
- elif Input.is_action_just_pressed("ui_right"):
- dire_delta = 1
-
- if is_moving:
- return
- dire += dire_delta
- if dire_delta == -1:
- tween.rotate_char(self, rotation_degrees - 90)
- if dire < 0:
- dire = 3
- elif dire_delta == 1:
- tween.rotate_char(self, rotation_degrees + 90)
- if dire > 3:
- dire = 0
- turn(dire)
- dire_delta = 0
- posix += dirx
- posiy += diry
- target_pos = Vector2(posix * cell_size + cell_half_size, posiy * cell_size + cell_half_size)
- tween.move_char(self, target_pos)
- is_moving = true
- tween.start()
- func _on_tween_completed(o, k):
- is_moving = false
- func turn(dir:int):
- dirx = 0
- diry = 0
- if dir == DIR_UP:
- diry -= 1
- elif dir == DIR_RIGHT:
- dirx += 1
- elif dir == DIR_DOWN:
- diry += 1
- else:
- dirx -= 1
|