Week 2: bullet factory, raycast, shove attack, unique names
This commit is contained in:
parent
435a143e59
commit
e3c11f0b4c
16
bullet.gd
Normal file
16
bullet.gd
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
class_name Bullet extends Area2D
|
||||||
|
var speed = 700
|
||||||
|
|
||||||
|
signal bulletHit(body, bullet)
|
||||||
|
|
||||||
|
func _physics_process(delta: float) -> void:
|
||||||
|
position += transform.x * speed * delta
|
||||||
|
|
||||||
|
func setSpeed(value):
|
||||||
|
speed = value
|
||||||
|
|
||||||
|
|
||||||
|
func _on_body_entered(body: Node2D) -> void:
|
||||||
|
if not body is Player:
|
||||||
|
print("bullet hit!")
|
||||||
|
bulletHit.emit(body, self)
|
1
bullet.gd.uid
Normal file
1
bullet.gd.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://wmy17yj81te8
|
@ -33,3 +33,11 @@ func _on_trigger_trigger_fired(effect: Variant, body: Variant) -> void:
|
|||||||
if totalCrates == 0:
|
if totalCrates == 0:
|
||||||
print("YOU WIN!")
|
print("YOU WIN!")
|
||||||
get_tree().reload_current_scene()
|
get_tree().reload_current_scene()
|
||||||
|
|
||||||
|
func numberOfCrates(value:int)->void:
|
||||||
|
totalCrates = value
|
||||||
|
|
||||||
|
func bulletDamage(body, bullet):
|
||||||
|
print("Game controller knows about bullet")
|
||||||
|
body.queue_free()
|
||||||
|
#bullet.queue_free()
|
||||||
|
@ -20,3 +20,16 @@ config/icon="res://icon.svg"
|
|||||||
folder_colors={
|
folder_colors={
|
||||||
"res://scripts/": "red"
|
"res://scripts/": "red"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[input]
|
||||||
|
|
||||||
|
shove={
|
||||||
|
"deadzone": 0.2,
|
||||||
|
"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.2,
|
||||||
|
"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)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
15
scenes/bullet.tscn
Normal file
15
scenes/bullet.tscn
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
[gd_scene load_steps=3 format=3 uid="uid://cy02yf4v8q5v6"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" uid="uid://wmy17yj81te8" path="res://bullet.gd" id="1_mkf8s"]
|
||||||
|
|
||||||
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_y25gk"]
|
||||||
|
size = Vector2(34, 8)
|
||||||
|
|
||||||
|
[node name="Area2D" type="Area2D"]
|
||||||
|
script = ExtResource("1_mkf8s")
|
||||||
|
|
||||||
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||||
|
shape = SubResource("RectangleShape2D_y25gk")
|
||||||
|
debug_color = Color(0.931576, 2.76271e-05, 0.652218, 0.42)
|
||||||
|
|
||||||
|
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
|
15
scenes/crate.tscn
Normal file
15
scenes/crate.tscn
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
[gd_scene load_steps=3 format=3 uid="uid://ch3ncwkrhv38n"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" uid="uid://d2pjirchmmx8w" path="res://scripts/crate.gd" id="1_b66cd"]
|
||||||
|
|
||||||
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_uwrxv"]
|
||||||
|
size = Vector2(144, 44)
|
||||||
|
|
||||||
|
[node name="Crate" type="RigidBody2D" groups=["pushables"]]
|
||||||
|
rotation = 1.08962
|
||||||
|
script = ExtResource("1_b66cd")
|
||||||
|
metadata/_edit_group_ = true
|
||||||
|
|
||||||
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||||
|
shape = SubResource("RectangleShape2D_uwrxv")
|
||||||
|
debug_color = Color(0.82094, 0.38733, 0.296015, 0.42)
|
@ -1,31 +1,26 @@
|
|||||||
[gd_scene load_steps=8 format=3 uid="uid://bvs868ncmeoj5"]
|
[gd_scene load_steps=9 format=3 uid="uid://bvs868ncmeoj5"]
|
||||||
|
|
||||||
[ext_resource type="Script" uid="uid://dtod1rsvbxyti" path="res://gamecontroller.gd" id="1_lnu2h"]
|
[ext_resource type="Script" uid="uid://dtod1rsvbxyti" path="res://gamecontroller.gd" id="1_lnu2h"]
|
||||||
[ext_resource type="Script" uid="uid://c3vqmu6nc4gb0" path="res://scripts/player.gd" id="1_uwrxv"]
|
[ext_resource type="Script" uid="uid://bijvpac45v6s1" path="res://scripts/scene_manager.gd" id="2_lbhrr"]
|
||||||
|
[ext_resource type="PackedScene" uid="uid://c5a5u2c250g6" path="res://scenes/player.tscn" id="2_lnu2h"]
|
||||||
[ext_resource type="PackedScene" uid="uid://dgbqoys4mm8j" path="res://scenes/trigger.tscn" id="3_lnu2h"]
|
[ext_resource type="PackedScene" uid="uid://dgbqoys4mm8j" path="res://scenes/trigger.tscn" id="3_lnu2h"]
|
||||||
|
[ext_resource type="PackedScene" uid="uid://ch3ncwkrhv38n" path="res://scenes/crate.tscn" id="4_iywne"]
|
||||||
[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"]
|
[ext_resource type="PackedScene" uid="uid://cy02yf4v8q5v6" path="res://scenes/bullet.tscn" id="6_p57ef"]
|
||||||
radius = 21.2132
|
|
||||||
|
|
||||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"]
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"]
|
||||||
size = Vector2(160, 48)
|
size = Vector2(160, 48)
|
||||||
|
|
||||||
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_uwrxv"]
|
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_uwrxv"]
|
||||||
|
|
||||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_uwrxv"]
|
|
||||||
size = Vector2(144, 44)
|
|
||||||
|
|
||||||
[node name="Game" type="Node2D"]
|
[node name="Game" type="Node2D"]
|
||||||
script = ExtResource("1_lnu2h")
|
script = ExtResource("1_lnu2h")
|
||||||
|
|
||||||
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
|
[node name="SceneManager" type="Node" parent="."]
|
||||||
position = Vector2(489, 45)
|
unique_name_in_owner = true
|
||||||
script = ExtResource("1_uwrxv")
|
script = ExtResource("2_lbhrr")
|
||||||
metadata/_edit_group_ = true
|
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
|
[node name="CharacterBody2D" parent="." instance=ExtResource("2_lnu2h")]
|
||||||
shape = SubResource("CircleShape2D_8cj0n")
|
position = Vector2(489, 45)
|
||||||
debug_color = Color(0.332985, 0.606108, 0.412422, 0.42)
|
|
||||||
|
|
||||||
[node name="Level" type="Node2D" parent="."]
|
[node name="Level" type="Node2D" parent="."]
|
||||||
|
|
||||||
@ -52,44 +47,33 @@ shape = SubResource("WorldBoundaryShape2D_uwrxv")
|
|||||||
|
|
||||||
[node name="Crates" type="Node2D" parent="."]
|
[node name="Crates" type="Node2D" parent="."]
|
||||||
|
|
||||||
[node name="Crate 0" type="RigidBody2D" parent="Crates"]
|
[node name="Crate 0" parent="Crates" instance=ExtResource("4_iywne")]
|
||||||
position = Vector2(580, 158)
|
position = Vector2(580, 158)
|
||||||
rotation = 1.08962
|
|
||||||
metadata/_edit_group_ = true
|
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Crates/Crate 0"]
|
[node name="Crate 1" parent="Crates" instance=ExtResource("4_iywne")]
|
||||||
shape = SubResource("RectangleShape2D_uwrxv")
|
position = Vector2(256, 226)
|
||||||
debug_color = Color(0.82094, 0.38733, 0.296015, 0.42)
|
rotation = -0.544367
|
||||||
|
|
||||||
[node name="Crate 1" type="RigidBody2D" parent="Crates"]
|
[node name="Crate 2" parent="Crates" instance=ExtResource("4_iywne")]
|
||||||
position = Vector2(758, 131)
|
position = Vector2(792, 60)
|
||||||
rotation = 2.21149
|
rotation = -0.536401
|
||||||
metadata/_edit_group_ = true
|
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Crates/Crate 1"]
|
[node name="Triggers" type="Node2D" parent="."]
|
||||||
shape = SubResource("RectangleShape2D_uwrxv")
|
|
||||||
debug_color = Color(0.82094, 0.38733, 0.296015, 0.42)
|
|
||||||
|
|
||||||
[node name="Crate 2" type="RigidBody2D" parent="Crates"]
|
[node name="Trigger" parent="Triggers" instance=ExtResource("3_lnu2h")]
|
||||||
position = Vector2(390, 129)
|
|
||||||
rotation = 2.24474
|
|
||||||
metadata/_edit_group_ = true
|
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Crates/Crate 2"]
|
|
||||||
shape = SubResource("RectangleShape2D_uwrxv")
|
|
||||||
debug_color = Color(0.82094, 0.38733, 0.296015, 0.42)
|
|
||||||
|
|
||||||
[node name="Trigger" parent="." instance=ExtResource("3_lnu2h")]
|
|
||||||
position = Vector2(254, 406)
|
position = Vector2(254, 406)
|
||||||
|
|
||||||
[node name="Trigger2" parent="." instance=ExtResource("3_lnu2h")]
|
[node name="Trigger2" parent="Triggers" instance=ExtResource("3_lnu2h")]
|
||||||
position = Vector2(860, 415)
|
position = Vector2(860, 415)
|
||||||
effect = "destroy"
|
effect = "destroy"
|
||||||
|
|
||||||
[node name="Trigger3" parent="." instance=ExtResource("3_lnu2h")]
|
[node name="Trigger3" parent="Triggers" instance=ExtResource("3_lnu2h")]
|
||||||
position = Vector2(582, 414)
|
position = Vector2(582, 414)
|
||||||
effect = "elaborate"
|
effect = "elaborate"
|
||||||
|
|
||||||
[connection signal="triggerFired" from="Trigger" to="." method="_on_trigger_trigger_fired"]
|
[node name="Area2D" parent="." instance=ExtResource("6_p57ef")]
|
||||||
[connection signal="triggerFired" from="Trigger2" to="." method="_on_trigger_trigger_fired"]
|
position = Vector2(887.175, 161.841)
|
||||||
[connection signal="triggerFired" from="Trigger3" to="." method="_on_trigger_trigger_fired"]
|
|
||||||
|
[connection signal="triggerFired" from="Triggers/Trigger" to="." method="_on_trigger_trigger_fired"]
|
||||||
|
[connection signal="triggerFired" from="Triggers/Trigger2" to="." method="_on_trigger_trigger_fired"]
|
||||||
|
[connection signal="triggerFired" from="Triggers/Trigger3" to="." method="_on_trigger_trigger_fired"]
|
||||||
|
27
scenes/player.tscn
Normal file
27
scenes/player.tscn
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
[gd_scene load_steps=3 format=3 uid="uid://c5a5u2c250g6"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" uid="uid://c3vqmu6nc4gb0" path="res://scripts/player.gd" id="1_3vyb7"]
|
||||||
|
|
||||||
|
[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"]
|
||||||
|
radius = 21.2132
|
||||||
|
|
||||||
|
[node name="CharacterBody2D" type="CharacterBody2D"]
|
||||||
|
script = ExtResource("1_3vyb7")
|
||||||
|
metadata/_edit_group_ = true
|
||||||
|
|
||||||
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||||
|
position = Vector2(-2, 0)
|
||||||
|
shape = SubResource("CircleShape2D_8cj0n")
|
||||||
|
debug_color = Color(0.368579, 0.61578, 3.85046e-07, 0.42)
|
||||||
|
|
||||||
|
[node name="RightCast" type="RayCast2D" parent="."]
|
||||||
|
target_position = Vector2(25, 0)
|
||||||
|
|
||||||
|
[node name="LeftCast" type="RayCast2D" parent="."]
|
||||||
|
target_position = Vector2(-29, 0)
|
||||||
|
|
||||||
|
[node name="RightSpawn" type="Node2D" parent="."]
|
||||||
|
position = Vector2(22, -9)
|
||||||
|
|
||||||
|
[node name="LeftSpawn" type="Node2D" parent="."]
|
||||||
|
position = Vector2(-30, -10)
|
1
scripts/crate.gd
Normal file
1
scripts/crate.gd
Normal file
@ -0,0 +1 @@
|
|||||||
|
class_name Crate extends RigidBody2D
|
1
scripts/crate.gd.uid
Normal file
1
scripts/crate.gd.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://d2pjirchmmx8w
|
@ -1,30 +1,92 @@
|
|||||||
extends CharacterBody2D
|
class_name Player extends CharacterBody2D
|
||||||
|
|
||||||
|
@onready var right_cast: RayCast2D = $RightCast
|
||||||
|
@onready var left_cast: RayCast2D = $LeftCast
|
||||||
|
@onready var right_spawn: Node2D = $RightSpawn
|
||||||
|
@onready var left_spawn: Node2D = $LeftSpawn
|
||||||
|
|
||||||
const SPEED = 300.0
|
const SPEED = 300.0
|
||||||
const JUMP_VELOCITY = -400.0
|
const JUMP_VELOCITY = -400.0
|
||||||
|
|
||||||
@export var pushpower = 80.0
|
@export var bump_power = 60.0
|
||||||
|
@export var shove_power = 600.0
|
||||||
|
|
||||||
|
enum FaceDirection {LEFT, RIGHT}
|
||||||
|
var facing: FaceDirection = FaceDirection.LEFT
|
||||||
|
|
||||||
|
var pushTarget: RigidBody2D
|
||||||
|
var pushEnable: bool = false
|
||||||
|
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
# Add the gravity.
|
# Add the gravity.
|
||||||
if not is_on_floor():
|
if not is_on_floor():
|
||||||
velocity += get_gravity() * delta
|
velocity += get_gravity() * delta
|
||||||
|
|
||||||
|
# Handle input
|
||||||
|
handle_input()
|
||||||
|
|
||||||
|
# Move and handle player collisions
|
||||||
|
move_and_slide()
|
||||||
|
handle_collisions()
|
||||||
|
|
||||||
|
func handle_input() -> void:
|
||||||
|
if Input.is_action_just_pressed("shoot"):
|
||||||
|
print("Shoot")
|
||||||
|
if facing == FaceDirection.LEFT:
|
||||||
|
%SceneManager.makeBullet(left_spawn.global_transform, -700)
|
||||||
|
if facing == FaceDirection.RIGHT:
|
||||||
|
%SceneManager.makeBullet(right_spawn.global_transform, 700)
|
||||||
|
|
||||||
|
if Input.is_action_just_pressed("shove") and pushEnable:
|
||||||
|
if facing == FaceDirection.LEFT:
|
||||||
|
print("Shove attack LEFT!!!")
|
||||||
|
pushTarget.apply_central_impulse(Vector2(-1,0) * shove_power)
|
||||||
|
pushEnable = false
|
||||||
|
if facing == FaceDirection.RIGHT:
|
||||||
|
print("Shove attack RIGHT!!!")
|
||||||
|
pushTarget.apply_central_impulse(Vector2(1,0) * shove_power)
|
||||||
|
pushEnable = false
|
||||||
|
|
||||||
# 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
|
||||||
|
|
||||||
# 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:
|
if direction:
|
||||||
velocity.x = direction * SPEED
|
velocity.x = direction * SPEED
|
||||||
|
facing = FaceDirection.LEFT if direction < 0 else FaceDirection.RIGHT
|
||||||
else:
|
else:
|
||||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||||
|
|
||||||
move_and_slide()
|
func update_movement() -> void:
|
||||||
|
pass
|
||||||
|
|
||||||
|
func update_states() -> void:
|
||||||
|
pass
|
||||||
|
|
||||||
|
func handle_collisions() -> 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() * pushpower)
|
c.get_collider().apply_central_impulse(-c.get_normal() * bump_power)
|
||||||
|
|
||||||
|
if right_cast.is_colliding() and facing == FaceDirection.RIGHT:
|
||||||
|
print("Right raycast hitting something")
|
||||||
|
var collider = right_cast.get_collider()
|
||||||
|
if collider is Node and collider is RigidBody2D:
|
||||||
|
print("I think it's a crate")
|
||||||
|
pushTarget = collider
|
||||||
|
pushEnable = true
|
||||||
|
|
||||||
|
if left_cast.is_colliding() and facing == FaceDirection.LEFT:
|
||||||
|
print("Left raycast hitting something")
|
||||||
|
var collider = left_cast.get_collider()
|
||||||
|
if collider is Node and collider is RigidBody2D:
|
||||||
|
print("I think it's a crate")
|
||||||
|
pushTarget = collider
|
||||||
|
pushEnable = true
|
||||||
|
|
||||||
|
if not right_cast.is_colliding() and not left_cast.is_colliding():
|
||||||
|
pushEnable = false
|
||||||
|
48
scripts/scene_manager.gd
Normal file
48
scripts/scene_manager.gd
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
extends Node
|
||||||
|
|
||||||
|
@onready var game: GameController = $".."
|
||||||
|
@onready var crates: Node2D = $"../Crates"
|
||||||
|
|
||||||
|
var bullet = preload("res://scenes/bullet.tscn")
|
||||||
|
var bulletArray = []
|
||||||
|
var totalAllowedBullets = 7
|
||||||
|
|
||||||
|
# Called when the node enters the scene tree for the first time.
|
||||||
|
func _ready() -> void:
|
||||||
|
buildLevel()
|
||||||
|
|
||||||
|
|
||||||
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
|
func _process(delta: float) -> void:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
func buildLevel() -> void:
|
||||||
|
# count crates
|
||||||
|
var totalCrates = 0
|
||||||
|
for obj in crates.get_children():
|
||||||
|
if obj is Crate:
|
||||||
|
totalCrates += 1
|
||||||
|
# tell the game controller
|
||||||
|
game.numberOfCrates(totalCrates)
|
||||||
|
|
||||||
|
|
||||||
|
func bulletFactory():
|
||||||
|
var mybullet
|
||||||
|
if bulletArray.size() < totalAllowedBullets:
|
||||||
|
#new bullet
|
||||||
|
mybullet = bullet.instantiate()
|
||||||
|
mybullet.connect("bulletHit", game.bulletDamage)
|
||||||
|
owner.add_child(mybullet)
|
||||||
|
else:
|
||||||
|
#recycled bullet
|
||||||
|
mybullet = bulletArray.pop_back()
|
||||||
|
|
||||||
|
bulletArray.push_front(mybullet)
|
||||||
|
return mybullet
|
||||||
|
|
||||||
|
func makeBullet(position, speed):
|
||||||
|
print("Scene manager make bullet")
|
||||||
|
var mybullet = bulletFactory()
|
||||||
|
mybullet.setSpeed(speed)
|
||||||
|
mybullet.transform = position
|
1
scripts/scene_manager.gd.uid
Normal file
1
scripts/scene_manager.gd.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://bijvpac45v6s1
|
Loading…
Reference in New Issue
Block a user