working spder bomb

This commit is contained in:
Maddo 2025-02-28 11:17:28 +01:00
commit f1f40a91dd
17 changed files with 400 additions and 105 deletions

View file

@ -28,6 +28,8 @@ public partial class Bullet : Area2D
private GameManager _gameManager;
[Signal] public delegate void OnDestroyEventHandler();
public void Initialize(BulletInfo bulletInfo, GameManager gameManager)
{
_bulletInfo = bulletInfo;
@ -234,11 +236,11 @@ public partial class Bullet : Area2D
{
if (_bulletInfo?.DestructionParticlesScene != null)
{
var particle = this.CreateSibling<AutodeleteParticle>(_bulletInfo.DestructionParticlesScene);
this.CreateSibling<Node2D>(_bulletInfo.DestructionParticlesScene);
particle.Init();
//particle.Init();
}
EmitSignal(SignalName.OnDestroy);
QueueFree();
}
}

View file

@ -9,7 +9,7 @@ using Cirno.Scripts.Utils;
public partial class GameManager : Node2D
{
public static GameManager Instance { get; private set;}
public static GameManager Instance { get; private set; }
private Hud _hud;
private PlayerMovement _player;
@ -25,7 +25,7 @@ public partial class GameManager : Node2D
[Export] public PackedScene PlayerTemplate { get; set; }
[Export] public Dictionary<int, NodePath> SpawnMarkers { get; private set; } = new();
//[Export] public Marker2D PlayerSpawnMarker { get; set; }
[Export] public PackedScene WeaponTemplate { get; private set; }
@ -51,12 +51,12 @@ public partial class GameManager : Node2D
[Signal]
public delegate void PlayerRespawnedEventHandler();
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
Instance = this;
Instance = this;
_hud = GetNodeOrNull<Hud>("HUD");
if (_hud == null) GD.Print("No HUD in scene.");
@ -74,7 +74,7 @@ public partial class GameManager : Node2D
{
this.GameStateChange += _hud.OnGameStateChanged;
}
if (_inventoryManager != null && _hud != null)
{
_inventoryManager.ItemAdded += (item, currentAmount) => _hud.AddInventoryItem(item, currentAmount);
@ -82,7 +82,7 @@ public partial class GameManager : Node2D
}
PlayerRespawned += OnPlayerRespawned;
GameState = GameState.Playing;
//_ = DelayPlayerSpawn();
@ -102,9 +102,9 @@ public partial class GameManager : Node2D
StartingEquipment.AddRange(mapStartData.StartingEquipment);
}
public void ApplySessionState(SessionSettings settings)
public void ApplySessionState(SessionSettings settings)
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
@ -137,7 +137,7 @@ public partial class GameManager : Node2D
_player.ShieldChanged += (newShield, maxShield) => _hud.UpdateShield(newShield, maxShield);
_player.InteractableAreaEntered += (interactable) => _hud.UpdateInteractable(interactable);
_player.Death += () =>
{
// Show Game Over
@ -149,7 +149,7 @@ public partial class GameManager : Node2D
GD.Print("No player and hud in scene");
return;
}
if (_inventoryManager is not null)
{
_inventoryManager.ItemAdded += (LootItem item, int amount) =>
@ -161,23 +161,25 @@ public partial class GameManager : Node2D
};
_inventoryManager.WeaponEquip += _player.EquipWeapon;
_inventoryManager.ItemUsed += _player.UseItem;
}
SpawnWeapons();
}
public void SpawnPlayer()
{
if (_player != null) return;
//_player = this.CreateChild<PlayerMovement>(PlayerTemplate, PlayerSpawnMarker.Position );
_player = PlayerTemplate.Instantiate<PlayerMovement>();
this.CallDeferred("add_child", _player);
_player.Transform = this.GlobalTransform;
_player.GlobalPosition = GetStartPosition();
//_player.GlobalPosition = PlayerSpawnMarker.Position;
CameraTargetPlayer();
@ -197,11 +199,11 @@ public partial class GameManager : Node2D
if (SpawnMarkers.TryGetValue(MapStartData.EggIndex, out var spawnMarker))
{
var marker = GetNode<Node2D>(spawnMarker);
return marker.Position; // Why position and not globalposition? I have no idea
}
}
var m = GetNode<Node2D>(SpawnMarkers.First().Value);
return m.GlobalPosition;
}
@ -222,7 +224,7 @@ public partial class GameManager : Node2D
{
_cameraTarget.Position += offset.Value;
}
}
private void SpawnWeapons()
@ -305,12 +307,13 @@ public partial class GameManager : Node2D
}
}
public GameState ToggleControlMode() {
if (GameState is GameState.Playing)
public GameState ToggleControlMode()
{
if (GameState is GameState.Playing)
{
ChangeState(GameState.Controlling);
}
else if (GameState is GameState.Controlling)
else if (GameState is GameState.Controlling)
{
ChangeState(GameState.Playing);
}

View file

@ -8,20 +8,23 @@ using Cirno.Scripts.Resources;
public partial class InventoryManager : Node2D
{
public bool RedKeycard { get; set; }
private Dictionary<string, ItemContainer> _itemsDict = new Dictionary<string, ItemContainer>();
public List<ItemContainer> Items => _itemsDict.Where(x => x.Value.Count > 0).Select(x => x.Value).ToList();
[Signal]
public delegate void ItemAddedEventHandler(LootItem item, int currentAmount);
[Signal]
public delegate void ItemRemovedEventHandler(string itemKey, int currentAmount);
[Signal]
public delegate void WeaponEquipEventHandler(string itemKey);
[Signal]
public delegate void ItemUsedEventHandler(LootItem itemKey, int totalCount);
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
@ -31,12 +34,12 @@ public partial class InventoryManager : Node2D
public override void _Process(double delta)
{
}
public bool HasItems(List<string> itemKeys)
{
return itemKeys.Aggregate(false, (current, item) => current || GetItemCount(item) > 0);
}
public int GetItemCount(string itemKey)
{
return _itemsDict.TryGetValue(itemKey, out var itm) ? itm.Count : 0;
@ -47,20 +50,20 @@ public partial class InventoryManager : Node2D
if (!_itemsDict.TryGetValue(itemKey, out var itm)) return 0;
int removed = Math.Min(itm.Count, amount);
itm.Count -= amount;
if (itm.Count <= 0)
{
_itemsDict.Remove(itemKey);
}
EmitSignal(nameof(ItemRemoved), itemKey, itm.Count);
return removed;
}
public bool AddItem(LootItem item)
{
//var item = new LootItem() { Item = type, Amount = amount };
@ -73,7 +76,7 @@ public partial class InventoryManager : Node2D
Count = item.Amount,
});
GD.Print($"Added new ({item.ItemKey}) {item.Item} x{item.Amount}");
EmitSignal(nameof(ItemAdded), item, item.Amount);
}
else
@ -82,7 +85,7 @@ public partial class InventoryManager : Node2D
{
itm.Count += item.Amount;
}
else
else
{
if (item.PickupIfMaxed)
{
@ -95,10 +98,10 @@ public partial class InventoryManager : Node2D
}
EmitSignal(nameof(ItemAdded), item, itm.Count);
}
return true;
}
public void AddRedKeycard()
{
RedKeycard = true;
@ -114,29 +117,27 @@ public partial class InventoryManager : Node2D
if (!_itemsDict.TryGetValue(itemKey, out var itm)) return false;
if (itm.Count == 0) return false;
GD.Print($"Used item in manager {itemKey}");
switch (itm.Item.Item)
{
case ItemTypes.Medkit:
// Heal
break;
// Heal
case ItemTypes.FrogBomb:
// Use Bomb
break;
// Use Bomb
case ItemTypes.Bomb:
// Bomb
break;
// Bomb
case ItemTypes.Mine:
break;
case ItemTypes.Battery:
EmitSignal(SignalName.ItemUsed, itm.Item, itm.Count);
break;
case ItemTypes.Weapon:
// Equip weapon
EmitSignal(SignalName.WeaponEquip, itm.Item.ItemKey);
break;
}
return true;
}
}

