frostney / flockn

:video_game: Leightweight declarative game engine for JavaScript
The Unlicense
16 stars 0 forks source link

Discussion: Is the ES6 class hierarchy really needed? #7

Open frostney opened 9 years ago

frostney commented 9 years ago

This is not something that will be tackled for the 0.1.0 release.

While I really like ES6 classes, I am contemplating if they are really needed in the flockn game engine. Every object is described as a function and not as a class. Internally they are being converted to ES6 classes with a base class. I'm wondering if it that is really needed or we could opt for a more mixin-based approach. I'm even wondering if we need classes all-together, as I feel it might create a rift between how the game engine is implemented and how it is used.

Tankenstein commented 9 years ago

I personally feel like the most intuitive way to use a game engine would be to create a class that extends from a base class for different types of objects, and defining methods on those classes. I feel like using mixins with functions would be more convoluted.

An example would be this:

class Hero extends Flockn.Entity {
  constructor() {
    super();
    this.name = 'Hero';
    this.texture.image.filename = 'hero_front.png';
  }

  update(deltaTime) {
    this.angle += deltaTime*10;
  }

  onKeyDown(keyCode) {
    switch (keyCode) {
      case Flockn.keys.UP: // fallthrough
      case Flockn.keys.w:
        this.position.y -= 10;
        break;

      case Flockn.keys.DOWN: // fallthrough
      case Flockn.keys.s:
        this.position.y += 10;
        break;

      case Flockn.keys.LEFT: // fallthrough
      case Flockn.keys.a:
        this.position.x -= 10;
        break;

      case Flockn.keys.RIGHT: // fallthrough
      case Flockn.keys.d:
        this.position.x += 10;
        break;
    }
  }

  onMouseClick() {
    alert('You clicked that hero guy.');
  }
}

I also feel like onMouseClick there could be refined, by making it more like onKeyDown, so there would be something like onMouse, with some kind of event as an argument.

This class way should feel familiar to anyone that has developed in Unity or the like, so there would be some basic methods like the constructor, update and event handlers.

frostney commented 9 years ago

@Tankenstein I love this idea and with ES7 class decorators I think that could be something really special (https://twitter.com/frostney_/status/582828034371223553). I'm not sure that's the direction I want to take flockn in, but if I find the time I would love to take this ideas and create a seperate repository/project out of those.

Tankenstein commented 9 years ago

I might just write a small wrapper (without annotations), that could handle doing that, once i get some time off work, since i also REALLY want a javascript game engine like that.