mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
Box models and item drops
This commit is contained in:
parent
cc9c4e5aa1
commit
2fe9618942
132 changed files with 9210 additions and 999 deletions
9
Scripts/Actors/EditorSprite3D.cs
Normal file
9
Scripts/Actors/EditorSprite3D.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Actors;
|
||||
|
||||
[Tool]
|
||||
public partial class EditorSprite3D : Sprite3D
|
||||
{
|
||||
|
||||
}
|
||||
1
Scripts/Actors/EditorSprite3D.cs.uid
Normal file
1
Scripts/Actors/EditorSprite3D.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://iih7baty86xt
|
||||
118
Scripts/Actors/ItemMarker3D.cs
Normal file
118
Scripts/Actors/ItemMarker3D.cs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
using Cirno.Scripts.Interactables;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Actors;
|
||||
|
||||
[Tool]
|
||||
public partial class ItemMarker3D : Marker3D
|
||||
{
|
||||
private LootItem _item;
|
||||
|
||||
[Export]
|
||||
public LootItem Item
|
||||
{
|
||||
get => _item;
|
||||
set
|
||||
{
|
||||
_item = value;
|
||||
if (Engine.IsEditorHint())
|
||||
{
|
||||
QueueRedraw();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public bool AutoSpawn { get; set; } = false;
|
||||
|
||||
[ExportToolButton("Update Icon")] public Callable RedrawButton => Callable.From(Redraw);
|
||||
[ExportToolButton("Clear Children")] public Callable ClearChildrenButton => Callable.From(ClearChildren);
|
||||
|
||||
// public override void _Draw()
|
||||
// {
|
||||
// if (!Engine.IsEditorHint()) return;
|
||||
// if (Item is null) return;
|
||||
// if (Item.InventorySprite is null) return;
|
||||
//
|
||||
// DrawTexture(Item.InventorySprite, - new Vector2(Item.InventorySprite.GetWidth() / 2f, Item.InventorySprite.GetHeight() / 2f));
|
||||
// }
|
||||
//
|
||||
private void Redraw()
|
||||
{
|
||||
QueueRedraw();
|
||||
}
|
||||
|
||||
private void ClearChildren()
|
||||
{
|
||||
var children = GetChildren();
|
||||
foreach (var child in children)
|
||||
{
|
||||
if (child is Sprite3D)
|
||||
{
|
||||
child.QueueFree();
|
||||
}
|
||||
}
|
||||
|
||||
_sprite = null;
|
||||
}
|
||||
|
||||
private void QueueRedraw()
|
||||
{
|
||||
if (!Engine.IsEditorHint()) return;
|
||||
if (Item?.InventorySprite is null) return;
|
||||
|
||||
if (_sprite is null)
|
||||
{
|
||||
GD.Print("Remaking sprite");
|
||||
_sprite = new EditorSprite3D();
|
||||
this.AddChild(_sprite);
|
||||
//_sprite.Owner = GetTree().EditedSceneRoot;
|
||||
}
|
||||
|
||||
_sprite.Texture = Item.InventorySprite;
|
||||
//_sprite.SetRotationDegrees(new Vector3(-45, 45, 0));
|
||||
_sprite.FixedSize = true;
|
||||
_sprite.SetBillboardMode(BaseMaterial3D.BillboardModeEnum.Enabled);
|
||||
_sprite.TextureFilter = BaseMaterial3D.TextureFilterEnum.Nearest;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private Sprite3D _sprite;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_sprite = GetNodeOrNull<Sprite3D>("Sprite3D");
|
||||
if (Engine.IsEditorHint())
|
||||
{
|
||||
QueueRedraw();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ClearChildren();
|
||||
|
||||
if (AutoSpawn)
|
||||
{
|
||||
Spawn(false);
|
||||
}
|
||||
}
|
||||
|
||||
// public override void _Process(double delta)
|
||||
// {
|
||||
//
|
||||
// }
|
||||
|
||||
public ItemPickup3D Spawn(bool deleteMarker)
|
||||
{
|
||||
if (Engine.IsEditorHint()) return null;
|
||||
if (Item is null) return null;
|
||||
|
||||
if (deleteMarker)
|
||||
{
|
||||
this.QueueFree();
|
||||
}
|
||||
|
||||
return Item.Spawn3D(this);
|
||||
}
|
||||
}
|
||||
1
Scripts/Actors/ItemMarker3D.cs.uid
Normal file
1
Scripts/Actors/ItemMarker3D.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://b88cmj87g78mx
|
||||
68
Scripts/Interactables/ItemPickup3D.cs
Normal file
68
Scripts/Interactables/ItemPickup3D.cs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
using System.Linq;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.Interactables;
|
||||
|
||||
public partial class ItemPickup3D : Interactable3D
|
||||
{
|
||||
[Export] public Array<LootItem> LootTable = [];
|
||||
|
||||
private bool _autoPickup = false;
|
||||
|
||||
public bool AutoPickup => _autoPickup;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_autoPickup = LootTable.Any(x => x.AutoPickup);
|
||||
|
||||
}
|
||||
|
||||
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
|
||||
{
|
||||
if (!MeetsRequirements()) return false;
|
||||
Collect();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void AddItemsToInventory()
|
||||
{
|
||||
var failedItems = new Array<LootItem>();
|
||||
foreach (var item in LootTable)
|
||||
{
|
||||
if (!InventoryManager.Instance.AddItem(item))
|
||||
{
|
||||
failedItems.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
if (failedItems.Count > 0)
|
||||
{
|
||||
foreach (var failedItem in failedItems)
|
||||
{
|
||||
var dup = this.Duplicate() as ItemPickup;
|
||||
this.AddSibling(dup);
|
||||
dup.LootTable = [failedItem];
|
||||
}
|
||||
}
|
||||
|
||||
// Delet This
|
||||
|
||||
QueueFree();
|
||||
}
|
||||
|
||||
public void Collect()
|
||||
{
|
||||
AddItemsToInventory();
|
||||
|
||||
}
|
||||
|
||||
public void SetSprite(Texture2D sprite)
|
||||
{
|
||||
var spriteNode = GetNodeOrNull<Sprite3D>("Sprite3D");
|
||||
if (spriteNode is null) return;
|
||||
spriteNode.Texture = sprite;
|
||||
}
|
||||
}
|
||||
1
Scripts/Interactables/ItemPickup3D.cs.uid
Normal file
1
Scripts/Interactables/ItemPickup3D.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cc60s31w2sive
|
||||
|
|
@ -24,24 +24,67 @@ public partial class LootItem : Resource
|
|||
|
||||
[Export(PropertyHint.Flags, "Icon,Count,Ammo,Energy")]
|
||||
public UiItemType UiType { get; set; } = 0;
|
||||
|
||||
[Export] public bool Selectable;
|
||||
[Export] public bool AutoPickup { get; private set; } = false;
|
||||
|
||||
[Export] public Texture2D InventorySprite;
|
||||
|
||||
//[Export] public SpriteFrames WorldSprite;
|
||||
//[Export] public PackedScene HudItemScene;
|
||||
[Export(PropertyHint.File)] public StringName DropScenePath { get; private set; } // Has to be a string path to avoid recursion issues
|
||||
[Export(PropertyHint.File)]
|
||||
public StringName DropScenePath { get; private set; } // Has to be a string path to avoid recursion issues
|
||||
|
||||
[Export(PropertyHint.File)]
|
||||
public StringName DropScenePath3D { get; private set; } // Has to be a string path to avoid recursion issues
|
||||
|
||||
public ItemPickup Spawn(Node2D sibling, bool dropAsChild = false)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(DropScenePath)) return null;
|
||||
var itemScene = GD.Load<PackedScene>(DropScenePath);
|
||||
var spawnedItem = dropAsChild ? sibling.CreateChild<ItemPickup>(itemScene) : sibling.CreateSibling<ItemPickup>(itemScene);
|
||||
|
||||
var spawnedItem = dropAsChild
|
||||
? sibling.CreateChild<ItemPickup>(itemScene)
|
||||
: sibling.CreateSibling<ItemPickup>(itemScene);
|
||||
|
||||
spawnedItem.Name = this.ItemKey;
|
||||
|
||||
|
||||
spawnedItem.LootTable.Add(this);
|
||||
spawnedItem.SetSprite(InventorySprite);
|
||||
|
||||
return spawnedItem;
|
||||
}
|
||||
|
||||
public ItemPickup3D Spawn3D(Node3D sibling, bool dropAsChild = false)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(DropScenePath3D)) return null;
|
||||
var itemScene = GD.Load<PackedScene>(DropScenePath3D);
|
||||
|
||||
var spawnedItem = itemScene.Instantiate<ItemPickup3D>();
|
||||
spawnedItem.Name = this.ItemKey;
|
||||
if (dropAsChild)
|
||||
{
|
||||
CallDeferred(MethodName.DeferredSpawn3D, sibling, spawnedItem, sibling.GlobalPosition);
|
||||
//sibling.CallDeferred(Node.MethodName.AddChild, spawnedItem);
|
||||
//sibling.AddChild(spawnedItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
CallDeferred(MethodName.DeferredSpawn3D, sibling.GetParentNode3D(), spawnedItem, sibling.GlobalPosition);
|
||||
//sibling.GetParent().CallDeferred(Node.MethodName.AddChild, spawnedItem);
|
||||
//sibling.GetParent().AddChild(spawnedItem);
|
||||
}
|
||||
|
||||
//spawnedItem.GlobalPosition = sibling.GlobalPosition;
|
||||
|
||||
spawnedItem.LootTable.Add(this);
|
||||
spawnedItem.SetSprite(InventorySprite);
|
||||
|
||||
return spawnedItem;
|
||||
}
|
||||
|
||||
private void DeferredSpawn3D(Node3D parent, Node3D instance, Vector3 position)
|
||||
{
|
||||
parent.AddChild(instance);
|
||||
instance.GlobalPosition = position;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue