Time modifiers

This commit is contained in:
Marco 2025-02-09 11:48:30 +01:00
commit 4e6257dbe3
6 changed files with 99 additions and 9 deletions

View file

@ -36,6 +36,7 @@ burstInterval = 1.0
spread = 180.0
owner = 2
_modifier = SubResource("Resource_l3ln6")
_timeModifiers = Array[Resource]([])
WaitForCompletion = true
[sub_resource type="Resource" id="Resource_7yi74"]
@ -50,6 +51,7 @@ burstInterval = 1.0
spread = 180.0
owner = 2
_modifier = SubResource("Resource_l3ln6")
_timeModifiers = null
WaitForCompletion = true
[sub_resource type="Resource" id="Resource_gm1rv"]

View file

@ -1,7 +1,10 @@
using Cirno.Scripts.Actors;
using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Actors;
using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.AttackPatterns;
@ -18,6 +21,7 @@ public partial class SpiralPattern : AttackPattern
[Export] private float spread = 360f;
[Export] private BulletOwner owner = BulletOwner.Enemy;
[Export] private Resource _modifier;
[Export] private Array<Resource> _timeModifiers;
private double timer;
private double burstTimer;
@ -50,6 +54,7 @@ public partial class SpiralPattern : AttackPattern
BulletScene = BulletScene,
RotationOffset = _rotationOffset,
Modifier = _modifier as IBulletModifier,
TimeModifiers = _timeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().ToList() ?? new List<TimeModifier>()
});
// spawner.SpawnSpiralPattern(Boss.GlobalPosition, bulletSpeed, owner, bulletCount, rotationSpeed, timer, spread, BulletScene);

View file

@ -1,6 +1,8 @@
using Godot;
using System;
using System.Diagnostics;
using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
public partial class Bullet : Area2D
{
@ -14,15 +16,66 @@ public partial class Bullet : Area2D
public BulletOwner Owner = BulletOwner.None;
private Vector2 _direction = Vector2.Right;
//public delegate void BulletHitEventHandler(Node Body);
//public event BulletHitEventHandler BulletHit;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
private double _elapsedTime = 0f;
private BulletInfo _bulletInfo;
public void Initialize(BulletInfo bulletInfo)
{
//this.Connect("body_entered", new Callable(this, nameof(OnBodyEntered)));
_bulletInfo = bulletInfo;
Position = bulletInfo.Position;
Owner = bulletInfo.Owner;
}
private void ApplyTimeModifiers()
{
foreach (var modifier in _bulletInfo.TimeModifiers)
{
if (modifier.Applied)
{
continue;
}
if (_elapsedTime >= modifier.TimeInSeconds)
{
switch (modifier.ModifierType)
{
case TimeModifierType.SpeedChange:
//_bulletInfo.Speed += modifier.Value;
Speed = modifier.Value;
break;
case TimeModifierType.RotationChange:
RotateBullet(modifier.Value);
//Rotation += Mathf.DegToRad(modifier.Value);
break;
case TimeModifierType.FacePlayer:
FacePlayer();
break;
}
modifier.Applied = true;
}
}
}
private void RotateBullet(float degrees)
{
float radians = Mathf.DegToRad(degrees);
_direction = _direction.Rotated(radians).Normalized(); // Rotate direction
}
private void FacePlayer()
{
// TODO: cache player
var player = GetTree().GetFirstNodeInGroup("Player") as Node2D;
if (player != null)
{
_direction = (player.GlobalPosition - this.GlobalPosition).Normalized();
//LookAt(player.GlobalPosition);
}
}
//private void OnBodyEntered(Node body)
//{
@ -45,6 +98,12 @@ public partial class Bullet : Area2D
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
_elapsedTime += delta;
if (_bulletInfo != null)
{
ApplyTimeModifiers();
}
this.Position += ((float)(Speed * delta) * _direction);
}

View file

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Components;
@ -20,9 +22,11 @@ public partial class BulletSpawner : Node2D
{
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletInfo.BulletScene ?? BulletScene, bulletInfo.Position);
bullet.Initialize(bulletInfo);
//var bullet = BulletScene.Instantiate<Bullet>();
bullet.Position = bulletInfo.Position;
bullet.Owner = bulletInfo.Owner;
//bullet.Position = bulletInfo.Position;
//bullet.Owner = bulletInfo.Owner;
//bullet.Speed = speed;
float modifiedSpeed = bulletInfo.Modifier?.ModifySpeed(bulletInfo.Speed, i, bulletInfo.BulletCount) ?? bulletInfo.Speed;
@ -93,4 +97,5 @@ public class BulletInfo
public float Spread { get; set; }
public PackedScene BulletScene { get; set; }
public IBulletModifier Modifier { get; set; }
public List<TimeModifier> TimeModifiers { get; set; } = new List<TimeModifier>();
}

View file

@ -0,0 +1,19 @@
using Godot;
namespace Cirno.Scripts.Resources;
[GlobalClass]
public partial class TimeModifier : Resource
{
[Export] public float TimeInSeconds = 1f;
[Export] public TimeModifierType ModifierType;
[Export] public float Value;
public bool Applied = false;
}
public enum TimeModifierType
{
SpeedChange,
RotationChange,
FacePlayer
}

View file

@ -21,7 +21,7 @@ Settings/temporaryLogLifetime=5.0
[application]
config/name="Cirno"
run/main_scene="res://Scenes/test.tscn"
run/main_scene="res://Scenes/Maps/BossTestArena.tscn"
config/features=PackedStringArray("4.3", "C#", "GL Compatibility")
config/icon="res://icon.svg"