Esempio n. 1
0
        public Food CompareClosest(Vector2 position, Food foodLeft, Food foodRight)
        {
            if (Math.Abs(position.X - foodLeft.FoodRectangle.X) + Math.Abs(position.Y - foodLeft.FoodRectangle.Y) < Math.Abs(position.X - foodRight.FoodRectangle.X) + Math.Abs(position.Y - foodRight.FoodRectangle.Y))
                return foodLeft;

            return foodRight;
        }
Esempio n. 2
0
        public bool GetClosestSensor(Food food, ProximitySensor sensorA, ProximitySensor sensorB)
        {
            if (Math.Abs(food.FoodRectangle.X - sensorA.rotatedPosition.X) + Math.Abs(food.FoodRectangle.Y - sensorA.rotatedPosition.Y) < Math.Abs(food.FoodRectangle.X - sensorB.rotatedPosition.X) + Math.Abs(food.FoodRectangle.Y - sensorB.rotatedPosition.Y))
                return true;

            return false;
        }
Esempio n. 3
0
        public void Update(GameTime gameTime)
        {
            collisionCircle.Update(gameTime, (int)(position.X - origin.X), (int)(position.Y - origin.Y));

            proximitySensor[0].Update(gameTime, (int)(position.X), (int)(position.Y), rotation, 8, 16);
            proximitySensor[1].Update(gameTime, (int)(position.X), (int)(position.Y), rotation, -8, 16);
            proximitySensor[2].Update(gameTime, (int)(position.X), (int)(position.Y), rotation, 0, -20);

            float x = Math.Abs(proximitySensor[0].rotatedPosition.X - proximitySensor[1].rotatedPosition.X) + Math.Min(proximitySensor[0].rotatedPosition.X, proximitySensor[1].rotatedPosition.X);
            float y = Math.Abs(proximitySensor[0].rotatedPosition.Y - proximitySensor[1].rotatedPosition.Y) + Math.Min(proximitySensor[0].rotatedPosition.Y, proximitySensor[1].rotatedPosition.Y);

            closestLeft = sensorFunctions.GetClosestFood(proximitySensor[1].rotatedPosition, Tank.foodList);
            closestRight = sensorFunctions.GetClosestFood(proximitySensor[0].rotatedPosition, Tank.foodList);
            closest = sensorFunctions.CompareClosest(new Vector2(x, y), closestLeft, closestRight);

            int[] input = new int[2];

            if (sensorFunctions.GetClosestSensor(closest, proximitySensor[1], proximitySensor[0])) input[1] = 1;
            else if (sensorFunctions.GetClosestSensor(closest, proximitySensor[2], proximitySensor[1]) || sensorFunctions.GetClosestSensor(closest, proximitySensor[2], proximitySensor[0])) input[1] = 1;
            else input[0] = 1;

            brain.SendInput(input);
            brain.UpdateNetwork();

            if (position.X >= Game1.theoreticalScreenWidth) position.X = 0;
            if (position.X + texture.Width <= 0) position.X = Game1.theoreticalScreenWidth;
            if (position.Y >= Game1.theoreticalScreenHeight) position.Y = 0;
            if (position.Y + texture.Height <= 0) position.Y = Game1.theoreticalScreenHeight;

            if (brain.GetOutput()[0].value == 1) rotation -= 1;
            if (brain.GetOutput()[1].value == 1) rotation += 1;
            if (brain.GetOutput()[2].value == 1) Swim(.5f);

            rectangle = new Rectangle((int)(position.X), (int)(position.Y), texture.Width, texture.Height);
        }