From 83faf15858bdac97c440e01f168b6b56ede46ca3 Mon Sep 17 00:00:00 2001 From: chimchooree Date: Fri, 8 Sep 2023 10:27:52 -0500 Subject: [PATCH] wasd movement --- Main.tscn | 8 ++++- entity/AI/Player.gd | 66 +++++++++++++++++++++++++++++++++++++++ entity/Girl.gd | 11 ++++++- entity/KinematicBody2D.gd | 15 +++++---- events/moved/Char.gd | 12 +++---- events/moved/Moved.gd | 33 ++++++++++---------- 6 files changed, 113 insertions(+), 32 deletions(-) create mode 100644 entity/AI/Player.gd diff --git a/Main.tscn b/Main.tscn index 683622d..0ef1d1c 100644 --- a/Main.tscn +++ b/Main.tscn @@ -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/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://entity/KinematicBody2D.gd" type="Script" id=5] [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] flags = 4 @@ -161,6 +162,11 @@ animation = "WalkDown" position = Vector2( 1, -2 ) 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"] __meta__ = { "_edit_lock_": true diff --git a/entity/AI/Player.gd b/entity/AI/Player.gd new file mode 100644 index 0000000..0a9dcbd --- /dev/null +++ b/entity/AI/Player.gd @@ -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() diff --git a/entity/Girl.gd b/entity/Girl.gd index 90d574e..b31be79 100644 --- a/entity/Girl.gd +++ b/entity/Girl.gd @@ -1,3 +1,12 @@ 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) diff --git a/entity/KinematicBody2D.gd b/entity/KinematicBody2D.gd index 65064a8..b8f9f21 100644 --- a/entity/KinematicBody2D.gd +++ b/entity/KinematicBody2D.gd @@ -27,6 +27,8 @@ func set_external_velocity(new_velocity): external_velocity = new_velocity func get_external_velocity(): return external_velocity +func get_gpos(): + return global_position func set_internal_velocity(new_velocity): internal_velocity = new_velocity func get_internal_velocity(): @@ -60,15 +62,12 @@ func get_user(): func _process(delta): var v = get_velocity() * get_speed() * delta * KnowledgeBase.logic_time - print(move_and_collide(v)) + move_and_collide(v) if v != Vector2(0,0): MessageBus.publish("moved", get_user()) -#func _on_input_event(viewport, event, shape_idx): -# get_physics_body_node()._on_input_event(viewport, event, shape_idx) +#func _setup(): + #set_process(true) -func _setup(): - set_process(true) - -func _ready(): - set_process(false) +#func _ready(): +# set_process(false) diff --git a/events/moved/Char.gd b/events/moved/Char.gd index adcd695..e11cb74 100644 --- a/events/moved/Char.gd +++ b/events/moved/Char.gd @@ -2,10 +2,10 @@ extends "res://events/moved/Moved.gd" # Event Handler for Character Moving -func _arrived(character): # at next - var next = character.get_next() - return next && character.reached_next() +#func _arrived(character): # at next + #var next = character.get_next() + #return next && character.reached_next() -func _reached(character): # at current dot - var cd = character.get_current_dot() - return cd && character.reached_current_dot() +#func _reached(character): # at current dot +# var cd = character.get_current_dot() +# return cd && character.reached_current_dot() diff --git a/events/moved/Moved.gd b/events/moved/Moved.gd index ecad50c..a8bdd97 100644 --- a/events/moved/Moved.gd +++ b/events/moved/Moved.gd @@ -27,25 +27,26 @@ func track_pos(new_pos): # Handle func handle(character): - var current_pos = character.get_gpos() - track_pos(current_pos) - if _arrived(character): - character.think("Moved/Arrived") - character.emit_signal("arrived") - return - elif _reached(character): - character.think("Moved/Reached Dot") - character.emit_signal("reached") - return - elif compare_pos(): - character.think("Moved/Not Moving") - character.emit_signal("not_moving") - return + return +# var current_pos = character.get_gpos() +# track_pos(current_pos) +# if _arrived(character): +# character.think("Moved/Arrived") +# character.emit_signal("arrived") +# return +# elif _reached(character): +# character.think("Moved/Reached Dot") +# character.emit_signal("reached") +# return +# elif compare_pos(): +# character.think("Moved/Not Moving") +# character.emit_signal("not_moving") +# return #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 posjk = character.get_velocity() # character.think("Moved/Wrong Directiaon") # character.emit_signal("wrong_direction") - else: - return + #else: +# return