コード例 #1
0
ファイル: EntityFactory.cs プロジェクト: Grufferz/ECS-Test
        public static EntityReturner CreateTroll(int xPos, int yPos, string name, DungeonMap m)
        {
            List <Components.Component> compList = new List <Components.Component>();
            //stats
            int lev     = 2;
            int size    = 8 + RogueSharp.DiceNotation.Dice.Roll("1d3");
            int intBase = 1;

            // set bitwise to 0
            int checker = 0;

            Components.PositionComp positionComp = new Components.PositionComp(xPos, yPos);
            compList.Add(positionComp);
            checker = checker | (int)ComponentTypes.Position;

            Components.RenderComp rendComp = new Components.RenderComp('T', Core.Colours.OrcColour);
            compList.Add(rendComp);
            checker = checker | (int)ComponentTypes.Render;

            Components.AttributesComp attComp = new Components.AttributesComp(10, size, lev, intBase);
            compList.Add(attComp);
            checker = checker | (int)ComponentTypes.Attributes;

            //get base factor for health
            int hp = ((int)(size + attComp.Hardiness) / 2) * lev;

            Components.HealthComp healthComp = new Components.HealthComp(hp);
            compList.Add(healthComp);
            checker = checker | (int)ComponentTypes.Health;

            Components.ActorComp actComp = new Components.ActorComp();
            compList.Add(actComp);
            checker = checker | (int)ComponentTypes.Actor;

            Components.AIComp aiComp = new Components.AIComp(m, Types.AITypes.Creature, new RogueSharp.Point(xPos, yPos));
            compList.Add(aiComp);
            checker = checker | (int)ComponentTypes.AI;

            int speed = Game.Random.Next(3) + 6;

            Components.SchedulableComp shedComp = new Components.SchedulableComp(speed);
            compList.Add(shedComp);
            checker = checker | (int)ComponentTypes.Schedulable;

            Components.InventoryComp invComp = new Components.InventoryComp();
            compList.Add(invComp);
            checker = checker | (int)ComponentTypes.Inventory;

            Components.CreatureDetailsComp detailsComp
                = new Components.CreatureDetailsComp("Troll", name, Types.CreatureTypes.Troll);
            compList.Add(detailsComp);
            checker = checker | (int)ComponentTypes.CreatureDetails;

            EntityReturner er = new EntityReturner(checker, compList);

            return(er);
        }
コード例 #2
0
ファイル: EntityFactory.cs プロジェクト: Grufferz/ECS-Test
        public static EntityReturner CreateZombie(int xPos, int yPos, string name, DungeonMap m)
        {
            List <Components.Component> compList = new List <Components.Component>();
            //stats
            int lev     = 2;
            int size    = 4 + RogueSharp.DiceNotation.Dice.Roll("1d3");
            int intBase = 1;

            // set bitwise to 0
            int checker = 0;

            Components.PositionComp positionComp = new Components.PositionComp(xPos, yPos);
            compList.Add(positionComp);
            checker = checker | (int)ComponentTypes.Position;

            Components.RenderComp rendComp = new Components.RenderComp('z', RLNET.RLColor.Cyan);
            compList.Add(rendComp);
            checker = checker | (int)ComponentTypes.Render;

            Components.AttributesComp attComp = new Components.AttributesComp(12, size, lev, intBase);
            compList.Add(attComp);
            checker = checker | (int)ComponentTypes.Attributes;

            //get base factor for health
            int hp = ((int)(size + attComp.Hardiness) / 2) * lev;

            Components.HealthComp healthComp = new Components.HealthComp(hp);
            compList.Add(healthComp);
            checker = checker | (int)ComponentTypes.Health;

            Components.ActorComp actComp = new Components.ActorComp();
            compList.Add(actComp);
            checker = checker | (int)ComponentTypes.Actor;

            Components.AIComp aiComp = new Components.AIComp(m, Types.AITypes.Undead, new RogueSharp.Point(xPos, yPos));
            compList.Add(aiComp);
            checker = checker | (int)ComponentTypes.AI;

            int speed = RogueSharp.DiceNotation.Dice.Roll("1d6") + 4;

            Components.SchedulableComp shedComp = new Components.SchedulableComp(speed);
            compList.Add(shedComp);
            checker = checker | (int)ComponentTypes.Schedulable;

            Components.CreatureDetailsComp detailsComp
                = new Components.CreatureDetailsComp("Zombie", name, Types.CreatureTypes.Zombie);
            compList.Add(detailsComp);
            checker = checker | (int)ComponentTypes.CreatureDetails;

            EntityReturner er = new EntityReturner(checker, compList);

            return(er);
        }