View file

@ -13,7 +13,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
{
[Export]
public int Speed { get; set; } = 400;
[Export]
public int StrafeSpeed { get; set; } = 200;
@ -27,7 +27,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
[Export]
public string GameOverScene { get; set; }
[Export]
public Texture2D WingsSprite { get; set; }
@ -42,14 +42,14 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
private Vector2 _movementDirection { get; set; }
private Vector2 _facingDirection { get; set; }
private Vector2 _rightStickInput { get; set; }
private Sprite2D _crosshair;
[Export] public float MaxHealth = 32f;
[Export] public float MaxShield = 32f;
[Export] public Shader BlinkShader {get;set;}
[Export] public Shader BlinkShader { get; set; }
[Export] public Sprite2D HitboxSprite { get; set; }
[Export] public BulletOwner BulletGroup { get; set; } = BulletOwner.Player;
@ -67,10 +67,10 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
public Array<Weapon> EquippedWeapons { get; set; } = new Array<Weapon>();
public int CurrentWeaponIndex { get; set; } = 0;
//private float _currentHealth = 0f;
//private float _currentShield = 0f;
private bool _isDestroyed = false;
private GameManager _gameManager;
@ -79,14 +79,14 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
private ActorResourceProvider _healthProvider;
private ActorResourceProvider _shieldProvider;
private bool _isStrafing { get; set; }
private bool _canMove = true;
[Signal]
public delegate void HealthChangedEventHandler(float newHealth, float maxHealth);
[Signal]
public delegate void ShieldChangedEventHandler(float newShield, float maxShield);
@ -95,7 +95,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
[Signal]
public delegate void InteractableAreaExitedEventHandler(Interactable interactable);
[Signal]
public delegate void DeathEventHandler();
@ -104,7 +104,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
get => _healthProvider.CurrentResource;
set => _healthProvider.CurrentResource = value;
}
public float CurrentShield
{
get => _shieldProvider.CurrentResource;
@ -126,7 +126,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
_animatedSprite = GetNode<AnimatedSprite2D>("./Smoothing2D/AnimatedSprite2D");
_crosshair = GetNode<Sprite2D>("./Smoothing2D/Crosshair");
_healthProvider = GetNode<ActorResourceProvider>("HealthProvider");
_shieldProvider = GetNode<ActorResourceProvider>("ShieldProvider");
@ -154,18 +154,18 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
_inventoryManager = this.GetInventoryManager();
_gameManager.GameStateChange += GameManagerOnGameStateChange;
if (SelectorScene != null)
{
_selector = this.CreateSibling<Selector>(SelectorScene, this.GlobalPosition);
_selector.Visible = false;
}
_lastCheckPointPosition = GlobalPosition;
_ = UnTeleport();
}
private void GameManagerOnGameStateChange(GameState state)
{
switch (state)
@ -181,7 +181,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
case GameState.Dialogue:
_canMove = false;
break;
}
}
@ -200,7 +200,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
_canMove = true;
}
}
public void AddWeapon(Weapon weapon)
{
EquippedWeapons.Add(weapon);
@ -215,7 +215,44 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
EquipWeapon(weapon);
}
public void UseItem(LootItem item, int currentAmount)
{
GD.Print($"Used item on player {item.ItemKey}");
switch (item.Item)
{
case ItemTypes.FrogBomb:
_inventoryManager.RemoveItem(item.ItemKey, 1);
// emit projectile
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, item.WeaponData.BulletData.BulletScene, this.GlobalPosition);
var bulletData = item.WeaponData.MakeBullet(this.GlobalPosition);
bullet.Initialize(bulletData, _gameManager);
//bullet.SetDirection(ShootDirection);
bullet.SetDirection(_facingDirection);
bullet.Speed = item.WeaponData.BulletData.BulletSpeed;
RequestMovementDisable(true);
// set camera
_gameManager.CameraTargetObject(bullet);
// set event destroy
bullet.OnDestroy += () =>
{
_gameManager.CameraTargetPlayer();
RequestMovementDisable(false);
};
// go back
break;
}
//var item = _inventoryManager.getitem
}
// Triggered by event in inventorymanager
public void EquipWeapon(Weapon weapon)
{
EquippedWeapon = weapon;
@ -229,10 +266,10 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
{
CurrentWeaponIndex = EquippedWeapons.Count - 1;
}
EquipWeapon(EquippedWeapons[CurrentWeaponIndex]);
}
public void PreviousWeapon()
{
CurrentWeaponIndex -= 1;
@ -240,7 +277,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
{
CurrentWeaponIndex = 0;
}
EquipWeapon(EquippedWeapons[CurrentWeaponIndex]);
}
@ -309,11 +346,11 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
{
return Input.GetVector("left", "right", "up", "down");
}
private Vector2 GetRightStickInput()
{
return new Vector2(
Input.GetAxis("aim_left","aim_right"),
Input.GetAxis("aim_left", "aim_right"),
Input.GetAxis("aim_up", "aim_down")
);
}
@ -328,7 +365,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
//var cPos = new Vector2(this.Position.X + CrosshairDistance * Godot.Mathf.Cos(angle), this.Position.Y + CrosshairDistance * Godot.Mathf.Sin(angle));
}
public override void _Process(double delta)
{
if (_isDestroyed)
@ -336,8 +373,9 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
if (Input.IsActionJustPressed(_shootActionName))
Respawn();
return;
};
}
;
SetAnimation();
if (!_canMove) return;
@ -349,9 +387,9 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
{
HitboxSprite.Visible = _isStrafing;
}
_rightStickInput = GetRightStickInput();
// Update Facing Direction
if (!_isStrafing)
{
@ -377,8 +415,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
{
PreviousWeapon();
}
_crosshair.Position = CalculateCrosshairPosition();
_crosshair.Position = CalculateCrosshairPosition();
}
public void Respawn()
@ -394,8 +432,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
{
if (_isDestroyed) return;
if (!_canMove) return;
Velocity = _movementDirection * MovementSpeed;
Velocity = _movementDirection * MovementSpeed;
MoveAndSlide();
}
@ -415,19 +453,19 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
_selector.AddInteractable(interactable);
//_selector.SelectedInteractable = interactable;
}
}
private void _on_interaction_controller_area_exited(Area2D area)
{
if (!_canMove) return;
if (area.IsInGroup("Interactable") && area is Interactable interactable)
if (area.IsInGroup("Interactable") && area is Interactable interactable)
{
Debug.WriteLine($"Interactable {area.Name} Exited");
EmitSignal(nameof(InteractableAreaExited), interactable);
if (_selector == null) return;
_selector.RemoveInteractable(interactable);
}
@ -458,20 +496,25 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
GD.Print($"Player damaged for {damage}");
if (_isDestroyed) return;
if (CurrentShield > 0 && type is not DamageType.Explosive or DamageType.Acid) {
if (CurrentShield > 0 && type is not DamageType.Explosive or DamageType.Acid)
{
// Reduce shield
PlayShieldAnimation();
CurrentShield -= damage;
if (CurrentShield < 0 ) {
if (CurrentShield < 0)
{
CurrentHealth -= Math.Abs(CurrentShield);
CurrentShield = 0;
}
}
else {
if (type is DamageType.Fire) {
else
{
if (type is DamageType.Fire)
{
CurrentHealth -= damage * 2;
}
else {
else
{
CurrentHealth -= damage;
}
@ -483,13 +526,13 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
Explode();
}
private void PlayShieldAnimation()
private void PlayShieldAnimation()
{
if (_shieldParticles is null) return;
_shieldParticles.Emitting = true;
}
public void Blink()
public void Blink()
{
if (BlinkShader != null)
{
@ -506,7 +549,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
await ToSignal(tween, "finished");
}
public async Task Teleport()
public async Task Teleport()
{
((ShaderMaterial)_animatedSprite.Material).Shader = BlinkShader;
@ -516,7 +559,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
await ToSignal(tween, "finished");
}
public async Task UnTeleport()
public async Task UnTeleport()
{
((ShaderMaterial)_animatedSprite.Material).Shader = BlinkShader;