FlixelCommunity / flixel

Community fork of Adam “Atomic” Saltsman's popular game engine Flixel. Distilled from a variety of Flash games he worked on over the last couple years, including Gravity Hook, Fathom and Canabalt, its primary function is to provide some useful base classes that you can extend to make your own game objects.
http://flixelcommunity.org/
Other
84 stars 17 forks source link

Add `FollowStyle` class #190

Open IQAndreas opened 10 years ago

IQAndreas commented 10 years ago

This will allow you to create your own FollowStyle for the camera (perhaps you want it to pan easily whenever something moves instead of "jumping" immediately to the correct position?

And if you want a custom deadzone, you would either extend the class, or call something like camera.follow(target, new FollowStyle(cutomDeadzone));.

But primarily, it will use things like FollowStyle.LOCKON, and second, it will reduce the code in FlxCamera#follow() to something like:

public function follow(Target:FlxBasic, followStyle:FollowStyle):void
{
    target = Target;
    deadzone = followStyle.getDeadzone(this, target);
}

Bleh, maybe it's just easier to show these proposed changes with actual code once we get to that point. This is probably the least clear suggestion I have written so far. :/

IQAndreas commented 10 years ago

Also, this would make it easy to add FollowStyles that only follow the target on the x or y axis.

Dovyski commented 10 years ago

This suggestion is amazing! It cleans up FlxCamera#follow() code and allows the creation of fancy following patterns as you mentioned.

Maybe we could change follow() to:

follow(Target:FlxBasic, FollowStyle:uint, FollowStyleClass:Class = FollowStyle)

That way the method is responsible for instantiating the requested follow style. The "official" styles could be implemented within the FollowStyle class itself, e.g.:

class FollowStyle {
  public static const STYLE_LOCKON:uint = 0;
  public static const PLATFORMER :uint = 1;
  (...)

  public function FollowStyle(Style :uint) {
    switch(Style) {
        case STYLE_LOCKON: deadzone = new FlxRectangle();
        (...)
    }
  }
}

Then in FlxCamera#follow():

public function follow(Target:FlxBasic, FollowStyle:uint, FollowStyleClass:Class = FollowStyle) :void {
  target = Target;
  followStyle = new FollowStyleClass(this, FollowStyle);
} 

New follow styles can be easily created and used by developers e.g.

camera.follow(target, MyCustomFollowStyle.LOCKON, MyCustomFollowStyle);

class MyCustomFollowStyle extends FollowStyle {
  (...)
}