Пример #1
0
        void panelAppear(ref PanelInfo panel, ref Sprite2DRenderer sprite)
        {
            var dt = World.TinyEnvironment().frameDeltaTime;

            panel.Timer += dt;

//			var scl = scale.Value;
//			scl -= new float3( 0.9f * dt, 0.9f * dt, 0 );
//			scale.Value = scl;

            var col = sprite.color;

            col.a = panel.Timer * 2f;
            if (col.a > 1f)
            {
                col.a = 1f;
            }

            if (panel.Timer >= 0.5f)
            {
                panel.Status = PnlStNormal;
                panel.Timer  = 0;
                col.a        = 1f;
            }
            sprite.color = col;
        }
Пример #2
0
        public Entity CreateGold(EntityCommandBuffer ecb, int2 xy, float3 pos)
        {
            Entity entity = ecb.CreateEntity(Gold);

            Sprite2DRenderer s = new Sprite2DRenderer();
            Translation      t = new Translation();
            WorldCoord       c = new WorldCoord();
            LayerSorting     l = new LayerSorting();

            t.Value = pos;

            c.x = xy.x;
            c.y = xy.y;

            // Only tint sprites if ascii
            s.color = GlobalGraphicsSettings.ascii
                ? new Unity.Tiny.Core2D.Color(0.964f, 0.749f, 0.192f)
                : Color.Default;
            if (GlobalGraphicsSettings.ascii)
            {
                s.color.a = 0;
            }

            s.sprite = SpriteSystem.IndexSprites[SpriteSystem.ConvertToGraphics((char)236)];
            l.layer  = 1;

            ecb.SetComponent(entity, s);
            ecb.SetComponent(entity, t);
            ecb.SetComponent(entity, c);
            ecb.SetComponent(entity, l);

            return(entity);
        }
Пример #3
0
        public void ResetPlayer(EntityCommandBuffer cb, Entity player, WorldCoord worldCoord, Translation translation)
        {
            CreatureDescription descr = CreatureDescriptions[(int)ECreatureId.Player];

            Creature c = new Creature {
                id = (int)ECreatureId.Player
            };
            HealthPoints hp = new HealthPoints {
                max = descr.health, now = descr.health
            };
            AttackStat att = new AttackStat {
                range = descr.attackRange
            };
            Level lvl = new Level {
                level = 1
            };
            ExperiencePoints exp = new ExperiencePoints {
                now = 0, next = LevelSystem.GetXPRequiredForLevel(1)
            };
            GoldCount gp = new GoldCount {
                count = 0
            };
            Mobile mobile = new Mobile {
                Destination = new float3(0, 0, 0), Initial = new float3(0, 0, 0), MoveTime = 0, Moving = false
            };
            Animated animated = new Animated {
                Id = descr.spriteId, Direction = Direction.Right, Action = Action.None, AnimationTime = 0, AnimationTrigger = false
            };
            Sight sight = new Sight {
                SightRadius = 4
            };
            TurnPriorityComponent tp = new TurnPriorityComponent {
                Value = -1
            };

            // Only tint sprites if ascii
            Sprite2DRenderer s = new Sprite2DRenderer();
            LayerSorting     l = new LayerSorting {
                order = 2
            };

            s.color  = GlobalGraphicsSettings.ascii ? descr.asciiColor : Color.Default;
            s.sprite = SpriteSystem.IndexSprites[SpriteSystem.ConvertToGraphics(descr.ascii)];
            l.layer  = 2;

            cb.SetComponent(player, s);
            cb.SetComponent(player, c);
            cb.SetComponent(player, l);
            cb.SetComponent(player, hp);
            cb.SetComponent(player, att);
            cb.SetComponent(player, lvl);
            cb.SetComponent(player, exp);
            cb.SetComponent(player, gp);
            cb.SetComponent(player, mobile);
            cb.SetComponent(player, animated);
            cb.SetComponent(player, sight);
            cb.SetComponent(player, worldCoord);
            cb.SetComponent(player, translation);
            cb.SetComponent(player, tp);
        }
