working force push with raycast
This commit is contained in:
		
							parent
							
								
									14f739c428
								
							
						
					
					
						commit
						6f55d9442c
					
				@ -12,5 +12,28 @@ config_version=5
 | 
			
		||||
 | 
			
		||||
config/name="January Game"
 | 
			
		||||
run/main_scene="res://scenes/game.tscn"
 | 
			
		||||
config/features=PackedStringArray("4.2", "Forward Plus")
 | 
			
		||||
config/features=PackedStringArray("4.3", "Forward Plus")
 | 
			
		||||
config/icon="res://icon.svg"
 | 
			
		||||
 | 
			
		||||
[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)
 | 
			
		||||
]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,15 +1,13 @@
 | 
			
		||||
[gd_scene load_steps=9 format=3 uid="uid://cxnmaxykhbgaq"]
 | 
			
		||||
[gd_scene load_steps=8 format=3 uid="uid://cxnmaxykhbgaq"]
 | 
			
		||||
 | 
			
		||||
[ext_resource type="PackedScene" uid="uid://63qb2drh1l33" path="res://scenes/crate.tscn" id="1_0wmww"]
 | 
			
		||||
[ext_resource type="Script" path="res://scripts/gamecontroller.gd" id="1_5dy8j"]
 | 
			
		||||
[ext_resource type="Script" path="res://scripts/player.gd" id="2_aq17o"]
 | 
			
		||||
[ext_resource type="Script" path="res://scripts/Trigger.gd" id="3_4laji"]
 | 
			
		||||
[ext_resource type="PackedScene" uid="uid://c1cuiluq1fcpk" path="res://scenes/player.tscn" id="3_w28al"]
 | 
			
		||||
 | 
			
		||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_f1luk"]
 | 
			
		||||
size = Vector2(172, 20)
 | 
			
		||||
 | 
			
		||||
[sub_resource type="CircleShape2D" id="CircleShape2D_y4y41"]
 | 
			
		||||
 | 
			
		||||
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_k1pyx"]
 | 
			
		||||
 | 
			
		||||
[sub_resource type="CircleShape2D" id="CircleShape2D_wdyhk"]
 | 
			
		||||
@ -19,7 +17,7 @@ radius = 34.1321
 | 
			
		||||
script = ExtResource("1_5dy8j")
 | 
			
		||||
 | 
			
		||||
[node name="StaticBody2D" type="StaticBody2D" parent="."]
 | 
			
		||||
position = Vector2(302, 266)
 | 
			
		||||
position = Vector2(302, 430)
 | 
			
		||||
metadata/_edit_group_ = true
 | 
			
		||||
 | 
			
		||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
 | 
			
		||||
@ -33,14 +31,7 @@ rotation = 0.688747
 | 
			
		||||
position = Vector2(391, 64)
 | 
			
		||||
rotation = 0.358379
 | 
			
		||||
 | 
			
		||||
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
 | 
			
		||||
position = Vector2(286, 181)
 | 
			
		||||
script = ExtResource("2_aq17o")
 | 
			
		||||
metadata/_edit_group_ = true
 | 
			
		||||
 | 
			
		||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
 | 
			
		||||
shape = SubResource("CircleShape2D_y4y41")
 | 
			
		||||
debug_color = Color(0.752941, 0.211765, 0.984314, 0.419608)
 | 
			
		||||
[node name="CharacterBody2D" parent="." instance=ExtResource("3_w28al")]
 | 
			
		||||
 | 
			
		||||
[node name="StaticBody2D2" type="StaticBody2D" parent="."]
 | 
			
		||||
position = Vector2(299, 490)
 | 
			
		||||
@ -50,7 +41,7 @@ metadata/_edit_group_ = true
 | 
			
		||||
shape = SubResource("WorldBoundaryShape2D_k1pyx")
 | 
			
		||||
 | 
			
		||||
[node name="Trigger" type="Area2D" parent="."]
 | 
			
		||||
position = Vector2(439, 460)
 | 
			
		||||
position = Vector2(736, 460)
 | 
			
		||||
script = ExtResource("3_4laji")
 | 
			
		||||
metadata/_edit_group_ = true
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								GodotProject/scenes/player.tscn
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								GodotProject/scenes/player.tscn
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
[gd_scene load_steps=3 format=3 uid="uid://c1cuiluq1fcpk"]
 | 
			
		||||
 | 
			
		||||
[ext_resource type="Script" path="res://scripts/player.gd" id="1_s1kdl"]
 | 
			
		||||
 | 
			
		||||
[sub_resource type="CircleShape2D" id="CircleShape2D_y4y41"]
 | 
			
		||||
 | 
			
		||||
[node name="CharacterBody2D" type="CharacterBody2D"]
 | 
			
		||||
script = ExtResource("1_s1kdl")
 | 
			
		||||
metadata/_edit_group_ = true
 | 
			
		||||
 | 
			
		||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
 | 
			
		||||
shape = SubResource("CircleShape2D_y4y41")
 | 
			
		||||
debug_color = Color(0.752941, 0.211765, 0.984314, 0.419608)
 | 
			
		||||
 | 
			
		||||
[node name="RightRay" type="RayCast2D" parent="."]
 | 
			
		||||
target_position = Vector2(15, 0)
 | 
			
		||||
collide_with_areas = true
 | 
			
		||||
 | 
			
		||||
[node name="LeftRay" type="RayCast2D" parent="."]
 | 
			
		||||
target_position = Vector2(-15, 0)
 | 
			
		||||
@ -8,10 +8,9 @@ func _ready():
 | 
			
		||||
	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")
 | 
			
		||||
 | 
			
		||||
@ -3,31 +3,64 @@ extends CharacterBody2D
 | 
			
		||||
 | 
			
		||||
const SPEED = 300.0
 | 
			
		||||
const JUMP_VELOCITY = -400.0
 | 
			
		||||
@export var PUSH_FORCE = 90
 | 
			
		||||
@export var BUMP_FORCE = 90
 | 
			
		||||
 | 
			
		||||
# Shove attack
 | 
			
		||||
@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
 | 
			
		||||
 | 
			
		||||
# Get the gravity from the project settings to be synced with RigidBody nodes.
 | 
			
		||||
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func _physics_process(delta):
 | 
			
		||||
	# Add the gravity.
 | 
			
		||||
	if not is_on_floor():
 | 
			
		||||
		velocity.y += 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
 | 
			
		||||
		
 | 
			
		||||
	# 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")
 | 
			
		||||
		
 | 
			
		||||
	# 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:
 | 
			
		||||
			pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
 | 
			
		||||
	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)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user