Skip to content

Breakable

namespace Griff.Modules.PhysicsGames;
public sealed class Breakable : Component, IDrawable, IPlaytestReport

Something that breaks when it’s hit hard enough, with no code: a plank of wood, a pane of glass, a stone block, a pig in a tower. Every hit harder than its MinHit adds to its Damage, cracking it more and more as it goes, and once that reaches its Strength it breaks: it splits into pieces that tumble away and fade (Split), or vanishes in a puff of dust, with its Sound and BreakMessage. A hit is as hard as the push it took to stop the two coming together: a crate of Mass 1 landing at 5 units a second hits 5, so heavy, fast things hit hardest. Hits come from anything (a shot from a Slingshot, a falling block, the floor), and what breaks it can break something else in turn.

Materials come from a table: give it a Table Values on a Materials table whose rows (Wood, Glass, Stone) have columns named like its properties (Strength, Min Hit) and its body’s and sprite’s (Mass, Tint). With a Health on the same entity, each hit that counts takes its damage off that too (its bar, flash and Hurt Sound go with it), and it breaks when the Health dies. Its Break action breaks it whatever its damage, and Repair mends it. Its Damage and Broken outputs are for a Counter, a Bar or a wire; its Damage Message and Break Message (with its Points) for a Counter that keeps the score.

This is a component: its saved properties are on its component page.

public string? BreakMessage { get; set; }

A message sent with Game.Send as it breaks, e.g. “Popped”: with its Points as its data (a Counter adds them up), or with none, the entity (a Counter counts one).

public bool Broken { get; set; }

True once it has broken (and, with Remove off, while it lies turned off until it’s repaired).

public Color CrackColor { get; set; }

The color its cracks are drawn in (on a sprite with no sheet of stages); clear draws none.

public float Damage { get; set; }

How much damage it has taken: its hits added up. It breaks as this reaches its Strength. Set it to start it cracked.

public string? DamageMessage { get; set; }

A message sent as each hit damages it, with the damage as its data: a Counter of these scores every knock.

public int Dust { get; set; }

How many specks of dust burst out as it breaks, in its color (without an Effect); 0 none.

public string? Effect { get; set; }

A prefab spawned where it broke, e.g. “Explosion”: a burst of particles, a score popup. Takes the place of the dust.

public string? HitSound { get; set; }

A sound played as a hit damages it without breaking it, e.g. “Sounds/crack.wav”.

public float MinHit { get; set; }

Hits softer than this do no damage at all: knocks, nudges and landing gently don’t wear it down.

public float PiecesLast { get; set; }

Seconds its pieces last before they’ve faded away; 0 leaves them lying about.

public int Points { get; set; }

What it’s worth as it breaks: the data its Break Message carries, for a Counter keeping the score. 0 sends the entity.

public Color PointsColor { get; set; }

The color its points are shown in.

public bool Remove { get; set; }

Takes it away as it breaks. Untick it to leave it turned off instead, with its Broken output on (for a wire or a script to read), until its Repair action brings it back.

public bool ShowPoints { get; set; }

Shows its Points where it broke, rising and fading away: what knocking it out scored.

public string? Sound { get; set; }

A sound played as it breaks, e.g. “Sounds/smash.wav”.

public int Split { get; set; }

How many pieces it splits into along its longer side as it breaks, and as many across as keeps them about square: 2 halves a plank and quarters a crate, 3 makes nine of a crate. Each is a part of its sprite, tumbling away and fading. 0 (or 1) vanishes in a puff of dust instead.

public int Stage { get; }

Which stage of cracking it’s at: 0 whole, up to its Stages as its damage nears its Strength.

public int Stages { get; set; }

How many stages of cracking it shows as its damage builds up, one more for each share of its Strength. With a sprite sheet (a Sprite with Columns or Rows), its frames from the first are those stages instead: frame 0 whole, 1 cracked, and so on. 0 shows none.

public float Strength { get; set; }

How much damage it takes before it breaks: the hits it takes add up to this. A hit’s damage is how hard it is (a crate of Mass 1 landing at 5 units a second hits 5). 0 doesn’t break by damage (only its Break action, or its Health dying).

public void Break()

Breaks it now, whatever its damage.

public void Repair()

Mends it: no damage, no cracks, and (left turned off as it broke, with Remove off) back where it was.

public event Action<Breakable>? Broke

Raised as it breaks.

public event Action<Breakable, float, Entity>? Hit

Raised as a hit damages it, with the damage and what hit it.