Skip to content

Commit

Permalink
refactor: Graphics Component simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Jan 19, 2024
1 parent 82799b1 commit 4e081bf
Show file tree
Hide file tree
Showing 14 changed files with 382 additions and 508 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Breaking Changes

-
- Remove confusing Graphics Layering from `ex.GraphicsComponent`, recommend we use the `ex.GraphicsGroup` to manage this behavior
* Update `ex.GraphicsGroup` to be consistent and use `offset` instead of `pos` for graphics relative positioning

### Deprecated

Expand Down
26 changes: 18 additions & 8 deletions sandbox/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,21 +618,31 @@ var healthbar2 = new ex.Rectangle({
color: new ex.Color(0, 255, 0)
});

var backroundLayer = player.graphics.layers.create({
name: 'background',
order: -1
});

backroundLayer.show(healthbar2, { offset: ex.vec(0, -70) });
var playerText = new ex.Text({
text: 'A long piece of text is long',
font: new ex.Font({
size: 20,
family: 'Times New Roman'
})
});
// playerText.showDebug = true;
backroundLayer.show(playerText, { offset: ex.vec(0, -70) });

var group = new ex.GraphicsGroup({
members: [
{ graphic: healthbar2, pos: ex.vec(0, -70)},

Check failure on line 631 in sandbox/src/game.ts

View workflow job for this annotation

GitHub Actions / build

Object literal may only specify known properties, and 'pos' does not exist in type 'Graphic | GraphicsGrouping'.
{ graphic: playerText, pos: ex.vec(0, -70)}

Check failure on line 632 in sandbox/src/game.ts

View workflow job for this annotation

GitHub Actions / build

Object literal may only specify known properties, and 'pos' does not exist in type 'Graphic | GraphicsGrouping'.
]
})
healthbar.graphics.use(group);

// var backgroundLayer = player.graphics.layers.create({
// name: 'background',
// order: -1
// });

// backgroundLayer.show(healthbar2, { offset: ex.vec(0, -70) });

// // playerText.showDebug = true;
// backgroundLayer.show(playerText, { offset: ex.vec(0, -70) });

// Retrieve animations for player from sprite sheet
var left = ex.Animation.fromSpriteSheet(spriteSheetRun, ex.range(1, 10), 50);
Expand Down
1 change: 1 addition & 0 deletions sandbox/tests/material/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ void main() {
fragColor.rgb = mix(screen_color.rgb, mixColor, u_color.a)*fragColor.a + (wave_crest_color.rgb * wave_crest);
fragColor.rgb = texture(u_noise, v_uv).rgb * fragColor.a;
fragColor.rgb = vec3(gl_FragCoord.xy/u_resolution, 0.0);
}`;

const noise = new ex.ImageSource('./noise.avif', false, ex.ImageFiltering.Pixel);
Expand Down
10 changes: 8 additions & 2 deletions sandbox/tests/sprite-tint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ actor.onInitialize = () => {
})
);

actor.graphics.show(shadow, {offset: ex.vec(10, 10)});
actor.graphics.show(sprite);
const group = new ex.GraphicsGroup({
members: [
{graphic: shadow, pos: ex.vec(10, 10)},
sprite
]
})

actor.graphics.use(group);
};


Expand Down
81 changes: 66 additions & 15 deletions site/docs/04-graphics/04.2-graphics-component.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,78 @@ actor.graphics.onPostDraw = (ctx: ex.ExcaliburGraphicsContext) => {
}
```

### Layers
### Multiple Graphics at Once

The layer's component adds a way for multiple [graphics](#graphics) to be on top or behind each other for a certain actor or entity.
Sometimes you want to draw multiple pieces of graphics at once! There are two recommended ways to accomplish this!

Layers can be ordered numerically, larger negative layers behind, and positive layers in front.
#### GraphicsGroup

```typescript
actor.graphics.layers.create({ name: 'background', order: -1 })
actor.graphics.layers.create({ name: 'foreground', order: 1 })

actor.graphics.layers.get('background').show(myBackground)
actor.graphics.layers.get('foreground').show(myForeground)
[[GraphicsGroup|Graphics groups]] allow you to compose multiple graphics together into 1. This can be useful if you have multi layered or complex graphics requirements. One limitation however is you can only influence the relative offset from you Actor.

// no longer display the background
actor.graphics.layers.get('background').hide()
```typescript
const healthBarActor = new ex.Actor({...})

const healthBarRectangle = new ex.Rectangle({
width: 140,
height: 5,
color: new ex.Color(0, 255, 0)
});

const healthBarText = new ex.Text({
text: 'A long piece of text is long',
font: new ex.Font({
size: 20,
family: 'Times New Roman'
})
});

const group = new ex.GraphicsGroup({
members: [
{ graphic: healthbarRectangle, pos: ex.vec(0, -70)},
{ graphic: healthBarText, pos: ex.vec(0, -70)}
]
});

healthBarActor.graphics.use(group);
```

There is always a layer named `'default'` at `order: 0`
#### Child Actor or Entity

If you need independent articulation and a lot of control over positioning, rotation, and scale this is this strategy to reach for. One example is you might have a main actor, and a child actor for every limb of a paper doll.

```typescript
actor.graphics.show(myAnimation)
// is equivalent to
actor.graphics.layers.get('default').show(myAnimation)

import { Resources } from './Resources';

class PaperDoll extends ex.Actor {
this.leftArm = new ex.Actor({
pos: ex.vec(-10, 10)
});
this.rightArm = new ex.Actor({
pos: ex.vec(10, 10)
});
this.head = new ex.Actor({
pos: ex.vec(0, -10)
});
this.body = new ex.Actor({
pos: ex.vec(0, 20)
});
this.leftLeg = new ex.Actor({
pos: ex.vec(-10, 30)
});
this.rightLeg = new ex.Actor({
pos: ex.vec(10, 30)
});

constructor() {
this.leftArm.graphics.use(Resources.LeftArm);
this.rightArm.graphics.use(Resources.RightArm);

this.head.graphics.use(Resources.Head);
this.body.graphics.use(Resources.Body);

this.leftLeg.graphics.use(Resources.LeftLeg);
this.rightLeg.graphics.use(Resources.RightLeg);
}
}
```
7 changes: 3 additions & 4 deletions src/engine/Actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function isActor(x: any): x is Actor {
}

/**
* Actor contructor options
* Actor constructor options
*/
export interface ActorArgs {
/**
Expand Down Expand Up @@ -137,7 +137,7 @@ export interface ActorArgs {
*/
collider?: Collider;
/**
* Optionally suppy a [[CollisionGroup]]
* Optionally supply a [[CollisionGroup]]
*/
collisionGroup?: CollisionGroup;
}
Expand Down Expand Up @@ -492,8 +492,7 @@ export class Actor extends Entity implements Eventable, PointerEvents, CanInitia
}
public set color(v: Color) {
this._color = v.clone();
const defaultLayer = this.graphics.layers.default;
const currentGraphic = defaultLayer.graphics[0]?.graphic;
const currentGraphic = this.graphics.current;
if (currentGraphic instanceof Raster || currentGraphic instanceof Text) {
currentGraphic.color = this._color;
}
Expand Down
Loading

0 comments on commit 4e081bf

Please sign in to comment.