Tutorial: Meadow Quest (2D RPG)

10. Combat & health

Slimes that chase you, a sword to swing, three hearts, a moment of invulnerability after a hit, and respawning.

Swinging the sword at a slime; two hearts left

The rules

  • Two slimes guard the coins. Within 4 units of the hero they chase it.
  • Touching a slime costs a heart and knocks the hero back. For one second after, the hero can't be hurt again.
  • With the sword, Space swings it in the direction you last moved. A slime within 1.5 units of the swing is defeated.
  • At zero hearts the hero respawns at the start with three hearts. (In Part 14 this becomes a proper game over.)

Shared state lives in globals: hp (hearts left), hurtUntil (the time the hero can be hurt again), hasSword, and slain.

How to add the slimes

For each of Slime1 at (-4, -3.5) and Slime2 at (4.5, -4.5):

  1. A 2D Sprite, image mq-slime, Z-Order 9.
  2. Add Component > Physics Body 2D: Gravity scale 0, tick Fixed rotation and Never sleeps, collider Half width 0.4, Half height 0.35. Slimes are physics bodies, so walls stop them and they shove the hero.
  3. Script Slime:
tgscript
//!tgscript v1
//! script name=Slime
//! region graph
//! node 1 OnUpdate pos=(60,80)
export function onUpdate(ctx) {
    //! node 2 FindEntity pos=(60,240) entity=Hero
    const n2_value = ctx.findEntity("Hero");
    //! node 3 GetEntityPosition2D pos=(260,240)
    const n3_value = ctx.getEntityPosition2D(n2_value);
    //! node 4 GetPosition2D pos=(260,360)
    const n4_value = ctx.getPosition2D();
    //! node 5 SubVec2 pos=(460,300)
    const n5_out = ctx.subVec2(n3_value, n4_value);
    //! node 6 Vec2Length pos=(660,300)
    const n6_out = ctx.vec2Length(n5_out);
    //! node 7 Vec2Normalize pos=(660,420)
    const n7_out = ctx.vec2Normalize(n5_out);
    //! node 8 ConstNumber pos=(660,540)
    const n8_value = 4.00;
    //! node 9 Less pos=(860,300)
    const n9_out = (n6_out < n8_value);
    //! node 10 GetGlobal pos=(660,640) global=started
    const n10_value = ctx.getGlobal("started");
    //! node 11 ConstNumber pos=(660,740)
    const n11_value = 0.50;
    //! node 12 Greater pos=(860,420)
    const n12_out = (n10_value > n11_value);
    //! node 13 And pos=(1060,300)
    const n13_out = (n9_out && n12_out);
    //! node 29 ConstNumber pos=(860,840)
    const n29_value = 1.00;
    //! node 36 Time pos=(860,940)
    const n36_value = ctx.time();
    //! node 14 Branch pos=(1260,80)
    if (n13_out) {
        //! node 15 ConstNumber pos=(1060,420)
        const n15_value = 1.80;
        //! node 16 ScaleVec2 pos=(1260,320)
        const n16_out = ctx.scaleVec2(n7_out, n15_value);
        //! node 17 SetVelocity2D pos=(1460,80)
        ctx.setVelocity2D(n16_out);
    } else {
        //! node 18 ConstVec2 pos=(1260,460)
        const n18_value = ctx.vec2(0.00, 0.00);
        //! node 19 SetVelocity2D pos=(1460,240)
        ctx.setVelocity2D(n18_value);
    }
    //! node 20 ActionPressed pos=(1060,560) action=Jump
    const n20_value = ctx.actionPressed("Jump");
    //! node 21 GetGlobal pos=(1060,660) global=hasSword
    const n21_value = ctx.getGlobal("hasSword");
    //! node 22 Greater pos=(1260,620)
    const n22_out = (n21_value > n11_value);
    //! node 23 ConstNumber pos=(1060,760)
    const n23_value = 1.50;
    //! node 24 Less pos=(1260,740)
    const n24_out = (n6_out < n23_value);
    //! node 25 And pos=(1460,600)
    const n25_out = (n20_value && n22_out);
    //! node 26 And pos=(1660,640)
    const n26_out = (n25_out && n24_out);
    //! node 27 Branch pos=(1860,80)
    if (n26_out) {
        //! node 28 GetGlobal pos=(1860,240) global=slain
        const n28_value = ctx.getGlobal("slain");
        //! node 30 AddNumber pos=(2060,240)
        const n30_out = (n28_value + n29_value);
        //! node 31 SetGlobal pos=(2260,80) global=slain
        ctx.setGlobal("slain", n30_out);
        //! node 32 ConstVec2 pos=(2260,240)
        const n32_value = ctx.vec2(200.00, 230.00);
        //! node 33 SetPosition2D pos=(2460,80)
        ctx.setPosition2D(n32_value);
    }
    //! node 34 ConstNumber pos=(1660,860)
    const n34_value = 0.95;
    //! node 35 Less pos=(1860,860)
    const n35_out = (n6_out < n34_value);
    //! node 37 GetGlobal pos=(1660,960) global=hurtUntil
    const n37_value = ctx.getGlobal("hurtUntil");
    //! node 38 Greater pos=(1860,960)
    const n38_out = (n36_value > n37_value);
    //! node 39 And pos=(2060,900)
    const n39_out = (n35_out && n38_out);
    //! node 40 Branch pos=(2660,80)
    if (n39_out) {
        //! node 41 GetGlobal pos=(2660,240) global=hp
        const n41_value = ctx.getGlobal("hp");
        //! node 42 SubNumber pos=(2860,240)
        const n42_out = (n41_value - n29_value);
        //! node 43 SetGlobal pos=(3060,80) global=hp
        ctx.setGlobal("hp", n42_out);
        //! node 44 AddNumber pos=(2860,360)
        const n44_out = (n36_value + n29_value);
        //! node 45 SetGlobal pos=(3260,80) global=hurtUntil
        ctx.setGlobal("hurtUntil", n44_out);
        //! node 46 ConstNumber pos=(2860,480)
        const n46_value = 1.20;
        //! node 47 ScaleVec2 pos=(3060,440)
        const n47_out = ctx.scaleVec2(n7_out, n46_value);
        //! node 48 AddVec2 pos=(3260,400)
        const n48_out = ctx.addVec2(n3_value, n47_out);
        //! node 49 SetEntityPosition2D pos=(3460,80)
        ctx.setEntityPosition2D(n2_value, n48_out);
    }
}
//! endregion

