summaryrefslogtreecommitdiff
path: root/source/game/items/BlockItemActions.cs
blob: 94b6c6791dc043e4ff0456339a5265d0abe6fe88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
        }
    }
}