Пример #4
0
        static public Entity CreateSquare(Entity image, Color color, float cx = 0, float cy = 0, float cz = 0, float w = 1, float h = 1, float pixelsToWorldUnits = 1f)
        {
            var e = entityManager.CreateEntity();

            entityManager.AddComponent(e, typeof(Parent));
            entityManager.AddComponentData(e, new Translation()
            {
                Value = new float3(cx, cy, cz)
            });
            entityManager.AddComponentData(e, new NonUniformScale()
            {
                Value = new float3(w, h, 1)
            });

            var spr = new Sprite2DRenderer();

            spr.color    = color;
            spr.blending = BlendOp.Alpha;
            entityManager.AddComponentData(e, spr);

            var sp = new Sprite2D();

            sp.imageRegion        = new Rect(0f, 0f, 1f, 1f);
            sp.pivot              = new float2(0.5f, 0.5f);
            sp.image              = image;
            sp.pixelsToWorldUnits = pixelsToWorldUnits;
            entityManager.AddComponentData(e, sp);
            return(e);
        }
Пример #5
0
        public Entity CreateSpearTrap(EntityCommandBuffer ecb, int2 xy, float3 pos)
        {
            Entity entity = ecb.CreateEntity(SpearTrap);

            Sprite2DRenderer s = new Sprite2DRenderer();
            Translation      t = new Translation();
            WorldCoord       c = new WorldCoord();
            LayerSorting     l = new LayerSorting();

            t.Value = pos;

            c.x = xy.x;
            c.y = xy.y;

            // Only tint sprites if ascii
            s.color = TinyRogueConstants.DefaultColor;
            if (GlobalGraphicsSettings.ascii)
            {
                s.color.a = 0;
            }
            s.sprite = SpriteSystem.IndexSprites[SpriteSystem.ConvertToGraphics('^')];
            l.order  = 1;

            ecb.SetComponent(entity, s);
            ecb.SetComponent(entity, t);
            ecb.SetComponent(entity, c);
            ecb.SetComponent(entity, l);

            return(entity);
        }
Пример #6
0
        public Entity CreateTile(EntityManager entityManager, int2 xy, float3 pos, Entity parent)
        {
            Entity           entity = entityManager.CreateEntity(Tile);
            Sprite2DRenderer s      = new Sprite2DRenderer();
            Translation      t      = new Translation();
            Parent           p      = new Parent();
            WorldCoord       c      = new WorldCoord(); // ViewCoord?

            p.Value = parent;
            t.Value = pos;

            c.x = xy.x;
            c.y = xy.y;

            // Only tint sprites if ascii
            s.color = TinyRogueConstants.DefaultColor;
            if (GlobalGraphicsSettings.ascii)
            {
                s.color.a = 0;
            }

            s.sprite = SpriteSystem.IndexSprites[SpriteSystem.ConvertToGraphics(' ')];

            entityManager.SetComponentData(entity, s);
            entityManager.SetComponentData(entity, t);
            entityManager.SetComponentData(entity, p);
            entityManager.SetComponentData(entity, c);

            return(entity);
        }