The slime sets its velocity; the physics step moves it and resolves collisions. Defeat is a move far away (spawn and destroy are coming).

How to add the sword

A 2D Sprite Sword (image mq-sword, Z-Order 11), parked at (200, 250), with this script. While you have no sword it stays parked. Once you do, it rides at the hero's side and, for 0.15 seconds after Space, swings out in the direction you last moved.

tgscript
//!tgscript v1
//! script name=Sword
//! region graph
//! node 1 OnStart pos=(60,80)
export function onStart(ctx) {
    //! node 2 ConstVec3 pos=(260,80)
    const n2_value = ctx.vec3(0.00, -1.00, 0.00);
    //! node 3 SetVarVec3 pos=(460,80) var=face
    ctx.setVarVec3("face", n2_value);
}
//! node 10 OnUpdate pos=(60,320)
export function onUpdate(ctx) {
    //! node 11 GetGlobal pos=(60,460) global=hasSword
    const n11_value = ctx.getGlobal("hasSword");
    //! node 12 ConstNumber pos=(60,560)
    const n12_value = 0.50;
    //! node 13 Less pos=(260,460)
    const n13_out = (n11_value < n12_value);
    //! node 14 Branch pos=(460,320)
    if (n13_out) {
        //! node 15 ConstVec2 pos=(460,480)
        const n15_value = ctx.vec2(200.00, 250.00);
        //! node 16 SetPosition2D pos=(660,320)
        ctx.setPosition2D(n15_value);
    } else {
        //! node 17 ActionVector2 pos=(460,640) action=Move
        const n17_value = ctx.actionVector2("Move");
        //! node 18 Vec3Length pos=(660,640)
        const n18_out = ctx.vec3Length(n17_value);
        //! node 19 ConstNumber pos=(660,740)
        const n19_value = 0.10;
        //! node 20 Greater pos=(860,640)
        const n20_out = (n18_out > n19_value);
        //! node 21 Branch pos=(860,480)
        if (n20_out) {
            //! node 22 SetVarVec3 pos=(1060,480) var=face
            ctx.setVarVec3("face", n17_value);
        }
        //! node 23 ActionPressed pos=(1060,640) action=Jump
        const n23_value = ctx.actionPressed("Jump");
        //! node 24 Time pos=(1060,740)
        const n24_value = ctx.time();
        //! node 25 Branch pos=(1260,480)
        if (n23_value) {
            //! node 26 SetVar pos=(1460,480) var=swingAt
            ctx.setVar("swingAt", n24_value);
        }
        //! node 27 FindEntity pos=(1260,800) entity=Hero
        const n27_value = ctx.findEntity("Hero");
        //! node 28 GetEntityPosition2D pos=(1460,800)
        const n28_value = ctx.getEntityPosition2D(n27_value);
        //! node 29 GetVarVec3 pos=(1260,900) var=face
        const n29_value = ctx.getVarVec3("face");
        //! node 30 Vec3X pos=(1460,900)
        const n30_out = ctx.vec3X(n29_value);
        //! node 31 Vec3Y pos=(1460,1000)
        const n31_out = ctx.vec3Y(n29_value);
        //! node 32 MakeVec2 pos=(1660,940)
        const n32_out = ctx.makeVec2(n30_out, n31_out);
        //! node 33 Vec2Normalize pos=(1860,940)
        const n33_out = ctx.vec2Normalize(n32_out);
        //! node 34 GetVar pos=(1460,1100) var=swingAt
        const n34_value = ctx.getVar("swingAt");
        //! node 35 SubNumber pos=(1660,1100)
        const n35_out = (n24_value - n34_value);
        //! node 36 ConstNumber pos=(1660,1200)
        const n36_value = 0.15;
        //! node 37 Less pos=(1860,1100)
        const n37_out = (n35_out < n36_value);
        //! node 38 Branch pos=(2060,480)
        if (n37_out) {
            //! node 39 ConstNumber pos=(2060,640)
            const n39_value = 0.80;
            //! node 40 ScaleVec2 pos=(2260,640)
            const n40_out = ctx.scaleVec2(n33_out, n39_value);
            //! node 41 AddVec2 pos=(2460,640)
            const n41_out = ctx.addVec2(n28_value, n40_out);
            //! node 42 SetPosition2D pos=(2660,480)
            ctx.setPosition2D(n41_out);
            //! node 43 Vec2ToAngle pos=(2260,760)
            const n43_out = ctx.vec2ToAngle(n33_out);
            //! node 44 ConstNumber pos=(2260,860)
            const n44_value = -45.00;
            //! node 45 AddNumber pos=(2460,800)
            const n45_out = (n43_out + n44_value);
            //! node 46 SetAngle2D pos=(2860,480)
            ctx.setAngle2D(n45_out);
        } else {
            //! node 47 ConstVec2 pos=(2260,960)
            const n47_value = ctx.vec2(0.45, -0.15);
            //! node 48 AddVec2 pos=(2460,960)
            const n48_out = ctx.addVec2(n28_value, n47_value);
            //! node 49 SetPosition2D pos=(2660,640)
            ctx.setPosition2D(n48_out);
            //! node 50 ConstNumber pos=(2460,1060)
            const n50_value = 0.00;
            //! node 51 SetAngle2D pos=(2860,640)
            ctx.setAngle2D(n50_value);
        }
    }
}
//! endregion

