Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

9 years ago by Codestar

I'm wondering if any of my fellow impact devs out there have optimized their games/impact for things like google's V8 engine? If so, what tweaks or ways of working have you implemented in your code?

I'm currently working on implementing an entity component system for my game, to try to save myself from having such huge JS objects and deep running Inheritance chains. What things, if any, are you all doing?

9 years ago by drhayes

A couple of caveats: I don't worry about performance until I worry about performance, if that makes sense. There's no sense worrying about it until it becomes a problem.

That said, I don't like inheritance hierarchies either so I wrote a behavior framework for Impact that has, literally, no documentation at the moment.

Essentially, you write your Entity subclass then call enableBehaviors on it. This adds a method named addBehavior to the entity. Your behaviors should derive from BaseBehavior. I need to add examples of that.

Each behavior can then be turned on and off independently, which is nice for cinematics (turn off input) or for invincibility (turn off ability to be hurt) or whatever.

Here's a snippet from my player entity:

      // Add behaviors.
      this.addBehavior(new PlayerGroundMove());
      this.addBehavior(new RespondsToInput());
      this.addBehavior(new CanJump());
      this.addBehavior(new FallingDeath());
      this.addBehavior(new CanInvokeThings());
      this.addBehavior(new Hiding());
      this.addBehavior(new RavenTransform());
      if (ig.state.wieldingSword) {
        console.log('cool sword bro');
        this.addBehavior(new WieldingSword());
      }
      if (ig.state.wieldingMask) {
        console.log('check out that mask');
        this.addBehavior(new WieldingMask());
      }
      this.addBehavior(new TracksHealth());
      this.addBehavior(new BounceOnHurt());

The other thing I like is my code organization: my update method is pretty much non-existent. All the important code is in the behaviors.

9 years ago by Joncom

Neat idea drhayes. Would be interesting to see how an entire game would look structured in this way. Hard to imagine a game without update functions.

9 years ago by FelipeBudinich

In my experience you don't need to worry about anything but:

Performance:
- Writing and reading with a high frequency from localStorage: More than one key value pair every 3000 ms.
- Having too many draw calls on mobile devices: Be careful with fonts, it's better to render them to an offscreen canvas, and then draw that instead of drawing each character every tick.

Download Speed:
- Use a texture atlas if download speed is a priority: This is the second heaviest asset after music and sound effects, but there's not much you can do about those.

9 years ago by drhayes

Those are good tips. Noted. ( =

Yeah, localStorage is tricky -- it's a synchronous API slaved to a "who knows when it's going to complete the next operation" hard drive. I'd think twice before using it inside a critical part of the game, i.e. any time when the player isn't just standing around waiting to save the game.

9 years ago by drhayes

@Joncom: Here's a behavior I use for enemies and it's relatively short:

  var LOOKAHEAD_X = 4;

  var switchDirections = function(entity) {
    entity.direction *= -1;
    entity.vel.x *= -1;
    entity.accel.x *= -1;
  };

  StayOnPlatform = BaseBehavior.extend({
    added: function(entity) {
      entity.direction = [-1, 1].random();
      this.deltaX = entity.size.x / 2;
      this.deltaY = entity.size.y + 1;
    },

    update: function(entity) {
      // Check the entity below this entity's feet directly in front
      // of the entity.
      var x = entity.pos.x + this.deltaX + (LOOKAHEAD_X * entity.direction);
      var y = entity.pos.y + this.deltaY;
      var tile = ig.game.collisionMap.getTile(x, y);
      if (!tile) {
        switchDirections(entity);
      }
    },

    handleMovementTrace: function(entity, res) {
      if (res.collision.x) {
        switchDirections(entity);
      }
    }
  });

Here's one on the player:

  CanInvokeThings = BaseBehavior.extend({
    name: 'CanInvokeThings',

    update: function(entity) {
      if (entity.inputPressed('up') && !entity.crouched &&
          this.currentThing && entity.standing) {
        // Are we still touching that thing? Checking to be sure...
        if (!entity.touches(this.currentThing)) {
          this.currentThing = null;
          return;
        }
        this.currentThing.invoke(entity);
      }
    },

    check: function(entity, other) {
      if (other && typeof other.invoke === 'function') {
        this.currentThing = other;
      }
    }
  });

They tend to be pretty short and self-contained, which I like 'cuz my brain is only so big.

Also, I can turn behaviors on and off, e.g. the player stops tracking health because right now they're invincible, etc.
Page 1 of 1
« first « previous next › last »