bullets, bulletfactory, shove attack, scenemanager
This commit is contained in:
parent
e771dd5a61
commit
0afb528430
@ -20,3 +20,16 @@ config/icon="res://icon.svg"
|
||||
folder_colors={
|
||||
"res://scenes/": "orange"
|
||||
}
|
||||
|
||||
[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":88,"key_label":0,"unicode":120,"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":90,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
14
scenes/bullet.tscn
Normal file
14
scenes/bullet.tscn
Normal file
@ -0,0 +1,14 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://cair7oymuohyq"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://b8fowda770nwo" path="res://scripts/bullet.gd" id="1_mkf8s"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_y25gk"]
|
||||
size = Vector2(10, 4)
|
||||
|
||||
[node name="Bullet" type="Area2D"]
|
||||
script = ExtResource("1_mkf8s")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("RectangleShape2D_y25gk")
|
||||
debug_color = Color(0.680659, 0.501456, 0.117679, 0.42)
|
@ -1,29 +0,0 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") 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")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
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() * 100 )
|
@ -1,36 +1,35 @@
|
||||
[gd_scene load_steps=7 format=3 uid="uid://dc4obqdsdq81h"]
|
||||
[gd_scene load_steps=8 format=3 uid="uid://dc4obqdsdq81h"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dflv3rb4v8c0r" path="res://scripts/gamecontroller.gd" id="1_lnu2h"]
|
||||
[ext_resource type="Script" uid="uid://dmyw2bn6gnbbv" path="res://scenes/character_body_2d.gd" id="1_uwrxv"]
|
||||
[ext_resource type="PackedScene" uid="uid://drodkouhgldnf" path="res://scenes/player.tscn" id="2_lbhrr"]
|
||||
[ext_resource type="Script" uid="uid://cf8f01b63m2hq" path="res://scripts/scene_manager.gd" id="2_p57ef"]
|
||||
[ext_resource type="PackedScene" uid="uid://dm0s1wei11x43" path="res://scenes/crate.tscn" id="3_lbhrr"]
|
||||
[ext_resource type="PackedScene" uid="uid://bu03yhhfncspe" path="res://scenes/trigger.tscn" id="4_iywne"]
|
||||
[ext_resource type="PackedScene" uid="uid://cair7oymuohyq" path="res://scenes/bullet.tscn" id="5_iywne"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"]
|
||||
size = Vector2(60, 20)
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"]
|
||||
|
||||
[node name="Game" type="Node2D"]
|
||||
script = ExtResource("1_lnu2h")
|
||||
|
||||
[node name="SceneManager" type="Node2D" parent="."]
|
||||
unique_name_in_owner = true
|
||||
script = ExtResource("2_p57ef")
|
||||
|
||||
[node name="StaticBody2D" type="StaticBody2D" parent="."]
|
||||
position = Vector2(570, 369)
|
||||
scale = Vector2(4, 1)
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
|
||||
shape = SubResource("RectangleShape2D_8cj0n")
|
||||
|
||||
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
|
||||
position = Vector2(568, 344)
|
||||
script = ExtResource("1_uwrxv")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
|
||||
shape = SubResource("CircleShape2D_8cj0n")
|
||||
debug_color = Color(0.365399, 0.613218, 0.181601, 0.42)
|
||||
[node name="CharacterBody2D" parent="." instance=ExtResource("2_lbhrr")]
|
||||
position = Vector2(556, 348)
|
||||
|
||||
[node name="RigidBody2D3" parent="." instance=ExtResource("3_lbhrr")]
|
||||
position = Vector2(551, 270)
|
||||
position = Vector2(510, 326)
|
||||
|
||||
[node name="Area2D" parent="." instance=ExtResource("4_iywne")]
|
||||
position = Vector2(519, 453)
|
||||
@ -40,10 +39,13 @@ position = Vector2(653, 461)
|
||||
effect = "powerup"
|
||||
|
||||
[node name="RigidBody2D4" parent="." instance=ExtResource("3_lbhrr")]
|
||||
position = Vector2(570, 193)
|
||||
position = Vector2(468, 321)
|
||||
|
||||
[node name="RigidBody2D5" parent="." instance=ExtResource("3_lbhrr")]
|
||||
position = Vector2(589, 256)
|
||||
position = Vector2(590, 342)
|
||||
|
||||
[node name="Bullet" parent="." instance=ExtResource("5_iywne")]
|
||||
position = Vector2(490, 302)
|
||||
|
||||
[connection signal="areatrigger" from="Area2D" to="." method="_on_areatrigger"]
|
||||
[connection signal="areatrigger" from="Area2D2" to="." method="_on_areatrigger"]
|
||||
|
25
scenes/player.tscn
Normal file
25
scenes/player.tscn
Normal file
@ -0,0 +1,25 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://drodkouhgldnf"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dmyw2bn6gnbbv" path="res://scripts/character_body_2d.gd" id="1_3vyb7"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"]
|
||||
|
||||
[node name="CharacterBody2D" type="CharacterBody2D"]
|
||||
script = ExtResource("1_3vyb7")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("CircleShape2D_8cj0n")
|
||||
debug_color = Color(0.365399, 0.613218, 0.181601, 0.42)
|
||||
|
||||
[node name="RightCast" type="RayCast2D" parent="."]
|
||||
target_position = Vector2(16, 0)
|
||||
|
||||
[node name="LeftCast" type="RayCast2D" parent="."]
|
||||
target_position = Vector2(-16, 0)
|
||||
|
||||
[node name="RightSpawn" type="Node2D" parent="."]
|
||||
position = Vector2(14, -5)
|
||||
|
||||
[node name="LeftSpawn" type="Node2D" parent="."]
|
||||
position = Vector2(-14, -5)
|
13
scripts/bullet.gd
Normal file
13
scripts/bullet.gd
Normal file
@ -0,0 +1,13 @@
|
||||
class_name Bullet extends Area2D
|
||||
var speed = -700
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
func setSpeed(value):
|
||||
speed = value
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _physics_process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
1
scripts/bullet.gd.uid
Normal file
1
scripts/bullet.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://b8fowda770nwo
|
75
scripts/character_body_2d.gd
Normal file
75
scripts/character_body_2d.gd
Normal file
@ -0,0 +1,75 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
@onready var right_cast: RayCast2D = $RightCast
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_spawn: Node2D = $RightSpawn
|
||||
@onready var left_spawn: Node2D = $LeftSpawn
|
||||
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
|
||||
var pushTarget
|
||||
var pushEnabled = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("Shoot a bullet")
|
||||
if facing == FaceDirection.RIGHT:
|
||||
%SceneManager.makeBullet(right_spawn.global_transform, 700)
|
||||
if facing == FaceDirection.LEFT:
|
||||
%SceneManager.makeBullet(left_spawn.global_transform, -700)
|
||||
|
||||
if Input.is_action_just_pressed("shove") && pushEnabled:
|
||||
print("shove pressed")
|
||||
if facing == FaceDirection.RIGHT:
|
||||
pushTarget.apply_central_impulse(Vector2(1,0)*700)
|
||||
pushEnabled = false
|
||||
if facing == FaceDirection.LEFT:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0)*700)
|
||||
pushEnabled = false
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") 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")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
if direction <0:
|
||||
facing = FaceDirection.LEFT
|
||||
if direction >0:
|
||||
facing = FaceDirection.RIGHT
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
for inc in get_slide_collision_count():
|
||||
var col = get_slide_collision(inc)
|
||||
if col.get_collider() is RigidBody2D:
|
||||
col.get_collider().apply_central_impulse(-col.get_normal() * 100 )
|
||||
|
||||
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
|
||||
|
||||
#get the thing I am colliding with
|
||||
var collider = right_cast.get_collider()
|
||||
if collider is Node && collider is RigidBody2D:
|
||||
print("I can shove this (crate)")
|
||||
pushTarget = collider
|
||||
pushEnabled = true
|
||||
|
||||
if left_cast.is_colliding() && facing == FaceDirection.LEFT:
|
||||
|
||||
var collider = left_cast.get_collider()
|
||||
if collider is Node && collider is RigidBody2D:
|
||||
print("I can shove this to the left")
|
||||
pushTarget = collider
|
||||
pushEnabled = true
|
||||
if not right_cast.is_colliding() && not left_cast.is_colliding():
|
||||
pushEnabled = false
|
29
scripts/scene_manager.gd
Normal file
29
scripts/scene_manager.gd
Normal file
@ -0,0 +1,29 @@
|
||||
extends Node2D
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
var bulletArray =[]
|
||||
var totalAllowedBullets = 7
|
||||
|
||||
#makes bullets
|
||||
func bulletFactory():
|
||||
var mybullet
|
||||
if bulletArray.size() < totalAllowedBullets:
|
||||
#make a new bullet
|
||||
mybullet = bullet.instantiate()
|
||||
owner.add_child(mybullet)
|
||||
else:
|
||||
#recycle bullet
|
||||
mybullet = bulletArray.pop_back()
|
||||
|
||||
bulletArray.push_front(mybullet)
|
||||
return mybullet
|
||||
|
||||
#order desk
|
||||
func makeBullet(position, speed):
|
||||
print("Scenemanager orders a bullet")
|
||||
#ask the factory for a bullet
|
||||
var mybullet = bulletFactory()
|
||||
#set the speed for the bullet
|
||||
mybullet.setSpeed(speed)
|
||||
#position the bullet
|
||||
mybullet.transform = position
|
1
scripts/scene_manager.gd.uid
Normal file
1
scripts/scene_manager.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://cf8f01b63m2hq
|
Loading…
Reference in New Issue
Block a user