コード例 #3
0
        public void OnMessage(Core.MessageEventArgs e)
        {
            switch (e.MessageType)
            {
            case Core.EventTypes.CollisionCheck:

                Core.CollisionEventArgs details = (Core.CollisionEventArgs)e;
                //check entity manager lookup dict
                //string posKey = details.x.ToString() + "-" + details.y.ToString();
                bool       collision   = false;
                bool       openingDoor = false;
                List <int> deathList   = new List <int>();

                //if (_entityManager.EntityPostionLookUp.ContainsKey(posKey))

                int entIDRequestingMove = details.EntRequestingMove.UID;

                HashSet <int> innerIDS = _entityManager.Positions[details.y, details.x];
                foreach (int ids in innerIDS)
                {
                    // we have something there...

                    //List<Core.Entity> entL = _entityManager.EntityPostionLookUp[posKey];
                    Components.PositionComp pc
                        = (Components.PositionComp)_entityManager.GetSingleComponentByID(ids, Core.ComponentTypes.Position);

                    if (pc.X == details.x && pc.Y == details.y)
                    {
                        int checker = _entityManager.EntityBitLookUp[ids];
                        // is it a door?
                        if ((checker & (int)Core.ComponentTypes.Door) > 0)
                        {
                            Components.DoorComp doorComp =
                                (Components.DoorComp)_entityManager.GetSingleComponentByID(ids, Core.ComponentTypes.Door);

                            if (!doorComp.IsOpen)
                            {
                                openingDoor     = true;
                                doorComp.IsOpen = true;
                                _dungeonMap.SetCellProperties(pc.X, pc.Y, true, true);
                                Components.RenderComp rc = (Components.RenderComp)_entityManager.GetSingleComponentByID(ids, Core.ComponentTypes.Render);
                                rc.Glyph = '-';
                            }
                        }


                        //is it something living?
                        if ((checker & (int)Core.ComponentTypes.Health) > 0)
                        {
                            //Game.MessageLog.Add("FIGHT NOW");
                            collision = true;

                            //get details of person being hit
                            Components.HealthComp hComp =
                                (Components.HealthComp)_entityManager.GetSingleComponentByID(ids, Core.ComponentTypes.Health);
                            Components.AttributesComp attComp
                                = (Components.AttributesComp)_entityManager.GetSingleComponentByID(ids, Core.ComponentTypes.Attributes);

                            Components.CreatureDetailsComp detailsComp =
                                (Components.CreatureDetailsComp)_entityManager.GetSingleComponentByID(ids, Core.ComponentTypes.CreatureDetails);
                            bool isUndead = detailsComp.Undead;

                            Components.AIComp aiComp = (Components.AIComp)_entityManager.GetSingleComponentByID(ids, Core.ComponentTypes.AI);

                            // get hitter details
                            Components.AttributesComp attOfHitterComp
                                = (Components.AttributesComp)_entityManager.GetSingleComponentByID(entIDRequestingMove, Core.ComponentTypes.Attributes);

                            bool hit = false;
                            // base damage = 1
                            int dmg         = 1;
                            int attackSkill = (int)(attComp.Strength + attComp.Dexterity) / 2;

                            if (RogueSharp.DiceNotation.Dice.Roll("1d20") < attackSkill)
                            {
                                hit = true;

                                // get weapon we are using
                                Components.InventoryComp invComp
                                    = (Components.InventoryComp)_entityManager.GetSingleComponentByID(entIDRequestingMove, Core.ComponentTypes.Inventory);

                                // get damage bonus
                                int dmgBonus = RogueSharp.DiceNotation.Dice.Roll(attOfHitterComp.DmgBonus);
                                dmgBonus *= attOfHitterComp.DmgMod;

                                if (invComp != null)
                                {
                                    if (invComp.CurrentWeapon > 0)
                                    {
                                        Components.WeaponComp weapon
                                            = (Components.WeaponComp)_entityManager.GetSingleComponentByID(invComp.CurrentWeapon, Core.ComponentTypes.Weapon);

                                        dmg = RogueSharp.DiceNotation.Dice.Roll(weapon.DamageBase);
                                    }
                                }
                                int totalDamage = dmgBonus + dmg;
                                if (totalDamage <= 1)
                                {
                                    totalDamage = 1;
                                }
                                hComp.Health -= totalDamage;
                                if (hComp.Health < 0)
                                {
                                    hComp.Health = 0;
                                }

                                attComp.Morale -= totalDamage;
                                if (attComp.Morale < 1)
                                {
                                    attComp.Morale = 0;
                                }

                                if (hComp.Health < (hComp.MaxHealth * 0.2))
                                {
                                    if (!isUndead)
                                    {
                                        aiComp.Fleeing = true;
                                    }
                                }
                            }


                            aiComp.UnderAttack = true;

                            aiComp.LastBasher = entIDRequestingMove;
                            if (!aiComp.CurrentEnemies.Contains(entIDRequestingMove))
                            {
                                aiComp.CurrentEnemies.Add(entIDRequestingMove);
                            }
                            Components.CreatureDetailsComp cdComp =
                                (Components.CreatureDetailsComp)_entityManager.GetSingleComponentByID(ids, Core.ComponentTypes.CreatureDetails);
                            List <Components.Component>    ourList = _entityManager.GetCompsByID(details.EntRequestingMove.UID);
                            Components.CreatureDetailsComp ourComp =
                                (Components.CreatureDetailsComp)ourList.FirstOrDefault(x => x.CompType == Core.ComponentTypes.CreatureDetails);
                            if (hit)
                            {
                                Game.MessageLog.Add($"{ourComp.PersonalName} Bashes {cdComp.PersonalName} for {dmg} Damage");
                            }
                            else
                            {
                                Game.MessageLog.Add($"{ourComp.PersonalName} Misses {cdComp.PersonalName}");
                            }


                            if (hComp.Health < 1)
                            {
                                // entity is dead, collect up and kill at end
                                deathList.Add(ids);
                            }
                        }
                    }
                }

                foreach (int i in deathList)
                {
                    Core.DeleteEntEventArgs msg = new Core.DeleteEntEventArgs(Core.EventTypes.DeadEntity, i);
                    Core.EventBus.Publish(Core.EventTypes.DeadEntity, msg);
                }

                if (!collision)
                {
                    // move okay
                    Core.MoveOkayEventArgs msg = new Core.MoveOkayEventArgs(Core.EventTypes.MoveOK, details.EntRequestingMove, details.x, details.y);
                    Core.EventBus.Publish(Core.EventTypes.MoveOK, msg);
                }
                else     //if (!openingDoor)
                {
                    //    // fight on
                    Core.NoMoveEventArgs msg = new Core.NoMoveEventArgs(Core.EventTypes.NoMove, details.EntRequestingMove);
                    Core.EventBus.Publish(Core.EventTypes.NoMove, msg);
                }
                break;
            }
        }
