Collisions and cooldowns

This commit is contained in:
MaddoScientisto 2024-08-18 11:33:17 +02:00
commit 6ff1ab76ed
9 changed files with 105 additions and 3848 deletions

View file

@ -17,7 +17,7 @@ size = Vector2(11, 14)
[node name="Barrel" type="Area2D" groups=["Destroyable"]]
collision_layer = 64
collision_mask = 10
collision_mask = 138
script = ExtResource("1_avwdx")
Health = 2.0
ExplosionRadius = 2.0

View file

@ -17,8 +17,9 @@ radius = 4.0
[sub_resource type="CircleShape2D" id="CircleShape2D_v711r"]
radius = 85.0529
[node name="Enemy" type="Area2D"]
[node name="Enemy" type="Area2D" groups=["Destroyable"]]
collision_layer = 16
collision_mask = 9
script = ExtResource("1_lpwdj")
BulletScene = ExtResource("2_ogldd")
metadata/_edit_group_ = true
@ -26,14 +27,12 @@ metadata/_edit_group_ = true
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = SubResource("AtlasTexture_2brqc")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
visible = false
[node name="Damage_HitBox" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_8gtts")
[node name="RigidBody2D" type="RigidBody2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D"]
visible = false
position = Vector2(0, 5)
shape = SubResource("CircleShape2D_cacb5")
@ -44,5 +43,10 @@ collision_mask = 2
[node name="PlayerDetectionArea" type="CollisionShape2D" parent="PlayerDetection"]
shape = SubResource("CircleShape2D_v711r")
[node name="ShootTimer" type="Timer" parent="."]
wait_time = 0.4
one_shot = true
[connection signal="area_entered" from="." to="." method="_on_area_entered"]
[connection signal="area_entered" from="PlayerDetection" to="." method="_on_player_detection_area_entered"]
[connection signal="area_exited" from="PlayerDetection" to="." method="_on_player_detection_area_exited"]

View file

@ -1,20 +1,20 @@
[gd_scene load_steps=4 format=3 uid="uid://cuixq5ex0j40h"]
[ext_resource type="Script" path="res://Scripts/Bullet.cs" id="1_s0j1e"]
[ext_resource type="Texture2D" uid="uid://cybpmpb0d8yva" path="res://Sprites/Projectile.png" id="2_6t448"]
[ext_resource type="Texture2D" uid="uid://bdyd0bht18n47" path="res://Sprites/EnemyProjectile.png" id="2_iw5k0"]
[sub_resource type="CircleShape2D" id="CircleShape2D_jxptd"]
radius = 2.23607
[node name="Bullet" type="Area2D" groups=["bullets"]]
collision_layer = 8
collision_layer = 128
collision_mask = 71
script = ExtResource("1_s0j1e")
Speed = 200.0
metadata/_edit_group_ = true
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("2_6t448")
texture = ExtResource("2_iw5k0")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_jxptd")

File diff suppressed because one or more lines are too long

View file

@ -70,7 +70,12 @@ public partial class Bullet : Area2D
private void _on_area_entered(Area2D area)
{
// Replace with function body.
if (area.IsInGroup("Solid"))
{
QueueFree();
return;
}
if (area.IsInGroup("Destroyable") && area is IDestructible destructible)
{
//Debug.WriteLine("Collision with destroyable object area");

View file

@ -3,7 +3,7 @@ using Godot;
using System;
using System.Diagnostics;
public partial class Enemy : Area2D
public partial class Enemy : Area2D, IDestructible
{
private InteractionController _cachedPlayer;
private EnemyState _currentState = EnemyState.Idle;
@ -11,10 +11,21 @@ public partial class Enemy : Area2D
[Export]
public PackedScene BulletScene { get; set; }
[Export] public float Health = 4f;
[Export] public double RateOfFire = 0.4f;
private float _currentHealth = 0f;
private bool _isDestroyed = false;
private Timer _cooldownTimer;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_currentHealth = Health;
_cooldownTimer = GetNode<Timer>("./ShootTimer");
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
@ -51,7 +62,7 @@ public partial class Enemy : Area2D
return;
}
if (IsPlayerInSight())
if (_cooldownTimer.IsStopped() && IsPlayerInSight())
{
// SHOOT
var bullet = this.CreateChild<Bullet>(BulletScene);
@ -60,6 +71,7 @@ public partial class Enemy : Area2D
// bullet.Transform = this.GlobalTransform;
// bullet.Position = this.Position;
bullet.SetDirection((_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized());
_cooldownTimer.Start(RateOfFire);
}
}
@ -99,6 +111,35 @@ public partial class Enemy : Area2D
}
}
// Bullets collision
private void _on_area_entered(Area2D area)
{
}
public void Hit(float damage)
{
if (_isDestroyed) return;
_currentHealth -= damage;
if (!(_currentHealth <= 0)) return;
_isDestroyed = true;
Explode();
}
private void Explode()
{
Debug.WriteLine("Ded");
//CreateParticles();
//CreateDebris();
QueueFree();
}
public bool IsDestroyed()
{
return _isDestroyed;
}
private enum EnemyState
{
Idle,

BIN
Sprites/EnemyProjectile.png (Stored with Git LFS) Normal file

Binary file not shown.

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://bdyd0bht18n47"
path="res://.godot/imported/EnemyProjectile.png-daf1b2f5d48a8e44ecd2aa1375387b0b.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://Sprites/EnemyProjectile.png"
dest_files=["res://.godot/imported/EnemyProjectile.png-daf1b2f5d48a8e44ecd2aa1375387b0b.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

View file

@ -79,6 +79,7 @@ Use={
2d_physics/layer_5="enemies"
2d_physics/layer_6="shoot-through"
2d_physics/layer_7="solid-actors"
2d_physics/layer_8="EnemyBullets"
[physics]