aboutsummaryrefslogtreecommitdiff
path: root/source/game/items
diff options
context:
space:
mode:
Diffstat (limited to 'source/game/items')
-rw-r--r--source/game/items/BlockItemActions.cs73
-rw-r--r--source/game/items/CooldownItemActions.cs17
-rw-r--r--source/game/items/FoliageItemActions.cs18
-rw-r--r--source/game/items/IItemActions.cs11
-rw-r--r--source/game/items/PickaxeItemActions.cs63
-rw-r--r--source/game/items/TorchItemActions.cs15
-rw-r--r--source/game/items/UpgradeItemActions.cs48
7 files changed, 245 insertions, 0 deletions
diff --git a/source/game/items/BlockItemActions.cs b/source/game/items/BlockItemActions.cs
new file mode 100644
index 0000000..94b6c67
--- /dev/null
+++ b/source/game/items/BlockItemActions.cs
@@ -0,0 +1,73 @@
+using Celesteia.Game.Components;
+using Celesteia.Game.Components.Physics;
+using Celesteia.Game.Planets;
+using Celesteia.Resources;
+using Celesteia.Resources.Types;
+using Microsoft.Xna.Framework;
+using MonoGame.Extended;
+using MonoGame.Extended.Entities;
+
+namespace Celesteia.Game.Items {
+ public class BlockItemActions : CooldownItemActions {
+ protected BlockType type;
+ protected byte id = 0;
+
+ public BlockItemActions(NamespacedKey blockKey) {
+ UseTime = 0;
+ type = ResourceManager.Blocks.GetResource(blockKey) as BlockType;
+ id = type.GetID();
+ }
+
+ public override bool Primary(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user)
+ => Assert(gameTime, chunkMap, cursor, user, false) && Place(chunkMap, cursor, false);
+
+ public override bool Secondary(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user)
+ => Assert(gameTime, chunkMap, cursor, user, true) && Place(chunkMap, cursor, true);
+
+ public virtual bool Assert(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user, bool forWall) {
+ if (!base.Assert(gameTime)) return false;
+ if (id == 0) return false;
+
+ UpdateLastUse(gameTime);
+
+ if (!user.Has<Transform2>() || !user.Has<EntityAttributes>()) return false;
+
+ Transform2 entityTransform = user.Get<Transform2>();
+ EntityAttributes.EntityAttributeMap attributes = user.Get<EntityAttributes>().Attributes;
+
+ if (Vector2.Distance(entityTransform.Position, cursor.ToVector2()) > attributes.Get(EntityAttribute.BlockRange)) return false;
+
+ if (!forWall && user.Has<CollisionBox>()) {
+ Rectangle box = user.Get<CollisionBox>().Rounded;
+ RectangleF? rect = chunkMap.TestBoundingBox(cursor.X, cursor.Y, type.BoundingBox);
+ if (rect.HasValue) {
+ bool intersect = rect.Intersects(new RectangleF(box.X, box.Y, box.Width, box.Height));
+ if (intersect) return false;
+ }
+ }
+
+ if (!(forWall ?
+ chunkMap.GetBackground(cursor.X, cursor.Y) :
+ chunkMap.GetForeground(cursor.X, cursor.Y))
+ .Empty) return false;
+
+ // Require adjacent blocks or a filled tile on the other layer.
+ return (
+ chunkMap.GetAny(cursor.X - 1, cursor.Y) ||
+ chunkMap.GetAny(cursor.X + 1, cursor.Y) ||
+ chunkMap.GetAny(cursor.X, cursor.Y - 1) ||
+ chunkMap.GetAny(cursor.X, cursor.Y + 1)
+ ) || (forWall ?
+ chunkMap.GetForeground(cursor.X, cursor.Y) :
+ chunkMap.GetBackground(cursor.X, cursor.Y))
+ .Empty;
+ }
+
+ public bool Place(ChunkMap chunkMap, Point cursor, bool wall) {
+ if (wall) chunkMap.SetBackgroundID(cursor, id);
+ else chunkMap.SetForegroundID(cursor, id);
+
+ return true;
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/items/CooldownItemActions.cs b/source/game/items/CooldownItemActions.cs
new file mode 100644
index 0000000..647661e
--- /dev/null
+++ b/source/game/items/CooldownItemActions.cs
@@ -0,0 +1,17 @@
+using Celesteia.Game.Planets;
+using Microsoft.Xna.Framework;
+using MonoGame.Extended.Entities;
+
+namespace Celesteia.Game.Items {
+ public class CooldownItemActions : IItemActions {
+ public double UseTime = 1.0;
+ public double LastUse = 0.0;
+ public void UpdateLastUse(GameTime gameTime) => LastUse = gameTime.TotalGameTime.TotalSeconds;
+ public bool CheckUseTime(GameTime gameTime) => gameTime.TotalGameTime.TotalSeconds - LastUse >= UseTime;
+
+ public virtual bool Assert(GameTime gameTime) => CheckUseTime(gameTime);
+
+ public virtual bool Primary(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user) => false;
+ public virtual bool Secondary(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user) => false;
+ }
+} \ No newline at end of file
diff --git a/source/game/items/FoliageItemActions.cs b/source/game/items/FoliageItemActions.cs
new file mode 100644
index 0000000..54bde6d
--- /dev/null
+++ b/source/game/items/FoliageItemActions.cs
@@ -0,0 +1,18 @@
+using Celesteia.Game.Planets;
+using Celesteia.Resources;
+using Microsoft.Xna.Framework;
+using MonoGame.Extended.Entities;
+
+namespace Celesteia.Game.Items {
+ public class FoliageItemActions : BlockItemActions {
+ byte grown_soil;
+ public FoliageItemActions(NamespacedKey blockKey) : base(blockKey) {
+ grown_soil = ResourceManager.Blocks.GetResource(NamespacedKey.Base("grown_soil")).GetID();
+ }
+
+ public override bool Secondary(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user) => false;
+
+ public override bool Assert(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user, bool forWall)
+ => chunkMap.GetForeground(cursor.X, cursor.Y + 1).BlockID == grown_soil && base.Assert(gameTime, chunkMap, cursor, user, false);
+ }
+} \ No newline at end of file
diff --git a/source/game/items/IItemActions.cs b/source/game/items/IItemActions.cs
new file mode 100644
index 0000000..5a95f06
--- /dev/null
+++ b/source/game/items/IItemActions.cs
@@ -0,0 +1,11 @@
+using Celesteia.Game.Planets;
+using Microsoft.Xna.Framework;
+using MonoGame.Extended.Entities;
+
+namespace Celesteia.Game.Items {
+ public interface IItemActions {
+
+ public bool Primary(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user);
+ public bool Secondary(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user);
+ }
+} \ No newline at end of file
diff --git a/source/game/items/PickaxeItemActions.cs b/source/game/items/PickaxeItemActions.cs
new file mode 100644
index 0000000..56e2714
--- /dev/null
+++ b/source/game/items/PickaxeItemActions.cs
@@ -0,0 +1,63 @@
+using Celesteia.Game.Components;
+using Celesteia.Game.Components.Items;
+using Celesteia.Game.Planets;
+using Celesteia.Resources;
+using Microsoft.Xna.Framework;
+using MonoGame.Extended;
+using MonoGame.Extended.Entities;
+
+namespace Celesteia.Game.Items {
+ public class PickaxeItemActions : CooldownItemActions {
+ private int _power;
+
+ public PickaxeItemActions(int power) {
+ UseTime = 0.2;
+ _power = power;
+ }
+
+ public override bool Primary(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user)
+ => Assert(gameTime, chunkMap, cursor, user, false) && Break(chunkMap, cursor, user, false);
+
+ public override bool Secondary(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user)
+ => Assert(gameTime, chunkMap, cursor, user, true) && Break(chunkMap, cursor, user, true);
+
+ // Check if the conditions to use this item's action are met.
+ public bool Assert(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user, bool forWall) {
+ if (!base.Assert(gameTime)) return false;
+
+ // If the user has no transform or attributes, the rest of the function will not work, so check if they're there first.
+ if (!user.Has<Transform2>() || !user.Has<EntityAttributes>()) return false;
+
+ Transform2 entityTransform = user.Get<Transform2>();
+ EntityAttributes.EntityAttributeMap attributes = user.Get<EntityAttributes>().Attributes;
+
+ // Check if the targeted location is within the entity's block manipulation range.
+ if (Vector2.Distance(entityTransform.Position, cursor.ToVector2()) > attributes.Get(EntityAttribute.BlockRange)) return false;
+
+ BlockState state = (forWall ? chunkMap.GetBackground(cursor) : chunkMap.GetForeground(cursor));
+
+ // If there is no tile in the given location, the action will not continue.
+ if (state.Empty) return false;
+
+ // If the block is unbreakable, don't break it. Duh.
+ if (state.Type.Strength < 0) return false;
+
+ UpdateLastUse(gameTime);
+
+ return true;
+ }
+
+
+ public bool Break(ChunkMap chunkMap, Point cursor, Entity user, bool wall) {
+ // Add breaking progress accordingly.
+ ItemStack drops = null;
+ if (wall) chunkMap.AddBackgroundBreakProgress(cursor, _power, out drops);
+ else chunkMap.AddForegroundBreakProgress(cursor, _power, out drops);
+
+ if (drops != null && user.Has<Inventory>())
+ user.Get<Inventory>().AddItem(drops);
+
+ return true;
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/items/TorchItemActions.cs b/source/game/items/TorchItemActions.cs
new file mode 100644
index 0000000..12eb8ed
--- /dev/null
+++ b/source/game/items/TorchItemActions.cs
@@ -0,0 +1,15 @@
+using Celesteia.Game.Planets;
+using Celesteia.Resources;
+using Microsoft.Xna.Framework;
+using MonoGame.Extended.Entities;
+
+namespace Celesteia.Game.Items {
+ public class TorchItemActions : BlockItemActions {
+ public TorchItemActions(NamespacedKey blockKey) : base(blockKey) {}
+
+ public override bool Secondary(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user) => false;
+
+ public override bool Assert(GameTime g, ChunkMap cm, Point c, Entity u, bool wa = false)
+ => !cm.GetBackground(c).Empty && base.Assert(g, cm, c, u, false);
+ }
+} \ No newline at end of file
diff --git a/source/game/items/UpgradeItemActions.cs b/source/game/items/UpgradeItemActions.cs
new file mode 100644
index 0000000..18d922d
--- /dev/null
+++ b/source/game/items/UpgradeItemActions.cs
@@ -0,0 +1,48 @@
+using System;
+using Celesteia.Game.Components;
+using Celesteia.Game.Planets;
+using Microsoft.Xna.Framework;
+using MonoGame.Extended.Entities;
+
+namespace Celesteia.Game.Items {
+ public class UpgradeItemActions : CooldownItemActions {
+ private float _increase;
+ private EntityAttribute _attribute;
+ private float _max;
+
+ public UpgradeItemActions(EntityAttribute attribute, float increase, float max) {
+ UseTime = 0.2;
+ _attribute = attribute;
+ _increase = increase;
+ _max = max;
+ }
+
+ public override bool Primary(GameTime gameTime, ChunkMap chunkMap, Point cursor, Entity user) => Assert(gameTime, user) && Use(user);
+
+ // Check if the conditions to use this item's action are met.
+ public bool Assert(GameTime gameTime, Entity user) {
+ if (!base.Assert(gameTime)) return false;
+
+ // If the user has no attributes, the rest of the function will not work, so check if they're there first.
+ if (!user.Has<EntityAttributes>()) return false;
+
+ EntityAttributes.EntityAttributeMap attributes = user.Get<EntityAttributes>().Attributes;
+
+ // If the attribute is maxed out, don't upgrade.
+ if (attributes.Get(_attribute) >= _max) return false;
+
+ UpdateLastUse(gameTime);
+
+ return true;
+ }
+
+
+ public bool Use(Entity user) {
+ // Use the upgrade.
+ EntityAttributes.EntityAttributeMap attributes = user.Get<EntityAttributes>().Attributes;
+ attributes.Set(_attribute, Math.Clamp(attributes.Get(_attribute) + _increase, 0f, _max));
+
+ return true;
+ }
+ }
+} \ No newline at end of file