bullets and force push working

This commit is contained in:
winniewk 2024-09-09 21:02:15 -04:00
parent 8428eebd76
commit 0c164bff8a
7 changed files with 148 additions and 25 deletions

View File

@ -0,0 +1,16 @@
[gd_scene load_steps=3 format=3 uid="uid://djsdyfqvl85k1"]
[ext_resource type="Script" path="res://Sripts/Bullet.gd" id="1_iqvku"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_0nhk6"]
size = Vector2(20, 11)
[node name="Area2D" type="Area2D"]
script = ExtResource("1_iqvku")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_0nhk6")
debug_color = Color(0.345732, 0.414118, 0.250065, 0.42)
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

View File

@ -5,8 +5,6 @@
[sub_resource type="RectangleShape2D" id="RectangleShape2D_amdo7"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_amdo7"]
[node name="RigidBody2D" type="RigidBody2D"] [node name="RigidBody2D" type="RigidBody2D"]
position = Vector2(555, 296)
rotation = 0.785817
script = ExtResource("1_4l5o6") script = ExtResource("1_4l5o6")
metadata/_edit_group_ = true metadata/_edit_group_ = true

View File

@ -1,15 +1,13 @@
[gd_scene load_steps=7 format=3 uid="uid://bnrrbkgwh35jy"] [gd_scene load_steps=6 format=3 uid="uid://bnrrbkgwh35jy"]
[ext_resource type="PackedScene" uid="uid://bq1tvv23y61by" path="res://Scenes/Crate.tscn" id="1_8ldta"] [ext_resource type="PackedScene" uid="uid://bq1tvv23y61by" path="res://Scenes/Crate.tscn" id="1_8ldta"]
[ext_resource type="Script" path="res://Sripts/player.gd" id="2_2yqxe"] [ext_resource type="PackedScene" uid="uid://crvsom7i1waff" path="res://Scenes/Player.tscn" id="2_vxspj"]
[ext_resource type="PackedScene" uid="uid://cb1km20mpc3ag" path="res://Scenes/Trigger.tscn" id="3_sr8fi"] [ext_resource type="PackedScene" uid="uid://cb1km20mpc3ag" path="res://Scenes/Trigger.tscn" id="3_sr8fi"]
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_flxcs"] [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_flxcs"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_6fyfv"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_6fyfv"]
[sub_resource type="CircleShape2D" id="CircleShape2D_t5iiu"]
[node name="World" type="Node2D"] [node name="World" type="Node2D"]
[node name="BottomWorld" type="StaticBody2D" parent="."] [node name="BottomWorld" type="StaticBody2D" parent="."]
@ -40,23 +38,14 @@ metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="platform3"] [node name="CollisionShape2D" type="CollisionShape2D" parent="platform3"]
shape = SubResource("RectangleShape2D_6fyfv") shape = SubResource("RectangleShape2D_6fyfv")
[node name="RigidBody2D" parent="." instance=ExtResource("1_8ldta")] [node name="CharacterBody2D" parent="." instance=ExtResource("2_vxspj")]
position = Vector2(554, 291) position = Vector2(522, 600)
[node name="RigidBody2D2" parent="." instance=ExtResource("1_8ldta")]
position = Vector2(381, 149)
[node name="RigidBody2D3" parent="." instance=ExtResource("1_8ldta")]
position = Vector2(380, 182)
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
position = Vector2(381, 396)
script = ExtResource("2_2yqxe")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
shape = SubResource("CircleShape2D_t5iiu")
debug_color = Color(0, 1, 0, 0.662745)
[node name="Area2D" parent="." instance=ExtResource("3_sr8fi")] [node name="Area2D" parent="." instance=ExtResource("3_sr8fi")]
position = Vector2(683, 645) position = Vector2(683, 645)
[node name="RigidBody2D" parent="." groups=["Pushable"] instance=ExtResource("1_8ldta")]
position = Vector2(298, 550)
[node name="RigidBody2D2" parent="." groups=["Pushable"] instance=ExtResource("1_8ldta")]
position = Vector2(297, 527)

View File

