aboutsummaryrefslogtreecommitdiff
path: root/source/game/components
diff options
context:
space:
mode:
Diffstat (limited to 'source/game/components')
-rw-r--r--source/game/components/CameraFollow.cs5
-rw-r--r--source/game/components/EntityAttributes.cs35
-rw-r--r--source/game/components/TargetPosition.cs7
-rw-r--r--source/game/components/entity/GameWorldEntity.cs14
-rw-r--r--source/game/components/items/Inventory.cs96
-rw-r--r--source/game/components/items/ItemStack.cs34
-rw-r--r--source/game/components/physics/CollisionBox.cs36
-rw-r--r--source/game/components/physics/PhysicsEntity.cs38
-rw-r--r--source/game/components/player/LocalPlayer.cs5
-rw-r--r--source/game/components/player/PlayerInput.cs14
-rw-r--r--source/game/components/skybox/SkyboxRotateZ.cs17
11 files changed, 301 insertions, 0 deletions
diff --git a/source/game/components/CameraFollow.cs b/source/game/components/CameraFollow.cs
new file mode 100644
index 0000000..d6ae2f7
--- /dev/null
+++ b/source/game/components/CameraFollow.cs
@@ -0,0 +1,5 @@
+namespace Celesteia.Game.Components {
+ public class CameraFollow {
+ public float Weight = 1f;
+ }
+} \ No newline at end of file
diff --git a/source/game/components/EntityAttributes.cs b/source/game/components/EntityAttributes.cs
new file mode 100644
index 0000000..74a8e82
--- /dev/null
+++ b/source/game/components/EntityAttributes.cs
@@ -0,0 +1,35 @@
+using System;
+using System.Linq;
+
+namespace Celesteia.Game.Components {
+ public class EntityAttributes {
+ public EntityAttributeMap Attributes;
+
+ public EntityAttributes(EntityAttributeMap attributes) {
+ Attributes = attributes;
+ }
+
+ public EntityAttributes() : this(new EntityAttributeMap()) {}
+
+ public partial class EntityAttributeMap {
+ private float[] attributes;
+
+ public EntityAttributeMap() {
+ attributes = new float[Enum.GetValues(typeof(EntityAttribute)).Cast<EntityAttribute>().Count()];
+ }
+
+ public EntityAttributeMap Set(EntityAttribute attribute, float value) {
+ attributes[(int) attribute] = value;
+ return this;
+ }
+
+ public float Get(EntityAttribute attribute) {
+ return attributes[(int) attribute];
+ }
+ }
+ }
+
+ public enum EntityAttribute {
+ MovementSpeed, JumpFuel, JumpForce, BlockRange
+ }
+} \ No newline at end of file
diff --git a/source/game/components/TargetPosition.cs b/source/game/components/TargetPosition.cs
new file mode 100644
index 0000000..f8b1a9c
--- /dev/null
+++ b/source/game/components/TargetPosition.cs
@@ -0,0 +1,7 @@
+using Microsoft.Xna.Framework;
+
+namespace Celesteia.Game.Components {
+ public class TargetPosition {
+ public Vector2 Target;
+ }
+} \ No newline at end of file
diff --git a/source/game/components/entity/GameWorldEntity.cs b/source/game/components/entity/GameWorldEntity.cs
new file mode 100644
index 0000000..3087865
--- /dev/null
+++ b/source/game/components/entity/GameWorldEntity.cs
@@ -0,0 +1,14 @@
+using Celesteia.Game.ECS;
+
+namespace Celesteia.Game.Components.Entity {
+ public class GameWorldEntity {
+ private GameWorld _w;
+ private int _id;
+ public GameWorldEntity(GameWorld w, int id) {
+ _w = w;
+ _id = id;
+ }
+
+ public void Destroy() => _w.DestroyEntity(_id);
+ }
+} \ No newline at end of file
diff --git a/source/game/components/items/Inventory.cs b/source/game/components/items/Inventory.cs
new file mode 100644
index 0000000..4309f5d
--- /dev/null
+++ b/source/game/components/items/Inventory.cs
@@ -0,0 +1,96 @@
+using System;
+using Celesteia.Resources;
+
+namespace Celesteia.Game.Components.Items {
+ public class Inventory {
+ private ItemStack[] items;
+ public readonly int Capacity;
+
+ public Inventory(int slots = 27, params ItemStack[] startingItems) {
+ Capacity = slots;
+ items = new ItemStack[slots];
+
+ for (int i = 0; i < startingItems.Length; i++ ) items[i] = startingItems[i];
+ }
+
+ // Try adding an item to the inventory, return false if the inventory has no capacity for this action.
+ public bool AddItem(ItemStack stack) {
+ ItemStack existingStack = GetLastItemStack(stack.Key);
+
+ // If an item stack with this ID already exists, add the amount to that stack.
+ if (existingStack != null) {
+ existingStack.Amount += stack.Amount;
+
+ // If the max stack size was exceeded and a new stack has to be created, create it.
+ ItemStack newStack = existingStack.NewStack();
+ if (newStack != null) {
+ if (!HasCapacity()) return false;
+ existingStack.Amount = existingStack.Type.MaxStackSize;
+ AddItemStack(newStack);
+ }
+ } else AddItemStack(stack);
+
+ return true;
+ }
+
+ public ItemStack GetItemStackWithID(byte id) {
+ return Array.FindLast(items, x => x != null && x.ID == id && x.Amount < x.Type.MaxStackSize);
+ }
+
+ public ItemStack GetLastItemStack(NamespacedKey key) {
+ return Array.FindLast(items, x => x != null && x.Key.Equals(key) && x.Amount < x.Type.MaxStackSize);
+ }
+
+ public ItemStack GetSlot(int slot) {
+ if (slot < 0 || slot > Capacity - 1) throw new ArgumentException($"Slot {slot} falls outside of the inventory's capacity.");
+ return items[slot];
+ }
+
+ public void SetSlot(int slot, ItemStack stack) {
+ if (slot < 0 || slot > Capacity - 1) throw new ArgumentException($"Slot {slot} falls outside of the inventory's capacity.");
+ items[slot] = stack;
+ }
+
+ public bool ContainsAmount(NamespacedKey key, int amount) {
+ return GetAmount(key) >= amount;
+ }
+
+ public int GetAmount(NamespacedKey key) {
+ int amount = 0;
+
+ ItemStack[] stacksOfItem = Array.FindAll(items, x => x != null && x.Key.Equals(key));
+ foreach (ItemStack stackOfItem in stacksOfItem) amount += stackOfItem.Amount;
+
+ return amount;
+ }
+
+ public void RemoveAmount(NamespacedKey key, int amount) {
+ int amountToGo = amount;
+
+ ItemStack lastStack;
+ while (amountToGo > 0) {
+ lastStack = Array.FindLast(items, x => x != null && x.Key.Equals(key));
+ int toRemove = Math.Min(lastStack.Amount, amountToGo);
+
+ lastStack.Amount -= toRemove;
+ amountToGo -= toRemove;
+
+ AssertAmounts();
+ }
+ }
+
+ private void AddItemStack(ItemStack newStack) {
+ if (!HasCapacity()) return;
+ int i = Array.FindIndex(items, x => x == null);
+ items[i] = newStack;
+ }
+
+ private bool HasCapacity() {
+ return Array.Exists(items, x => x == null);
+ }
+
+ public void AssertAmounts() {
+ for (int i = 0; i < items.Length; i++) if (items[i] != null && items[i].Amount <= 0) items[i] = null;
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/components/items/ItemStack.cs b/source/game/components/items/ItemStack.cs
new file mode 100644
index 0000000..360904b
--- /dev/null
+++ b/source/game/components/items/ItemStack.cs
@@ -0,0 +1,34 @@
+using Celesteia.Resources;
+using Celesteia.Resources.Types;
+
+namespace Celesteia.Game.Components.Items {
+ public class ItemStack {
+ public byte ID;
+ public NamespacedKey Key;
+ public int Amount;
+ public readonly ItemType Type;
+
+ public ItemStack(NamespacedKey key, int amount) {
+ Key = key;
+ Amount = amount;
+
+ Type = ResourceManager.Items.GetResource(key) as ItemType;
+ }
+
+ public ItemStack NewStack() {
+ ItemStack stack = null;
+ if (Amount > Type.MaxStackSize) stack = new ItemStack(Key, Amount - Type.MaxStackSize);
+
+ return stack;
+ }
+
+ public ItemStack Clone() {
+ return new ItemStack(Key, Amount);
+ }
+
+ public override string ToString()
+ {
+ return $"{Amount}x {Type.Name}";
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/components/physics/CollisionBox.cs b/source/game/components/physics/CollisionBox.cs
new file mode 100644
index 0000000..6e678ac
--- /dev/null
+++ b/source/game/components/physics/CollisionBox.cs
@@ -0,0 +1,36 @@
+using System;
+using MonoGame.Extended;
+using Microsoft.Xna.Framework;
+
+namespace Celesteia.Game.Components.Physics {
+ public class CollisionBox {
+ public RectangleF _bounds;
+ public RectangleF Bounds { get => _bounds; set => _bounds = value; }
+ private Rectangle _rounded;
+ public Rectangle Rounded { get => _rounded; }
+
+ public CollisionBox(float width, float height) {
+ _bounds = new RectangleF(-width / 2f, -height / 2f, width, height);
+ _rounded = Round(_bounds);
+ }
+
+ public void Update(Vector2 position) {
+ _bounds.X = position.X - (Bounds.Width / 2f);
+ _bounds.Y = position.Y - (Bounds.Height / 2f);
+ _rounded = Round(_bounds);
+ }
+
+ public Rectangle Round(RectangleF floatRect) {
+ return new Rectangle((int)MathF.Floor(floatRect.X), (int)MathF.Floor(floatRect.Y), (int)MathF.Ceiling(floatRect.Width), (int)MathF.Ceiling(floatRect.Height));
+ }
+
+ public RectangleF Intersection(CollisionBox other) {
+ return Intersection(other.Bounds);
+ }
+
+ public RectangleF Intersection(RectangleF other) {
+ RectangleF intersection = other.Intersection(Bounds);
+ return intersection;
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/components/physics/PhysicsEntity.cs b/source/game/components/physics/PhysicsEntity.cs
new file mode 100644
index 0000000..7214dff
--- /dev/null
+++ b/source/game/components/physics/PhysicsEntity.cs
@@ -0,0 +1,38 @@
+using Microsoft.Xna.Framework;
+
+namespace Celesteia.Game.Components.Physics {
+ public class PhysicsEntity {
+ public float Mass;
+
+ public bool Gravity;
+
+ public bool CollidingUp;
+ public bool CollidingLeft;
+ public bool CollidingRight;
+ public bool CollidingDown;
+
+ public PhysicsEntity(float mass, bool affectedByGravity) {
+ Mass = mass;
+ Gravity = affectedByGravity;
+ }
+
+ private Vector2 _velocity;
+ public Vector2 Velocity => _velocity;
+
+ public void SetVelocity(Vector2 vector) {
+ _velocity = vector;
+ }
+
+ public void SetVelocity(float x, float y) {
+ SetVelocity(new Vector2(x, y));
+ }
+
+ public void AddVelocity(Vector2 vector) {
+ _velocity += vector;
+ }
+
+ public void AddVelocity(float x, float y) {
+ AddVelocity(new Vector2(x, y));
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/components/player/LocalPlayer.cs b/source/game/components/player/LocalPlayer.cs
new file mode 100644
index 0000000..4780ec1
--- /dev/null
+++ b/source/game/components/player/LocalPlayer.cs
@@ -0,0 +1,5 @@
+namespace Celesteia.Game.Components.Player {
+ public class LocalPlayer {
+ public float JumpRemaining = .5f;
+ }
+} \ No newline at end of file
diff --git a/source/game/components/player/PlayerInput.cs b/source/game/components/player/PlayerInput.cs
new file mode 100644
index 0000000..96f3a57
--- /dev/null
+++ b/source/game/components/player/PlayerInput.cs
@@ -0,0 +1,14 @@
+using Celesteia.Game.Input.Conditions;
+
+namespace Celesteia.Game.Components.Player {
+ public class PlayerInput {
+ public ICondition<float> Horizontal;
+ public ICondition<bool> Run;
+ public ICondition<bool> Jump;
+ public ICondition<bool> Inventory;
+ public ICondition<bool> Crafting;
+ public ICondition<bool> Pause;
+ public ICondition<bool> PrimaryUse;
+ public ICondition<bool> SecondaryUse;
+ }
+} \ No newline at end of file
diff --git a/source/game/components/skybox/SkyboxRotateZ.cs b/source/game/components/skybox/SkyboxRotateZ.cs
new file mode 100644
index 0000000..151aec9
--- /dev/null
+++ b/source/game/components/skybox/SkyboxRotateZ.cs
@@ -0,0 +1,17 @@
+namespace Celesteia.Game.Components.Skybox {
+ public class SkyboxRotateZ {
+ public float _magnitude = 1f;
+ // Amount to rotate by.
+ public float Magnitude { get { return _magnitude; } }
+
+ // Current rotation.
+ public float Current = 0f;
+
+ public SkyboxRotateZ(float magnitude) {
+ _magnitude = magnitude;
+
+ // Offset the starting rotation.
+ Current = _magnitude * 256f;
+ }
+ }
+} \ No newline at end of file