コード例 #4
0
ファイル: RenderSystem.cs プロジェクト: Grufferz/ECS-Test
        public void Update(RLConsole console, RLConsole statsConsole)
        {
            var posBit     = (int)Core.ComponentTypes.Position;
            var rendBit    = (int)Core.ComponentTypes.Render;
            int statBit    = (int)Core.ComponentTypes.Health;
            int detailsBit = (int)Core.ComponentTypes.CreatureDetails;
            int yPos       = 16;
            int xPos       = 2;

            Dictionary <int, int> ents = EntityManager.EntityBitLookUp;

            foreach (KeyValuePair <int, int> pair in ents)
            {
                int eid          = pair.Key;
                int furnitureRes = (int)Core.ComponentTypes.Furniture & pair.Value;
                if (furnitureRes > 0)
                {
                    Components.RenderComp   rc = (Components.RenderComp)EntityManager.GetSingleComponentByID(eid, Core.ComponentTypes.Render);
                    Components.PositionComp pc = (Components.PositionComp)EntityManager.GetSingleComponentByID(eid, Core.ComponentTypes.Position);
                    console.Set(pc.X, pc.Y, rc.Colour, Core.Colours.FloorBackground, rc.Glyph);
                }
            }

            foreach (KeyValuePair <int, int> pair in ents)
            {
                int res    = posBit & pair.Value;
                int res2   = rendBit & pair.Value;
                int actRes = (int)Core.ComponentTypes.Actor & pair.Value;

                char    renderChar = 'X';
                RLColor c          = RLColor.White;

                List <Components.Component> comps = EntityManager.GetCompsByID(pair.Key);

                if ((res > 0 && res2 > 0) && (actRes == 0))
                {
                    Components.RenderComp rendComp
                        = (Components.RenderComp)comps.Find(s => s.CompType == Core.ComponentTypes.Render);

                    Components.PositionComp posComp =
                        (Components.PositionComp)comps.Find(s => s.CompType == Core.ComponentTypes.Position);

                    if (rendComp != null && posComp != null)
                    {
                        console.Set(posComp.X, posComp.Y, rendComp.Colour,
                                    Core.Colours.FloorBackground, rendComp.Glyph);
                    }
                    if (rendComp != null)
                    {
                        renderChar = rendComp.Glyph;
                        c          = rendComp.Colour;
                    }
                }

                //draw stats
            }
            foreach (KeyValuePair <int, int> pair in ents)
            {
                // draw only actors on top
                if ((pair.Value & (int)Core.ComponentTypes.Actor) > 0)
                {
                    List <Components.Component> comps = EntityManager.GetCompsByID(pair.Key);

                    Components.RenderComp rendComp = (Components.RenderComp)comps.Find(s => s.CompType == Core.ComponentTypes.Render);

                    Components.PositionComp posComp =
                        (Components.PositionComp)comps.Find(s => s.CompType == Core.ComponentTypes.Position);
                    Components.AIComp aiComp = (Components.AIComp)comps.Find(x => x.CompType == Core.ComponentTypes.AI);

                    RLColor rColor;

                    if (aiComp.Fleeing)
                    {
                        rColor = RLColor.Red;
                    }
                    else
                    {
                        rColor = rendComp.Colour;
                    }

                    if (rendComp != null && posComp != null)
                    {
                        console.Set(posComp.X, posComp.Y, rColor,
                                    Core.Colours.FloorBackground, rendComp.Glyph);
                    }

                    int res3 = statBit & pair.Value;
                    if (res3 > 0)
                    {
                        Components.HealthComp healthStat =
                            (Components.HealthComp)comps.Find(s => s.CompType == Core.ComponentTypes.Health);

                        Components.CreatureDetailsComp detailsComp =
                            (Components.CreatureDetailsComp)comps.Find(x => x.CompType == Core.ComponentTypes.CreatureDetails);

                        Components.InventoryComp invComp = (Components.InventoryComp)comps.Find(x => x.CompType == Core.ComponentTypes.Inventory);

                        if (healthStat != null && detailsComp != null)
                        {
                            statsConsole.Print(xPos, yPos, detailsComp.PersonalName + " the " + detailsComp.Name, rendComp.Colour);
                            yPos++;
                            statsConsole.Print(xPos, yPos, rendComp.Glyph.ToString(), rendComp.Colour);
                            int width
                                = Convert.ToInt32(((double)healthStat.Health / (double)healthStat.MaxHealth) * 16);
                            int remainingWidth = 16 - width;
                            statsConsole.SetBackColor(xPos + 2, yPos, width, 1, Core.Swatch.Primary);
                            statsConsole.SetBackColor(xPos + 2 + width, yPos, remainingWidth, 1, Core.Swatch.PrimaryDarkest);
                            statsConsole.Print(xPos + 2, yPos, $": {healthStat.Health.ToString()}", Core.Swatch.DbLight);
                            yPos++;
                            if (invComp != null)
                            {
                                statsConsole.Print(xPos, yPos, $"Carrying {invComp.Treasure.Count.ToString()} Gold", rendComp.Colour);
                            }
                            yPos = yPos + 2;
                        }
                    }
                }
            }
            // stats console
            int count = 0;

            for (int yp = 0; yp < EntityManager.GetHeight(); yp++)
            {
                for (int xp = 0; xp < EntityManager.GetWidth(); xp++)
                {
                    if (EntityManager.Positions[yp, xp].Count > 0)
                    {
                        HashSet <int> ent = EntityManager.Positions[yp, xp];

                        foreach (int indvID in ent)
                        {
                            List <Components.Component> inner = EntityManager.Entities[indvID];
                            foreach (Components.Component ec in inner)
                            {
                                if (ec.CompType == Core.ComponentTypes.Collectable)
                                {
                                    count++;
                                }
                            }
                        }
                    }
                }
            }

            statsConsole.Print(1, 1, $"collectables=: {count.ToString()}", Core.Colours.TextHeading);
        }