Пример #7
0
        public Entity SpawnCreature(EntityCommandBuffer cb, ECreatureId cId, int priority)
        {
            Entity entity             = cb.CreateEntity(_creatureArchetype);
            CreatureDescription descr = CreatureDescriptions[(int)cId];

            Sprite2DRenderer s = new Sprite2DRenderer();
            LayerSorting     l = new LayerSorting();
            Creature         c = new Creature {
                id = (int)cId
            };
            HealthPoints hp = new HealthPoints {
                max = descr.health, now = descr.health
            };
            AttackStat att = new AttackStat {
                range = descr.attackRange
            };
            Sight sight = new Sight {
                SightRadius = descr.sightRadius
            };
            PatrollingState     patrol   = new PatrollingState();
            MeleeAttackMovement movement = new MeleeAttackMovement();
            Speed speed = new Speed {
                SpeedRate = descr.speed
            };
            Mobile mobile = new Mobile {
                Destination = new float3(0, 0, 0), Initial = new float3(0, 0, 0), MoveTime = 0, Moving = false
            };
            Animated animated = new Animated {
                Id = descr.spriteId, Direction = Direction.Right, Action = Action.None, AnimationTime = 0, AnimationTrigger = false
            };
            ArmorClass ac = new ArmorClass {
                AC = descr.ac
            };
            TurnPriorityComponent tp = new TurnPriorityComponent {
                Value = priority
            };

            // Only tint sprites if ascii
            s.color   = GlobalGraphicsSettings.ascii ? descr.asciiColor : Color.Default;
            s.color.a = 0.0f; // Start invisible
            s.sprite  = SpriteSystem.IndexSprites[SpriteSystem.ConvertToGraphics(descr.ascii)];
            l.layer   = 2;

            cb.SetComponent(entity, s);
            cb.SetComponent(entity, c);
            cb.SetComponent(entity, l);
            cb.SetComponent(entity, hp);
            cb.SetComponent(entity, att);
            cb.SetComponent(entity, sight);
            cb.SetComponent(entity, movement);
            cb.SetComponent(entity, patrol);
            cb.SetComponent(entity, speed);
            cb.SetComponent(entity, mobile);
            cb.SetComponent(entity, animated);
            cb.SetComponent(entity, ac);

            return(entity);
        }
Пример #8
0
        protected bool CanBatchWith(ref DisplayListEntry e0, ref DisplayListEntry ei)
        {
            var mgr = EntityManager;

            DisplayListEntryType det0 = e0.type;
            DisplayListEntryType deti = ei.type;

            if (det0 != deti)
            {
                return(false);
            }

            // special case tiled and sliced for now, they never batch (can check others as well!)
            switch (det0)
            {
            default:
            case DisplayListEntryType.TiledSprite:
            case DisplayListEntryType.SlicedSprite:
            case DisplayListEntryType.Shape:
            case DisplayListEntryType.GroupOnly:
            case DisplayListEntryType.HitBoxOnly:
            case DisplayListEntryType.Text:
                return(false);

            case DisplayListEntryType.Sprite: {
                // check blend mode
                Sprite2DRenderer sr0 = cachedGetSprite2DRenderer[e0.e];
                Sprite2DRenderer sri = cachedGetSprite2DRenderer[ei.e];
                if (sr0.blending != sri.blending)
                {
                    return(false);
                }
                // if we are depending on a texture, check that those match
                Sprite2D s0 = cachedGetSprite2D[sr0.sprite];
                Sprite2D si = cachedGetSprite2D[sri.sprite];
                if (s0.image != si.image)
                {
                    return(false);
                }
                // good!
                return(true);
            }
            }
        }
Пример #9
0
        public void CreateCollectible(EntityCommandBuffer ecb, int2 xy, float3 pos)
        {
            Entity entity = ecb.CreateEntity(Collectible);

            Sprite2DRenderer s  = new Sprite2DRenderer();
            Translation      t  = new Translation();
            WorldCoord       c  = new WorldCoord();
            LayerSorting     l  = new LayerSorting();
            CanBePickedUp    p  = new CanBePickedUp();
            HealthBonus      hb = new HealthBonus();

            t.Value = pos;

            c.x = xy.x;
            c.y = xy.y;

            // Only tint sprites if ascii
            s.color = GlobalGraphicsSettings.ascii
                ? new Unity.Tiny.Core2D.Color(1, 1, 1)
                : Color.Default;
            if (GlobalGraphicsSettings.ascii)
            {
                s.color.a = 0;
            }

            l.layer = 1;

            p.appearance.color  = s.color;
            p.appearance.sprite = SpriteSystem.IndexSprites[SpriteSystem.ConvertToGraphics('S')];   //defaults

            p.name        = new NativeString64("unknown pickup");
            p.description = new NativeString64("Check collectible gen");

            var collectibleGenSystem = World.Active.GetOrCreateSystem <CollectibleGenSystem>();

            collectibleGenSystem.GetRandomCollectible(ecb, entity, p, hb);
            s.sprite = p.appearance.sprite;

            ecb.SetComponent(entity, s);
            ecb.SetComponent(entity, t);
            ecb.SetComponent(entity, c);
            ecb.SetComponent(entity, l);
            ecb.SetComponent(entity, p);
        }