@ -0,0 +1,25 @@
[gd_scene load_steps=3 format=3 uid="uid://crvsom7i1waff"]
[ext_resource type="Script" path="res://Sripts/player.gd" id="1_jdiv6"]
[sub_resource type="CircleShape2D" id="CircleShape2D_t5iiu"]
[node name="CharacterBody2D" type="CharacterBody2D"]
script = ExtResource("1_jdiv6")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_t5iiu")
debug_color = Color(0, 1, 0, 0.662745)
[node name="RightRay" type="RayCast2D" parent="."]
target_position = Vector2(25, 0)
[node name="LeftRay" type="RayCast2D" parent="."]
target_position = Vector2(-25, 0)
[node name="MarkerRight" type="Node2D" parent="."]
position = Vector2(14, -2)
[node name="MarkerLeft" type="Node2D" parent="."]
position = Vector2(-14, -2)

View File

@ -0,0 +1,13 @@
extends Area2D
var speed := 700
signal hit(bullet, body)
func setSpeed(speedVal):
speed = speedVal
func _physics_process(delta: float) -> void:
position += transform.x * speed * delta
func _on_body_entered(body: Node2D) -> void:
print("Hit")

View File

@ -3,6 +3,20 @@ extends CharacterBody2D
const SPEED = 300.0 const SPEED = 300.0
const JUMP_VELOCITY = -400.0 const JUMP_VELOCITY = -400.0
const PUSH_FORCE = 700
var faceLeft = false
# can I push right or left
var pushLeftEnabled = false
var pushRightEnabled = false
@onready var right_ray: RayCast2D = $RightRay
@onready var left_ray: RayCast2D = $LeftRay
@onready var marker_right: Node2D = $MarkerRight
@onready var marker_left: Node2D = $MarkerLeft
var pushTarget
var bullet = preload("res://Scenes/Bullet.tscn")
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
@ -13,10 +27,43 @@ func _physics_process(delta: float) -> void:
# Handle jump. # Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor(): if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY velocity.y = JUMP_VELOCITY
if Input.is_action_just_pressed("Shove") && pushLeftEnabled && faceLeft:
print("Shove left")
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 1.5)
pushLeftEnabled = false
if Input.is_action_just_pressed("Shove") && pushRightEnabled && not faceLeft:
print("Shove right")
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 1.5)
pushRightEnabled = false
if Input.is_action_just_pressed("Shoot"):
print ("Shooting")
var mybullet = bullet.instantiate()
if faceLeft:
print("shoot left")
mybullet.setSpeed(-700)
#position bullet
mybullet.transform = marker_left.global_transform
else:
print("shoot right")
#position bullet
mybullet.transform = marker_right.global_transform
#add bullet
owner.add_child(mybullet)
# Get the input direction and handle the movement/deceleration. # Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions. # 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("ui_left", "ui_right")
if direction <0:
faceLeft = true
if direction >0:
faceLeft = false
if direction: if direction:
velocity.x = direction * SPEED velocity.x = direction * SPEED
else: else:
@ -26,5 +73,27 @@ func _physics_process(delta: float) -> void:
for i in get_slide_collision_count(): for i in get_slide_collision_count():
var c = get_slide_collision(i) var c = get_slide_collision(i)
if c. get_collider() is RigidBody2D: if c. get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal()* 200) c.get_collider().apply_central_impulse(-c.get_normal()* 50)
if left_ray.is_colliding():
print("left ray collistion")
var collider = left_ray.get_collider()
if collider is Node:
if collider.is_in_group("Pushable"):
pushLeftEnabled = true
pushTarget = collider
else:
# do somthing else
pushLeftEnabled = false
if right_ray.is_colliding():
print("right ray collistion")
var collider = right_ray.get_collider()
if collider is Node:
if collider.is_in_group("Pushable"):
pushRightEnabled = true
pushTarget = collider
else:
pushRightEnabled = false

View File

@ -14,3 +14,16 @@ config/name="August26GodotcompKieran"
run/main_scene="res://Scenes/Game.tscn" run/main_scene="res://Scenes/Game.tscn"
config/features=PackedStringArray("4.3", "Forward Plus") config/features=PackedStringArray("4.3", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[input]
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":90,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null)
]
}
Shoot={
"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)
]
}