Optimised enemy code

This commit is contained in:
weedmjac000
2026-01-24 11:04:26 -07:00
parent 582d276a00
commit f1ab5a352c
2 changed files with 81 additions and 19 deletions

View File

@@ -3,6 +3,8 @@
[ext_resource type="Script" uid="uid://cy22d14qd0f48" path="res://Assets/Scenes/DEVSCENES/IQBasedEnemy.cs" id="1_a638n"]
[ext_resource type="Texture2D" uid="uid://c0pno0ac25r7i" path="res://Assets/Sprites/knight.png" id="2_xbll0"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_bc7hl"]
[sub_resource type="AtlasTexture" id="AtlasTexture_a1e3t"]
atlas = ExtResource("2_xbll0")
region = Rect2(0, 64, 32, 32)
@@ -125,15 +127,23 @@ animations = [{
[sub_resource type="RectangleShape2D" id="RectangleShape2D_41iyt"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_bc7hl"]
[node name="Enemy20IQ" type="RigidBody2D"]
[node name="IQEnemy" type="RigidBody2D"]
collision_layer = 8
collision_mask = 3
script = ExtResource("1_a638n")
metadata/IQ = 20
metadata/MaxHP = 100.0
[node name="GroundDetector" type="Area2D" parent="."]
position = Vector2(1.1368684e-13, 10.999999)
scale = Vector2(0.099999994, 0.099999994)
[node name="CollisionShape2D" type="CollisionShape2D" parent="GroundDetector"]
scale = Vector2(0.99999994, 0.99999994)
shape = SubResource("RectangleShape2D_bc7hl")
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
position = Vector2(9.536743e-07, 5.684342e-14)
position = Vector2(9.536743e-07, -1.9999999)
scale = Vector2(0.99999994, 0.99999994)
sprite_frames = SubResource("SpriteFrames_rqn21")
autoplay = "default"

View File

@@ -3,10 +3,15 @@ using System;
public partial class IQBasedEnemy : RigidBody2D
{
// Misc
float GlobalTimer;
// Enemy Stats
public int IQ;
public float HP;
public float MaxHP;
float RegenerationAmmount;
float RegenerationInterval;
// Enemy Conditions
float CurrentYVelocity;
@@ -14,32 +19,79 @@ public partial class IQBasedEnemy : RigidBody2D
public bool IsGrounded;
// Enemy Abilities
bool AbilityJump = false;
bool AbilityMove = false;
bool AbilityAttack = false;
bool AbilityDash = false;
bool AbilityCloak = false;
bool AbilityTeleport = false;
bool AbilityRegenerate = false;
public override void _Ready()
{
IQ = (int)GetMeta("IQ"); // Read IQ
{
// Read & Set Abilities
if (HasMeta("AbilityJump")) AbilityJump = (bool)GetMeta("AbilityJump");
if (HasMeta("AbilityMove")) AbilityMove = (bool)GetMeta("AbilityMove");
if (HasMeta("AbilityAttack")) AbilityAttack = (bool)GetMeta("AbilityAttack");
if (HasMeta("AbilityDash")) AbilityDash = (bool)GetMeta("AbilityDash");
if (HasMeta("AbilityCloak")) AbilityCloak = (bool)GetMeta("AbilityCloak");
if (HasMeta("AbilityTeleport")) AbilityTeleport = (bool)GetMeta("AbilityTeleport");
if (HasMeta("AbilityRegenerate")) AbilityRegenerate = (bool)GetMeta("AbilityRegenerate");
if (HasMeta("RegenerationAmmount")) RegenerationAmmount = (float)GetMeta("RegenerationAmmount");
else RegenerationAmmount = 1.0f;
if (HasMeta("RegenerationInterval")) RegenerationInterval = (float)GetMeta("RegenerationInterval");
else RegenerationInterval = 1.0f;
// Read and set stats
if (HasMeta("MaxHP")) MaxHP = (float)GetMeta("MaxHP");
MaxHP = (float)GetMeta("MaxHP"); // Get Max HP
if (IQ >= 100)
MaxHP += 20;
HP = MaxHP;
CurrentYVelocity = 0f;
PreviousYVelocity = 0f;
// Set up ground detection
GetNode<Area2D>("GroundDetector").AreaEntered += OnGroundEntered;
GetNode<Area2D>("GroundDetector").AreaExited += OnGroundExited;
IsGrounded = true;
// Apply IQ based permenant modifiers
IQ = (int)GetMeta("IQ"); // Read IQ
if (IQ >= 100) MaxHP += 20;
HP = MaxHP;
}
public override void _Process(double delta)
{
// Ground Detection
CurrentYVelocity = LinearVelocity[1];
if (CurrentYVelocity == 0 && PreviousYVelocity == 0)
IsGrounded = true;
else
IsGrounded = false;
PreviousYVelocity = CurrentYVelocity;
// Incriment Global Timer
GlobalTimer += (float)delta;
// Regenerate Health
if (AbilityRegenerate){
if (GlobalTimer % RegenerationInterval == 0) Regenerate(RegenerationAmmount);
}
}
void Flip(string facing){
if (facing == "RIGHT") Scale = new Vector2(1,1);
if (facing =="LEFT") Scale = new Vector2(-1,1);
}
public void Regenerate(float ammount){
if (HP < MaxHP) HP += ammount;
if (HP > MaxHP) HP = MaxHP; // Catch errors
}
public void Damage(float ammount){
HP -= ammount;
if (HP <= 0) Kill();
}
public void Kill(){
QueueFree(); // Change to proper death w/ corpse
}
// Ground Detection
public void OnGroundEntered(Area2D area){if (area.CollisionLayer == 0) IsGrounded = true;}
public void OnGroundExited(Area2D area){if (area.CollisionLayer == 0) IsGrounded = false;}
}