Advertisement
SilverTES

Movement Pattern Shmup Enemy

May 3rd, 2025
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.95 KB | Source Code | 0 0
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4.  
  5. namespace ShootThemAll
  6. {
  7.     public enum CurveType
  8.     {
  9.         Linear,
  10.         Bezier,
  11.         Sinusoidal,
  12.         Custom,
  13.     }
  14.     public class MovePattern
  15.     {
  16.         public List<Vector2> Waypoints { get; set; }
  17.         public float Speed { get; set; }
  18.         public bool Loop { get; set; }
  19.         public CurveType CurveType { get; set; }
  20.         public Func<float, Vector2> CustomTrajectory { get; set; } // Fonction personnalisée
  21.  
  22.         public Vector2 CalculatePosition(float progress)
  23.         {
  24.             switch (CurveType)
  25.             {
  26.                 case CurveType.Linear:
  27.                     return LinearInterpolation(progress);
  28.                 case CurveType.Bezier:
  29.                     return BezierInterpolation(progress);
  30.                 case CurveType.Sinusoidal:
  31.                     return SinusoidalMovement(progress);
  32.                 default:
  33.                     return Waypoints[0]; // Fallback
  34.             }
  35.         }
  36.         private Vector2 LinearInterpolation(float progress)
  37.         {
  38.             int startIndex = (int)(progress * (Waypoints.Count - 1));
  39.             int endIndex = Math.Min(startIndex + 1, Waypoints.Count - 1);
  40.             float segmentProgress = (progress * (Waypoints.Count - 1)) % 1;
  41.  
  42.             return Vector2.Lerp(Waypoints[startIndex], Waypoints[endIndex], segmentProgress);
  43.         }
  44.  
  45.         private Vector2 BezierInterpolation(float progress)
  46.         {
  47.             if (Waypoints.Count < 2)
  48.                 return Vector2.Zero; // Pas assez de points pour une courbe
  49.  
  50.             // Normaliser le progrès entre 0 et 1
  51.             float t = Math.Clamp(progress, 0f, 1f);
  52.             int n = Waypoints.Count - 1; // Ordre de la courbe
  53.  
  54.             Vector2 result = Vector2.Zero;
  55.             for (int i = 0; i <= n; i++)
  56.             {
  57.                 // Calculer le coefficient binomial et le terme de la formule de Bézier
  58.                 float coefficient = BinomialCoefficient(n, i) * (float)Math.Pow(1 - t, n - i) * (float)Math.Pow(t, i);
  59.                 result += coefficient * Waypoints[i];
  60.             }
  61.  
  62.             return result;
  63.         }
  64.  
  65.         private static float BinomialCoefficient(int n, int k)
  66.         {
  67.             if (k < 0 || k > n) return 0;
  68.             if (k == 0 || k == n) return 1;
  69.  
  70.             // Optimisation : utiliser la symétrie pour réduire les calculs
  71.             k = Math.Min(k, n - k);
  72.             float result = 1;
  73.             for (int i = 1; i <= k; i++)
  74.             {
  75.                 result *= (n - (k - i)) / (float)i;
  76.             }
  77.             return result;
  78.         }
  79.  
  80.  
  81.         private Vector2 SinusoidalMovement(float progress)
  82.         {
  83.             // Exemple : mouvement sinusoïdal autour d'un axe
  84.             Vector2 basePos = LinearInterpolation(progress);
  85.             float amplitude = 50f; // Paramétrable
  86.             float frequency = 2f; // Paramétrable
  87.             basePos.Y += (float)Math.Sin(progress * frequency * Math.PI) * amplitude;
  88.             return basePos;
  89.         }
  90.  
  91.  
  92.     }
  93.     //public class Wave
  94.     //{
  95.     //    private List<(float spawnTime, Enemy enemy)> _enemies = new();
  96.     //    private float _timer;
  97.  
  98.     //    public void AddEnemy(float spawnTime, Enemy enemy)
  99.     //    {
  100.     //        _enemies.Add((spawnTime, enemy));
  101.     //    }
  102.  
  103.     //    public void Update(GameTime gameTime, List<Enemy> activeEnemies)
  104.     //    {
  105.     //        _timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
  106.  
  107.     //        foreach (var (spawnTime, enemy) in _enemies)
  108.     //        {
  109.     //            if (_timer >= spawnTime && !activeEnemies.Contains(enemy))
  110.     //            {
  111.     //                activeEnemies.Add(enemy);
  112.     //            }
  113.     //        }
  114.  
  115.     //        foreach (var enemy in activeEnemies)
  116.     //        {
  117.     //            enemy.Update(gameTime);
  118.     //        }
  119.     //    }
  120.     //}
  121.  
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement