Ejemplo n.º 1
0
        private static void BuildTankState(Entity tank,
                                           TankGenome genome)
        {
            var state = new TankStateComponent(tank, Globals.EventManager)
            {
                TrackWidth = 1f
            };

            foreach (var attribute in TankGenome.Attributes)
            {
                var percent = (float)genome.GetAttribute(attribute) /
                              TankGenome.MaxAttributeValue;

                SetStateAttribute(state, attribute, percent);
            }

            var rangePercent = (float)genome.GetAttribute(Attribute.GunRange) /
                               TankGenome.MaxAttributeValue;
            var damagePercent = (float)genome.GetAttribute(Attribute.GunDamage) /
                                TankGenome.MaxAttributeValue;
            var lengthBase = state.Dimensions.X / 2f;
            var length     = (lengthBase * 0.5f) * rangePercent + (lengthBase * 0.5f);
            var widthBase  = state.TurretWidth / 4f;
            var width      = (widthBase * 0.5f) * damagePercent + (widthBase * 0.5f);

            state.BarrelDimensions = new Vector2(length, width);

            tank.AddComponent(state);
        }
Ejemplo n.º 2
0
        public static Entity CreateFromGenome(TankGenome genome)
        {
            if (genome == null)
            {
                throw new ArgumentNullException("genome");
            }

            var tank = CreateTankBase();

            tank.AddComponent(new TankAiComponent(tank, Globals.EntityManager,
                                                  Globals.EventManager, Globals.PhysicsManager));
            BuildTankState(tank, genome);

            if (!tank.Initialize())
            {
                Log.ErrorFmt("Could not initialize {0}, discarding", tank.Name);
                tank.Dispose();
                return(null);
            }

            Globals.EntityManager.AddEntity(tank);
            return(tank);
        }