1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- extends Area2D
- var direction = Vector2()
- const DEG_UP = 0
- const DEG_RIGHT = 90
- const DEG_DOWN = 180
- const DEG_LEFT = 270
- const DEG_LIST = [DEG_UP, DEG_RIGHT, DEG_DOWN, DEG_LEFT]
- const DIR_UP = 0
- const DIR_RIGHT = 1
- const DIR_DOWN = 2
- const DIR_LEFT = 3
- var posix
- var posiy
- var dirx = 0
- var diry = 0
- var dire = DIR_UP
- var grid
- var blocks = []
- var is_blocked:bool = false
- export (NodePath) var rayU
- export (NodePath) var rayD
- export (NodePath) var rayL
- export (NodePath) var rayR
- var raycast
- func _ready():
- grid = get_parent()
-
- posix = int (position.x / 64)
- posiy = int (position.y / 64)
-
- turn(DIR_UP)
- pass
- func _physics_process(delta):
-
- if Input.is_action_pressed("ui_left"):
- dire -= 1
- if dire < 0:
- dire = 3
- update_avatar_dir()
- elif Input.is_action_pressed("ui_right"):
- dire += 1
- if dire > 3:
- dire = 0
- update_avatar_dir()
- if Input.is_action_pressed("ui_up"):
-
- turn(dire)
-
- if !raycast.is_colliding():
- posix += dirx
- posiy += diry
- position = Vector2(posix * 64 + 32, posiy * 64 + 32)
- pass
- func _on_area_entered(a):
- if a.get_parent() != $Position2D:
- print(a)
- blocks.append(a)
- is_blocked = true
- pass
- func _on_area_exited(a):
- blocks.erase(a)
- is_blocked = blocks.size()
- pass
-
- func turn(dir:int):
- dirx = 0
- diry = 0
- if dir == DIR_UP:
- raycast=get_node(rayU)
- diry -= 1
- elif dir == DIR_RIGHT:
- raycast=get_node(rayR)
- dirx += 1
- elif dir == DIR_DOWN:
- raycast=get_node(rayD)
- diry += 1
- else:
- raycast=get_node(rayL)
- dirx -= 1
- pass
- func update_avatar_dir():
- $Sprite.rotation_degrees = DEG_LIST[dire]
|