Пример #10
0
        public Entity CreateHealingItem(EntityCommandBuffer ecb, int2 xy, float3 pos, int healAmount)
        {
            Entity entity = ecb.CreateEntity(HealingPotion);

            HealItem         heal = new HealItem();
            Sprite2DRenderer s    = new Sprite2DRenderer();
            Translation      t    = new Translation();
            WorldCoord       c    = new WorldCoord();
            LayerSorting     l    = new LayerSorting();

            t.Value = pos;

            c.x = xy.x;
            c.y = xy.y;

            // Only tint sprites if ascii
            s.color = GlobalGraphicsSettings.ascii
                ? new Unity.Tiny.Core2D.Color(1.0f, 0.26f, 0.23f)
                : Color.Default;
            if (GlobalGraphicsSettings.ascii)
            {
                s.color.a = 0;
            }

            s.sprite = SpriteSystem.IndexSprites[SpriteSystem.ConvertToGraphics((char)235)];
            l.layer  = 1;

            heal.HealAmount = healAmount;

            ecb.SetComponent(entity, s);
            ecb.SetComponent(entity, t);
            ecb.SetComponent(entity, c);
            ecb.SetComponent(entity, l);
            ecb.SetComponent(entity, heal);

            return(entity);
        }
Пример #11
0
        public Entity CreateDoorway(EntityCommandBuffer ecb, int2 xy, float3 pos, bool horizontal)
        {
            Entity entity = ecb.CreateEntity(Doorway);

            Sprite2DRenderer s = new Sprite2DRenderer();
            Translation      t = new Translation();
            WorldCoord       c = new WorldCoord();
            LayerSorting     l = new LayerSorting();
            Door             d = new Door();

            d.Horizontal = horizontal;
            t.Value      = pos;

            c.x = xy.x;
            c.y = xy.y;

            // Only tint sprites if ascii
            s.color = GlobalGraphicsSettings.ascii
                ? new Unity.Tiny.Core2D.Color(18 / 255.0f, 222 / 255.0f, 23.0f / 255.0f)
                : Color.Default;
            if (GlobalGraphicsSettings.ascii)
            {
                s.color.a = 0;
            }

            s.sprite = SpriteSystem.IndexSprites[SpriteSystem.ConvertToGraphics(horizontal ? '\\' : '/')];
            // Have to draw above character in graphical
            l.layer = (short)(GlobalGraphicsSettings.ascii ? 1 : 3);

            ecb.SetComponent(entity, s);
            ecb.SetComponent(entity, t);
            ecb.SetComponent(entity, c);
            ecb.SetComponent(entity, l);
            ecb.SetComponent(entity, d);

            return(entity);
        }
Пример #12
0
        bool panelDisapper(ref PanelInfo panel, ref NonUniformScale scale, ref Sprite2DRenderer sprite)
        {
            //Debug.LogAlways("disapp");
            var dt = World.TinyEnvironment().frameDeltaTime;

            panel.Timer += dt;

            var scl = scale.Value;

            scl        -= new float3(1.9f * dt, 1.9f * dt, 0);
            scale.Value = scl;

            var col = sprite.color;

            col.a       -= 1.9f * dt;
            sprite.color = col;


            if (panel.Timer >= 0.5f)
            {
                return(true);
            }
            return(false);
        }
Пример #13
0
        static public Entity CreateSpriteRect(Entity image, Color color, int x0 = -1, int y0 = -1, int x1 = -1, int y1 = -1, int w = -1, int h = -1, float pixelsToWorldUnits = 1f)
        {
            var e = entityManager.CreateEntity();

            entityManager.AddComponent(e, typeof(Parent));

            var spr = new Sprite2DRenderer();

            spr.color    = color;
            spr.blending = BlendOp.Alpha;
            entityManager.AddComponentData(e, spr);

            var sp = new Sprite2D();

            sp.imageRegion = x0 >= 0 ? PixelRect(x0, y0, x1, y1, w, h) : new Rect()
            {
                x = 0, y = 0, width = 1, height = 1
            };
            sp.pivot = new float2(0.5f, 0.5f);
            sp.image = image;
            sp.pixelsToWorldUnits = pixelsToWorldUnits;
            entityManager.AddComponentData(e, sp);
            return(e);
        }
