Animation system with generics can’t use polymorphism

I am trying to make an animation system that affects different types so I thought I would use generics.

I have an animation controller that gets all animation objects into a collection (or at least that was the plan) then then run the animation on all the animation objects.

The problem is I can’t do it this way in C# as it does not allow polymorphic collections for different types on generics.

Here is the code structure involved for different animations:

public abstract class Animation<T> {
    protected T TargetValue {protected set; get;}  // could be int/float/vector2 etc etc
    protected T StartValue {protected set; get;} 
    public T CurrentValue {protected set; get;} 
    public Action OnUpdate;

    public virtual T Animate(float deltaTime) => OnUpdate?.Invoke();
}

public class Fade : Animation<float> { // lerp between two floats

   public override float Animate(float deltaTime) {
      CurrentValue = float.Lerp(StartValue,TargetValue,deltaTime);
      base.Animate(deltaTime);
   }

}

public class Translation : Animation<Vector2> { // move to target Vector2
   public override Vector2 Animate(float deltaTime) {
       CurrentValue = Vector2.Lerp(StartValue,TargetValue,deltaTime);  
       base.Animate(deltaTime); 
   }
}

Now my animation controller class is supposed to get all animations an run animations on them:

public class AnimationController {
   
    private List<Animation<???>> _animations = new List<Animation<???>>();    
    void Init() => _animations = GetAllComponents<Animation>(); // get all animations as collection


    public void Animation(float deltaTime)
       for(int i = 0; i < _animations.Count; i++) 
          _animations[i].Animate(deltaTime);
    }
}

However, this is not an option for me as I cannot have collections of generics with different types for the generic T.

So what would be a better design for this system because I do not know a good solution ???

2

Approach One

Each concrete implementation of Animation appears to be used by other classes in different ways:

  • At the minimum, Animation items need to be stored in a collection.
  • There is a need to call the Animation.Animate(float) method on each item in the collection. This caller (which processes the collection) does not need to be aware of the type of state parameters (time-varying variables).
  • Each Animation item may also be used by some other classes (which are not illustrated in the code example above). Each of these other classes make use of the state parameters (time-varying variables) of some specific concrete Animation items; therefore, they must be aware of the types.

Suggestion (C# specific. The suggestion may need to be tailored or modified for other languages.)

Firstly, define an Animation interface or abstract class that is non-generic.

public interface Animation
{
    void Animate(float deltaTime);
}

Then, define a generic Animation interface where the types of time-varying variables are specified.

public interface Animation<T> : Animation
{
    T CurrentValue { get; }
}

Finally, have your abstract classes implement Animation<T>. By doing so, they also indirectly implement Animation (the non-generic interface), which allows the AnimationController to call its void Animate(float) method even if AnimationController doesn’t know the types of each item’s time-varying variables.

This is a very common usage pattern in C# generics.


Summary of Approach One

In C#, it is widely recognized that generic classes may require some kind of uniform handling in which the generic parameters (the <T>) don’t matter and shouldn’t matter. To do so, the widely-accepted practice is to create a parent interface or parent abstract class that is non-generic, that provides limited access to the concrete classes, without the knowledge of the <T>.


Approach Two

Provide an abstraction for the concrete implementations of time-varying variables (float, Vector2, etc). Let’s call it Lerpable.

The Lerpable interface will have a method called Lerp.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật