wasd movement

master
chimchooree 1 year ago
parent 25e72421c3
commit 83faf15858

@ -1,4 +1,4 @@
[gd_scene load_steps=29 format=2] [gd_scene load_steps=30 format=2]
[ext_resource path="res://res/level/neighbor.png" type="Texture" id=1] [ext_resource path="res://res/level/neighbor.png" type="Texture" id=1]
[ext_resource path="res://res/character/girl.png" type="Texture" id=2] [ext_resource path="res://res/character/girl.png" type="Texture" id=2]
@ -6,6 +6,7 @@
[ext_resource path="res://res/character/wolf.png" type="Texture" id=4] [ext_resource path="res://res/character/wolf.png" type="Texture" id=4]
[ext_resource path="res://entity/KinematicBody2D.gd" type="Script" id=5] [ext_resource path="res://entity/KinematicBody2D.gd" type="Script" id=5]
[ext_resource path="res://entity/Girl.gd" type="Script" id=6] [ext_resource path="res://entity/Girl.gd" type="Script" id=6]
[ext_resource path="res://entity/AI/Player.gd" type="Script" id=7]
[sub_resource type="AtlasTexture" id=1] [sub_resource type="AtlasTexture" id=1]
flags = 4 flags = 4
@ -161,6 +162,11 @@ animation = "WalkDown"
position = Vector2( 1, -2 ) position = Vector2( 1, -2 )
shape = SubResource( 22 ) shape = SubResource( 22 )
[node name="Camera2D" type="Camera2D" parent="World/Enter/Girl/KinematicBody2D"]
[node name="AI" type="Node" parent="World/Enter/Girl"]
script = ExtResource( 7 )
[node name="SpawnNPC" type="Position2D" parent="World"] [node name="SpawnNPC" type="Position2D" parent="World"]
__meta__ = { __meta__ = {
"_edit_lock_": true "_edit_lock_": true

@ -0,0 +1,66 @@
extends Node
# Player AI
var ignore_input = false
var velocity_sum = Vector2(0.0,0.0)
func set_ignore_input(new_bool):
ignore_input = new_bool
func get_ignore_input():
return ignore_input
func get_user():
return get_parent()
# Get Player Input
func get_input():
if not ignore_input:
get_movement_input()
# Get Movement Key Input
func get_movement_input():
if Input.is_action_pressed('ui_right'):
movement_key_pressed(1)
if Input.is_action_pressed('ui_left'):
movement_key_pressed(2)
if Input.is_action_pressed('ui_up'):
movement_key_pressed(3)
if Input.is_action_pressed('ui_down'):
movement_key_pressed(4)
if Input.is_action_just_released("ui_right"):
movement_key_released(1)
if Input.is_action_just_released("ui_left"):
movement_key_released(2)
if Input.is_action_just_released("ui_up"):
movement_key_released(3)
if Input.is_action_just_released("ui_down"):
movement_key_released(4)
# WASD Movement
func movement_key_pressed(direction):
get_user().set_internal_velocity(Vector2(0.0,0.0))
if direction == 1: # right
velocity_sum.x = 0
velocity_sum.x += 1
elif direction == 2: # left
velocity_sum.x = 0
velocity_sum.x -= 1
elif direction == 3: # up
velocity_sum.y = 0
velocity_sum.y -= 1
elif direction == 4: # down
velocity_sum.y = 0
velocity_sum.y += 1
# Velocity cannot be faster with more vectors
get_user().set_internal_velocity(velocity_sum)
func movement_key_released(direction):
if direction == 1 or direction == 2: # left/right
velocity_sum.x = 0
elif direction == 3 or direction == 4: # up/down
velocity_sum.y = 0
get_user().set_internal_velocity(velocity_sum)
# Physics Process
func _physics_process(delta):
## Keyboard Movement
get_input()

@ -1,3 +1,12 @@
extends Node extends Node
# Variables ## Nodes
func get_body_node():
return $KinematicBody2D
## Parameters
func set_gpos(new_pos):
return get_body_node().set_gpos(new_pos)
func get_gpos():
return get_body_node().get_gpos()
func set_internal_velocity(new_vel):
get_body_node().set_internal_velocity(new_vel)

@ -27,6 +27,8 @@ func set_external_velocity(new_velocity):
external_velocity = new_velocity external_velocity = new_velocity
func get_external_velocity(): func get_external_velocity():
return external_velocity return external_velocity
func get_gpos():
return global_position
func set_internal_velocity(new_velocity): func set_internal_velocity(new_velocity):
internal_velocity = new_velocity internal_velocity = new_velocity
func get_internal_velocity(): func get_internal_velocity():
@ -60,15 +62,12 @@ func get_user():
func _process(delta): func _process(delta):
var v = get_velocity() * get_speed() * delta * KnowledgeBase.logic_time var v = get_velocity() * get_speed() * delta * KnowledgeBase.logic_time
print(move_and_collide(v)) move_and_collide(v)
if v != Vector2(0,0): if v != Vector2(0,0):
MessageBus.publish("moved", get_user()) MessageBus.publish("moved", get_user())
#func _on_input_event(viewport, event, shape_idx): #func _setup():
# get_physics_body_node()._on_input_event(viewport, event, shape_idx) #set_process(true)
func _setup(): #func _ready():
set_process(true) # set_process(false)
func _ready():
set_process(false)

@ -2,10 +2,10 @@ extends "res://events/moved/Moved.gd"
# Event Handler for Character Moving # Event Handler for Character Moving
func _arrived(character): # at next #func _arrived(character): # at next
var next = character.get_next() #var next = character.get_next()
return next && character.reached_next() #return next && character.reached_next()
func _reached(character): # at current dot #func _reached(character): # at current dot
var cd = character.get_current_dot() # var cd = character.get_current_dot()
return cd && character.reached_current_dot() # return cd && character.reached_current_dot()

@ -27,25 +27,26 @@ func track_pos(new_pos):
# Handle # Handle
func handle(character): func handle(character):
var current_pos = character.get_gpos() return
track_pos(current_pos) # var current_pos = character.get_gpos()
if _arrived(character): # track_pos(current_pos)
character.think("Moved/Arrived") # if _arrived(character):
character.emit_signal("arrived") # character.think("Moved/Arrived")
return # character.emit_signal("arrived")
elif _reached(character): # return
character.think("Moved/Reached Dot") # elif _reached(character):
character.emit_signal("reached") # character.think("Moved/Reached Dot")
return # character.emit_signal("reached")
elif compare_pos(): # return
character.think("Moved/Not Moving") # elif compare_pos():
character.emit_signal("not_moving") # character.think("Moved/Not Moving")
return # character.emit_signal("not_moving")
# return
#elif character.get_internal_velocity() != Vector2(0.0,0.0) and character.get_velocity() != character.get_internal_velocity(): #elif character.get_internal_velocity() != Vector2(0.0,0.0) and character.get_velocity() != character.get_internal_velocity():
# var sdfoia = character.get_internal_velocity() # var sdfoia = character.get_internal_velocity()
# var posjk = character.get_velocity() # var posjk = character.get_velocity()
# character.think("Moved/Wrong Directiaon") # character.think("Moved/Wrong Directiaon")
# character.emit_signal("wrong_direction") # character.emit_signal("wrong_direction")
else: #else:
return # return

Loading…
Cancel
Save