Пример #14
0
        protected override void OnUpdate()
        {
            var gss = EntityManager.World.GetExistingSystem <GameStateSystem>();

            if (gss.IsInGame)
            {
                Entities.ForEach((Entity creature, ref HealthPoints hp, ref Sprite2DRenderer renderer, ref Translation pos, ref Animated animated) =>
                {
                    if (hp.now <= 0)
                    {
                        if (GlobalGraphicsSettings.ascii)
                        {
                            if (EntityManager.HasComponent(creature, typeof(Player)))
                            {
                                gss.MoveToGameOver(PostUpdateCommands);
                                pos.Value = TinyRogueConstants.OffViewport;
                            }
                            else
                            {
                                PostUpdateCommands.DestroyEntity(creature);
                            }
                        }
                        else
                        {
                            var anim = EntityManager.World.GetExistingSystem <AnimationSystem>();

                            if (EntityManager.HasComponent(creature, typeof(Player)))
                            {
                                var player         = EntityManager.GetComponentData <Player>(creature);
                                var sequencePlayer = EntityManager.GetComponentData <Sprite2DSequencePlayer>(creature);

                                player.Dead               = true;
                                sequencePlayer.speed      = 0.5f;
                                animated.AnimationTime    = 0.75f;
                                animated.Action           = Action.Die;
                                animated.AnimationTrigger = true;

                                anim.SetAnimation(ref animated, ref sequencePlayer);

                                PostUpdateCommands.SetComponent(creature, player);
                                PostUpdateCommands.SetComponent(creature, animated);
                                PostUpdateCommands.SetComponent(creature, sequencePlayer);
                            }
                            else
                            {
                                Entity death  = PostUpdateCommands.CreateEntity();
                                Parent parent = new Parent();

                                Translation trans = pos;
                                Sprite2DRenderer deathRenderer = new Sprite2DRenderer {
                                    color = TinyRogueConstants.DefaultColor
                                };
                                Sprite2DSequencePlayer deathPlayer = new Sprite2DSequencePlayer {
                                    speed = 0.5f
                                };
                                Animated deathAnimated = new Animated {
                                    Id = animated.Id, Direction = animated.Direction, Action = Action.Die, AnimationTime = 0.75f, AnimationTrigger = true
                                };
                                LayerSorting layerSorting = new LayerSorting {
                                    layer = 2
                                };

                                anim.SetAnimation(ref deathAnimated, ref deathPlayer);

                                PostUpdateCommands.AddComponent(death, parent);
                                PostUpdateCommands.AddComponent(death, trans);
                                PostUpdateCommands.AddComponent(death, deathRenderer);
                                PostUpdateCommands.AddComponent(death, deathPlayer);
                                PostUpdateCommands.AddComponent(death, deathAnimated);
                                PostUpdateCommands.AddComponent(death, layerSorting);

                                PostUpdateCommands.DestroyEntity(creature);
                            }
                        }
                    }
                });
            }
        }
