working force push with raycast

This commit is contained in:
OddlyTimbot 2025-01-13 19:29:58 -05:00
parent d62e6ac024
commit 7bac7283dd
5 changed files with 92 additions and 21 deletions

View File

@ -20,3 +20,26 @@ config/icon="res://icon.svg"
folder_colors={
"res://scripts/": "red"
}
[input]
right={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
]
}
left={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null)
]
}
jump={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null)
]
}
shove={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null)
]
}

View File

@ -1,9 +1,9 @@
[gd_scene load_steps=9 format=3 uid="uid://r8jj43l4t6is"]
[gd_scene load_steps=8 format=3 uid="uid://r8jj43l4t6is"]
[ext_resource type="Script" path="res://scripts/gamecontroller.gd" id="1_8i1rq"]
[ext_resource type="PackedScene" uid="uid://di3tq4f0xmdqc" path="res://scenes/crate.tscn" id="1_tfj4n"]
[ext_resource type="Script" path="res://scripts/charactercontroller.gd" id="2_e3e8k"]
[ext_resource type="Script" path="res://scripts/trigger.gd" id="3_hm0br"]
[ext_resource type="PackedScene" uid="uid://dcbk086hhrn7y" path="res://scenes/player.tscn" id="3_oy8j2"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_jk5qv"]
size = Vector2(66, 20)
@ -11,8 +11,6 @@ size = Vector2(66, 20)
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_acecj"]
distance = -430.0
[sub_resource type="CircleShape2D" id="CircleShape2D_qrixx"]
[sub_resource type="CircleShape2D" id="CircleShape2D_8j7te"]
radius = 29.0
@ -20,14 +18,14 @@ radius = 29.0
script = ExtResource("1_8i1rq")
[node name="StaticBody2D" type="StaticBody2D" parent="."]
position = Vector2(542, 371)
position = Vector2(547, 333)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
shape = SubResource("RectangleShape2D_jk5qv")
[node name="RigidBody2D" parent="." instance=ExtResource("1_tfj4n")]
position = Vector2(526, 297)
position = Vector2(291, 405)
rotation = 2.0228
[node name="RigidBody2D2" parent="." instance=ExtResource("1_tfj4n")]
@ -46,15 +44,8 @@ rotation = 0.0531393
[node name="CollisionShape2D" type="CollisionShape2D" parent="floor"]
shape = SubResource("WorldBoundaryShape2D_acecj")
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
position = Vector2(441, 314)
script = ExtResource("2_e3e8k")
PUSH_FORCE = 70
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
shape = SubResource("CircleShape2D_qrixx")
debug_color = Color(0.345561, 0.615334, 0.230675, 0.42)
[node name="CharacterBody2D" parent="." instance=ExtResource("3_oy8j2")]
position = Vector2(236, 403)
[node name="Area2D" type="Area2D" parent="."]
position = Vector2(597, 419)

View File

@ -0,0 +1,19 @@
[gd_scene load_steps=3 format=3 uid="uid://dcbk086hhrn7y"]
[ext_resource type="Script" path="res://scripts/charactercontroller.gd" id="1_6alft"]
[sub_resource type="CircleShape2D" id="CircleShape2D_qrixx"]
[node name="CharacterBody2D" type="CharacterBody2D"]
script = ExtResource("1_6alft")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_qrixx")
debug_color = Color(0.345561, 0.615334, 0.230675, 0.42)
[node name="RightRay" type="RayCast2D" parent="."]
target_position = Vector2(18, 0)
[node name="LeftRay" type="RayCast2D" parent="."]
target_position = Vector2(-19, 0)

View File

@ -3,7 +3,16 @@ extends CharacterBody2D
const SPEED = 500.0
const JUMP_VELOCITY = -600.0
@export var PUSH_FORCE = 90
@export var BUMP_FORCE = 90
# Shove Attack Variables
@onready var right_ray: RayCast2D = $RightRay
@onready var left_ray: RayCast2D = $LeftRay
var faceLeft = false
var pushTarget
var pushRightEnabled = false
var pushLeftEnabled = false
@export var PUSH_FORCE = 700
func _physics_process(delta: float) -> void:
# Add the gravity.
@ -11,19 +20,48 @@ func _physics_process(delta: float) -> void:
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Shove Attack
if Input.is_action_just_pressed("shove"):
if pushRightEnabled && faceLeft == false:
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2 )
if pushLeftEnabled && faceLeft == true:
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2 )
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("ui_left", "ui_right")
var direction := Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
if direction <0:
faceLeft = true
if direction >0:
faceLeft = false
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
if right_ray.is_colliding():
if not faceLeft:
var collider = right_ray.get_collider()
if collider is RigidBody2D:
pushTarget = collider
pushRightEnabled = true
else:
pushRightEnabled = false
if left_ray.is_colliding():
if faceLeft:
var collider = left_ray.get_collider()
if collider is RigidBody2D:
pushTarget = collider
pushLeftEnabled = true
else:
pushLeftEnabled = false
for i in get_slide_collision_count():
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal() * PUSH_FORCE)
c.get_collider().apply_central_impulse(-c.get_normal() * BUMP_FORCE)

View File

@ -10,10 +10,10 @@ func _ready() -> void:
timer.wait_time = 1
timer.one_shot = false
timer.connect("timeout", secondCounter)
timer.start()
# timer.start()
func secondCounter():
print("one second")
countdown -=1
if countdown <=0:
print("YOU LOSE")