It remembers the last direction in a per-entity variable (face, set to "down" in OnStart) and the swing time in another (swingAt), compared against Time.

How to add hearts and respawning

  1. Three 2D Sprites Heart1, Heart2, Heart3, image mq-heart, Scale 0.5, 0.5 (the image is 8×8 pixels, so this keeps 16 px per unit), Z-Order 71.
  2. A 2D Sprite Hud with its Tint A set to 0 (it only carries the script), and script Health. OnStart sets hp to 3. Every frame it places each heart on the camera if hp is high enough, respawns the hero when hp reaches 0, and drinks a potion on Q (Part 11).
tgscript
//!tgscript v1
//! script name=Health
//! region graph
//! node 1 OnStart pos=(60,80)
export function onStart(ctx) {
    //! node 2 ConstNumber pos=(260,80)
    const n2_value = 3.00;
    //! node 3 SetGlobal pos=(460,80) global=hp
    ctx.setGlobal("hp", n2_value);
}
//! node 10 OnUpdate pos=(60,320)
export function onUpdate(ctx) {
    //! node 11 FindEntity pos=(60,460) entity=Camera
    const n11_value = ctx.findEntity("Camera");
    //! node 12 GetEntityPosition2D pos=(260,460)
    const n12_value = ctx.getEntityPosition2D(n11_value);
    //! node 13 GetGlobal pos=(60,560) global=hp
    const n13_value = ctx.getGlobal("hp");
    //! node 14 GetGlobal pos=(60,660) global=started
    const n14_value = ctx.getGlobal("started");
    //! node 15 ConstNumber pos=(60,760)
    const n15_value = 0.50;
    //! node 16 Greater pos=(260,660)
    const n16_out = (n14_value > n15_value);
    //! node 17 ConstVec2 pos=(260,760)
    const n17_value = ctx.vec2(200.00, 260.00);
    //! node 31 FindEntity pos=(260,900) entity=Heart1
    const n31_value = ctx.findEntity("Heart1");
    //! node 32 ConstVec2 pos=(260,1000)
    const n32_value = ctx.vec2(-0.70, 3.70);
    //! node 33 AddVec2 pos=(460,1000)
    const n33_out = ctx.addVec2(n12_value, n32_value);
    //! node 34 ConstNumber pos=(260,1100)
    const n34_value = 0.50;
    //! node 35 Greater pos=(460,1100)
    const n35_out = (n13_value > n34_value);
    //! node 36 And pos=(660,1100)
    const n36_out = (n35_out && n16_out);
    //! node 37 Branch pos=(1060,320)
    if (n36_out) {
        //! node 38 SetEntityPosition2D pos=(1260,320)
        ctx.setEntityPosition2D(n31_value, n33_out);
    } else {
        //! node 39 SetEntityPosition2D pos=(1260,480)
        ctx.setEntityPosition2D(n31_value, n17_value);
    }
    //! node 40 FindEntity pos=(260,1320) entity=Heart2
    const n40_value = ctx.findEntity("Heart2");
    //! node 41 ConstVec2 pos=(260,1420)
    const n41_value = ctx.vec2(0.00, 3.70);
    //! node 42 AddVec2 pos=(460,1420)
    const n42_out = ctx.addVec2(n12_value, n41_value);
    //! node 43 ConstNumber pos=(260,1520)
    const n43_value = 1.50;
    //! node 44 Greater pos=(460,1520)
    const n44_out = (n13_value > n43_value);
    //! node 45 And pos=(660,1520)
    const n45_out = (n44_out && n16_out);
    //! node 46 Branch pos=(1560,320)
    if (n45_out) {
        //! node 47 SetEntityPosition2D pos=(1760,320)
        ctx.setEntityPosition2D(n40_value, n42_out);
    } else {
        //! node 48 SetEntityPosition2D pos=(1760,480)
        ctx.setEntityPosition2D(n40_value, n17_value);
    }
    //! node 49 FindEntity pos=(260,1740) entity=Heart3
    const n49_value = ctx.findEntity("Heart3");
    //! node 50 ConstVec2 pos=(260,1840)
    const n50_value = ctx.vec2(0.70, 3.70);
    //! node 51 AddVec2 pos=(460,1840)
    const n51_out = ctx.addVec2(n12_value, n50_value);
    //! node 52 ConstNumber pos=(260,1940)
    const n52_value = 2.50;
    //! node 53 Greater pos=(460,1940)
    const n53_out = (n13_value > n52_value);
    //! node 54 And pos=(660,1940)
    const n54_out = (n53_out && n16_out);
    //! node 55 Branch pos=(2060,320)
    if (n54_out) {
        //! node 56 SetEntityPosition2D pos=(2260,320)
        ctx.setEntityPosition2D(n49_value, n51_out);
    } else {
        //! node 57 SetEntityPosition2D pos=(2260,480)
        ctx.setEntityPosition2D(n49_value, n17_value);
    }
    //! node 58 Less pos=(260,2300)
    const n58_out = (n13_value < n15_value);
    //! node 59 And pos=(460,2300)
    const n59_out = (n58_out && n16_out);
    //! node 60 Branch pos=(2600,320)
    if (n59_out) {
        //! node 61 FindEntity pos=(2600,480) entity=Hero
        const n61_value = ctx.findEntity("Hero");
        //! node 62 ConstVec2 pos=(2600,580)
        const n62_value = ctx.vec2(0.00, -8.00);
        //! node 63 SetEntityPosition2D pos=(2800,320)
        ctx.setEntityPosition2D(n61_value, n62_value);
        //! node 64 ConstNumber pos=(2800,480)
        const n64_value = 3.00;
        //! node 65 SetGlobal pos=(3000,320) global=hp
        ctx.setGlobal("hp", n64_value);
    }
    //! node 66 KeyDown pos=(260,2500) key=Q
    const n66_value = ctx.keyDown("Q");
    //! node 67 GetGlobal pos=(260,2600) global=potions
    const n67_value = ctx.getGlobal("potions");
    //! node 68 Greater pos=(460,2600)
    const n68_out = (n67_value > n15_value);
    //! node 69 ConstNumber pos=(260,2700)
    const n69_value = 2.50;
    //! node 70 Less pos=(460,2700)
    const n70_out = (n13_value < n69_value);
    //! node 71 And pos=(660,2550)
    const n71_out = (n66_value && n68_out);
    //! node 72 And pos=(860,2600)
    const n72_out = (n71_out && n70_out);
    //! node 73 Branch pos=(3200,320)
    if (n72_out) {
        //! node 74 ConstNumber pos=(3200,480)
        const n74_value = 3.00;
        //! node 75 SetGlobal pos=(3400,320) global=hp
        ctx.setGlobal("hp", n74_value);
        //! node 76 ConstNumber pos=(3200,580)
        const n76_value = 1.00;
        //! node 77 SubNumber pos=(3400,580)
        const n77_out = (n67_value - n76_value);
        //! node 78 SetGlobal pos=(3600,320) global=potions
        ctx.setGlobal("potions", n77_out);
    }
}
//! endregion

Good practices

  • Let physics move fighters. Slimes set velocity and have bodies, so they never pass through walls or each other. A script that teleports enemies around fights the physics engine.
  • Give hits a cooldown. Without hurtUntil, a slime touching the hero would take a heart every frame (50 per second).
  • One source of truth for health. Only scripts write hp; the hearts just display it. To add armour or a bigger health bar, change the numbers, not the drawing code.
  • Reset in OnStart. Globals reset when you press Stop; OnStart puts back the starting values when you press Play.
  • Split big scripts by job. Health and inventory are separate scripts on separate entities, so each graph stays readable.

This part's assets

Slime.tgscript · Sword.tgscript · Health.tgscript · mq-slime.png · mq-sword.png · mq-heart.png (each image with its .mt).

Next: inventory →