Пример #15
0
        protected override JobHandle OnUpdate(JobHandle inputDependencies)
        {
            if (_cachedMapSize.x != _gss.View.Width || _cachedMapSize.y != _gss.View.Height)
            {
                ResizeMaps(_gss.View.Width, _gss.View.Height);
            }

            var clearJob = new ClearMapsJob()
            {
                FlagsMap  = _flagMap,
                EntityMap = _entityMap
            };

            var clearJobHandle = clearJob.Schedule(inputDependencies);

            var fillJob = new FillMapsJob()
            {
                MapSize             = _cachedMapSize,
                FlagsMap            = _flagMap,
                EntityMap           = _entityMap,
                EntityType          = GetArchetypeChunkEntityType(),
                WorldCoordType      = GetArchetypeChunkComponentType <WorldCoord>(true),
                BlockedMovementType = GetArchetypeChunkComponentType <BlockMovement>(true),
                DoorType            = GetArchetypeChunkComponentType <Door>(true),
                HostileType         = GetArchetypeChunkComponentType <tag_Attackable>(true),
                PlayerType          = GetArchetypeChunkComponentType <Player>(true)
            };
            var fillJobHandle = fillJob.Schedule(_mapFillQuery, clearJobHandle);

            var pendingMoves        = new NativeQueue <PendingMove>(Allocator.TempJob);
            var pendingWaits        = new NativeQueue <PendingWait>(Allocator.TempJob);
            var pendingAttacks      = new NativeQueue <PendingAttack>(Allocator.TempJob);
            var pendingOpens        = new NativeQueue <PendingDoorOpen>(Allocator.TempJob);
            var pendingInteractions = new NativeQueue <PendingInteractions>(Allocator.TempJob);

            var actionJob = new ConsumeActionsJob()
            {
                MapSize             = _cachedMapSize,
                ActionQueue         = _tms.ActionQueue,
                FlagsMap            = _flagMap,
                EntityMap           = _entityMap,
                PendingMoves        = pendingMoves,
                PendingWaits        = pendingWaits,
                PendingAttacks      = pendingAttacks,
                PendingOpens        = pendingOpens,
                PendingInteractions = pendingInteractions
            };
            var actionJobHandle = actionJob.Schedule(fillJobHandle);

            actionJobHandle.Complete();

            // TODO: Jobify?
            var         log = EntityManager.World.GetExistingSystem <LogSystem>();
            PendingMove pm;

            while (pendingMoves.TryDequeue(out pm))
            {
                var trans = _gss.View.ViewCoordToWorldPos(new int2(pm.Wc.x, pm.Wc.y));
                if (GlobalGraphicsSettings.ascii)
                {
                    EntityManager.SetComponentData(pm.Ent, new Translation {
                        Value = trans
                    });
                }
                else
                {
                    var anim   = EntityManager.World.GetExistingSystem <AnimationSystem>();
                    var mobile = EntityManager.GetComponentData <Mobile>(pm.Ent);
                    mobile.Initial     = EntityManager.GetComponentData <Translation>(pm.Ent).Value;
                    mobile.Destination = trans;
                    EntityManager.SetComponentData(pm.Ent, mobile);
                    anim.StartAnimation(pm.Ent, Action.Move, pm.Dir);
                    _inv.LogItemsAt(pm.Wc);
                }
                EntityManager.SetComponentData(pm.Ent, pm.Wc);
            }

            PendingWait pw;

            while (pendingWaits.TryDequeue(out pw))
            {
                if (EntityManager.HasComponent <Player>(pw.Ent))
                {
                    log.AddLog(pw.Ouch ? "You bumped into a wall. Ouch." : "You wait a turn.");
                }

                var anim = EntityManager.World.GetExistingSystem <AnimationSystem>();
                anim.StartAnimation(pw.Ent, pw.Ouch ? Action.Bump : Action.Wait, pw.Dir);
            }

            int pendingAttackCount = pendingAttacks.Count;

            if (pendingAttackCount > 0)
            {
                NativeArray <PendingAttack> sortedPendingAttacks =
                    new NativeArray <PendingAttack>(pendingAttackCount, Allocator.Temp);
                for (int i = 0; i < pendingAttackCount; i++)
                {
                    sortedPendingAttacks[i] = pendingAttacks.Dequeue();
                }

                sortedPendingAttacks.Sort(new PendingAttackComparer());
                for (int i = 0; i < pendingAttackCount; i++)
                {
                    PendingAttack pa         = sortedPendingAttacks[i];
                    AttackStat    att        = EntityManager.GetComponentData <AttackStat>(pa.Attacker);
                    Creature      attacker   = EntityManager.GetComponentData <Creature>(pa.Attacker);
                    HealthPoints  hp         = EntityManager.GetComponentData <HealthPoints>(pa.Defender);
                    Creature      defender   = EntityManager.GetComponentData <Creature>(pa.Defender);
                    HealthPoints  attackerHp = EntityManager.GetComponentData <HealthPoints>(pa.Attacker);
                    ArmorClass    defAC      = EntityManager.GetComponentData <ArmorClass>(pa.Defender);

                    if (attackerHp.now <= 0) // don't let the dead attack, is this hack? Maybe.
                    {
                        continue;
                    }
                    string attackerName = CreatureLibrary.CreatureDescriptions[attacker.id].name;
                    string defenderName = CreatureLibrary.CreatureDescriptions[defender.id].name;

                    // Play animation even if the creature misses
                    var anim = EntityManager.World.GetExistingSystem <AnimationSystem>();
                    anim.StartAnimation(pa.Attacker, Action.Attack, pa.AttackerDir);

                    if (DiceRoller.Roll(1, 20, 0) >= defAC.AC)
                    {
                        int  dmg      = RandomRogue.Next(att.range.x, att.range.y);
                        bool firstHit = hp.now == hp.max;

                        hp.now -= dmg;

                        bool playerAttack = attackerName == "Player";
                        bool killHit      = hp.now <= 0;

                        string logStr;

                        if (playerAttack && killHit && firstHit)
                        {
                            logStr = string.Concat("You destroy the ", defenderName);
                            logStr = string.Concat(logStr, ".");
                            ExperiencePoints xp = EntityManager.GetComponentData <ExperiencePoints>(pa.Attacker);
                            xp.now += hp.max; //XP awarded equals the defenders max hp
                            EntityManager.SetComponentData(pa.Attacker, xp);
                        }
                        else if (playerAttack)
                        {
                            logStr = string.Concat(string.Concat(string.Concat(string.Concat(
                                                                                   "You hit the ",
                                                                                   defenderName),
                                                                               " for "),
                                                                 dmg.ToString()),
                                                   " damage!");

                            if (killHit)
                            {
                                logStr = string.Concat(logStr, " Killing it.");
                                ExperiencePoints xp = EntityManager.GetComponentData <ExperiencePoints>(pa.Attacker);
                                xp.now += hp.max; //XP awarded equals the defenders max hp
                                EntityManager.SetComponentData(pa.Attacker, xp);
                            }
                        }
                        else
                        {
                            bool playerHit = false;
                            if (defenderName == "Player")
                            {
                                defenderName = "you";
                                playerHit    = true;
                            }

                            logStr = string.Concat(string.Concat(string.Concat(string.Concat(string.Concat(
                                                                                                 attackerName,
                                                                                                 " hits "),
                                                                                             defenderName),
                                                                               " for "),
                                                                 dmg.ToString()),
                                                   " damage!");

                            if (playerHit)
                            {
                                _gss.LastPlayerHurtLog = logStr;
                            }
                        }

                        log.AddLog(logStr);

                        EntityManager.SetComponentData(pa.Defender, hp);
                    }
                    else
                    {
                        string logStr = attackerName;
                        logStr = string.Concat(logStr, " swings at ");
                        logStr = string.Concat(logStr, defenderName);
                        logStr = string.Concat(logStr, ".  But missed!");

                        log.AddLog(logStr);
                    }
                }

                sortedPendingAttacks.Dispose();
            }

            PendingDoorOpen pd;

            while (pendingOpens.TryDequeue(out pd))
            {
                if (EntityManager.HasComponent <Player>(pd.OpeningEntity))
                {
                    log.AddLog("You opened a door.");
                }
                Sprite2DRenderer s = EntityManager.GetComponentData <Sprite2DRenderer>(pd.DoorEnt);
                var door           = EntityManager.GetComponentData <Door>(pd.DoorEnt);
                door.Locked = false;
                door.Opened = true;
                EntityManager.RemoveComponent(pd.DoorEnt, typeof(BlockMovement));
                EntityManager.SetComponentData(pd.DoorEnt, door);
                EntityManager.SetComponentData(pd.DoorEnt, s);
            }

            PendingInteractions pi;

            while (pendingInteractions.TryDequeue(out pi))
            {
                using (var entities = EntityManager.GetAllEntities(Allocator.TempJob))
                {
                    foreach (Entity e in entities)
                    {
                        if (EntityManager.HasComponent(e, typeof(WorldCoord)) &&
                            EntityManager.HasComponent(e, typeof(Collectible)))
                        {
                            WorldCoord coord = EntityManager.GetComponentData <WorldCoord>(e);
                            int2       ePos  = new int2(coord.x, coord.y);

                            if (pi.InteractPos.x == ePos.x && pi.InteractPos.y == ePos.y)
                            {
                                _inv.CollectItemsAt(new EntityCommandBuffer(Allocator.TempJob), coord);
                            }
                        }

                        if (EntityManager.HasComponent(e, typeof(WorldCoord)) &&
                            EntityManager.HasComponent(e, typeof(Stairway)))
                        {
                            WorldCoord coord = EntityManager.GetComponentData <WorldCoord>(e);
                            int2       ePos  = new int2(coord.x, coord.y);

                            if (pi.InteractPos.x == ePos.x && pi.InteractPos.y == ePos.y)
                            {
                                if (EntityManager.HasComponent(e, typeof(Stairway)))
                                {
                                    _gss.MoveToNextLevel(new EntityCommandBuffer(Allocator.TempJob));
                                }
                            }
                        }
                    }
                }
            }

            // Cleanup
            pendingMoves.Dispose();
            pendingAttacks.Dispose();
            pendingWaits.Dispose();
            pendingOpens.Dispose();
            pendingInteractions.Dispose();

            return(actionJobHandle);
        }
Пример #16
0
        public Entity SpawnPlayer(EntityManager entityManager)
        {
            Entity entity             = entityManager.CreateEntity(_playerArchetype);
            CreatureDescription descr = CreatureDescriptions[(int)ECreatureId.Player];

            Creature c = new Creature {
                id = (int)ECreatureId.Player
            };
            HealthPoints hp = new HealthPoints {
                max = descr.health, now = descr.health
            };
            AttackStat att = new AttackStat {
                range = descr.attackRange
            };
            Level lvl = new Level {
                level = 1
            };
            ExperiencePoints exp = new ExperiencePoints {
                now = 0, next = LevelSystem.GetXPRequiredForLevel(1)
            };
            GoldCount gp = new GoldCount {
                count = 0
            };
            Mobile mobile = new Mobile {
                Destination = new float3(0, 0, 0), Initial = new float3(0, 0, 0), MoveTime = 0, Moving = false
            };
            Animated animated = new Animated {
                Id = descr.spriteId, Direction = Direction.Right, Action = Action.None, AnimationTime = 0, AnimationTrigger = false
            };
            Sight sight = new Sight {
                SightRadius = 4
            };
            ArmorClass ac = new ArmorClass {
                AC = descr.ac
            };
            TurnPriorityComponent tp = new TurnPriorityComponent {
                Value = -1
            };

            // Only tint sprites if ascii
            Sprite2DRenderer s = new Sprite2DRenderer();
            LayerSorting     l = new LayerSorting {
                order = 2
            };

            s.color  = GlobalGraphicsSettings.ascii ? descr.asciiColor : Color.Default;
            s.sprite = SpriteSystem.IndexSprites[SpriteSystem.ConvertToGraphics(descr.ascii)];
            l.layer  = 2;

            entityManager.SetComponentData(entity, s);
            entityManager.SetComponentData(entity, c);
            entityManager.SetComponentData(entity, l);
            entityManager.SetComponentData(entity, hp);
            entityManager.SetComponentData(entity, att);
            entityManager.SetComponentData(entity, lvl);
            entityManager.SetComponentData(entity, exp);
            entityManager.SetComponentData(entity, gp);
            entityManager.SetComponentData(entity, mobile);
            entityManager.SetComponentData(entity, animated);
            entityManager.SetComponentData(entity, sight);
            entityManager.SetComponentData(entity, ac);
            entityManager.SetComponentData(entity, tp);

            return(entity);
        }