Skip to main content

Triblast

Let's create a powerup that gives the player a triple shot for a limited duration. To do this, we are going to use a unique Easel feature called Collectors. Collectors are a way for one entity to give a value to another, but only as long as they are alive. They are often used to create short-lived effects like speed boosts, temporary invincibility, or in this case, a triple shot.

Triblast effect

To create the triblast effect, we are going to add a Collector that temporarily increases the number of plasma bolts that are fired when the player presses Spacebar.

Adding the collector

Select your ship.easel file. Insert the following highlighted code at the very top:

pub collect ship.NumPlasmaBolts = 1
signal ship.ReadyToPlay

pub fn ship.Ship([owner]) {
// ...
}

Parameterizing the plasma bolt

We want the three bolts of triblast to spread out slightly. To achieve this, let's add a headingOffset parameter to the plasma bolt function so we can tell it to spread out.

Select your plasmaBolt.easel file. Go to your PlasmaBolt function, find the function signature (the first line) and add a headingOffset=0 parameter to the parameter list, just after your ship parameter. Once you are done, your line should look like the following:

pub fn projectile.PlasmaBolt(ship, headingOffset=0, [owner]) {
// ...
}

Modifying the plasma bolt velocity

Now let's change the initial velocity of the plasma bolts to respect the new headingOffset parameter.

In plasmaBolt.easel, replace the velocity parameter of your Body function call with the following highlighted code:

pub fn projectile.PlasmaBolt(ship, headingOffset=0, [owner]) {
use body=projectile
use speed=50, radius=0.25
use color=#ffffff, ownerColor=true
use luminous=1, layer=-5

Body(
pos = ship.Pos,
velocity = speed * Direction(ship.Heading + headingOffset),
bullet=true,
)

// ...
}

Firing multiple plasma bolts

Now let's make the ship fire multiple plasma bolts. We will do this by placing the plasma bolt spawning code inside a for loop, which is one way to make a block of code repeat multiple times.

Select your ship.easel file. Modify your Ship function as follows. You will need to replace the code that was previously there and replace it with the highlighted code.

pub fn ship.Ship([owner]) {
// ...

on ButtonDown(KeySpacebar) {
ReadyToPlay

const AnglePerBolt = 0.03rev
let initialAngle = -(AnglePerBolt * (NumPlasmaBolts - 1)) / 2
for i in Range(0, NumPlasmaBolts) {
Spawn projectile {
PlasmaBolt(ship=, headingOffset = initialAngle + i*AnglePerBolt)
}
}
await Tick(0.1s)
}
}
info

The for loop allows us to repeat a block of code multiple times. It is common to combine a for loop with a Range or RangeInclusive to repeat a calculation over a range of numbers.

We haven't yet created the triblast powerup. We will do that next.

Triblast powerup

We will need a triblast powerup which the player can pickup to activate the upgrade. To create a short-lived effect in Easel, you spawn a short-lived entity, and so that is what we will do. We will create an entity that only lives for 10 seconds, and only while it is alive, it will give the player the triblast effect.

Defining the triblast powerup

Create a new file called triblast.easel, and fill it with the following code:

pub fn powerup.TriblastPowerup {
use body=powerup
use radius=1.5, speed=10, luminous=1

Body(
pos = (BoundaryRadius + radius + 5) * RandomVector,
velocity = speed*RandomVector,
)

PolygonSprite(shape=Circle, color=#00ffaa, opacity=0.5, bloom=3, shadow=0.5)
ImageSprite(@triblast.svg, radius=0.85*radius)
PolygonCollider(shape=Circle, category=Category:Powerup, collideWith=Category:None, sense=Category:Ship, density=1)
RecoverSpeed
DecayTurnRate

WrapOutsideBoundary

on BeforeCollide that {
if that.Category.Overlaps(Category:Ship) {
use ship = that

ship.Subspawn {
once Tick(10s) { Expire }

give NumPlasmaBolts(3)

BottomLeftContent {
HStack(padding=1) {
Pip(backgroundColor=#dd00ff) { Image(@triblast.svg, height=2, shadow=0.5) }
"You wield the mighty Triblast!"
}
}
}
Expire
break
}
}
}

Adding the triblast graphics

Next we need to insert the image file used for the triblast powerup. Insert the following file into your project. Right click then Save link as..., then drag-drop it into the file list of your project.

Spawning the triblast powerup

Finally, we need to spawn the Triblast powerup. For testing, let's replace the Shield powerup with the Triblast powerup. We will add the Shield back later.

Select your main.easel file. In your Main function, replace the ShieldPowerup with TriblastPowerup with the highlighted code below.

pub game fn World.Main() {
// ...

with Tick(5s) {
const MaxPowerups = 1
if QueryCount(filter=Category:Powerup) < MaxPowerups {
Spawn powerup {
TriblastPowerup
}
}
}

// ...
}

Click Preview. You should soon see a Triblast powerup appear. Catch the powerup and start firing your weapon to see the triple shot effect in action.

info

This uses a feature called Collectors to give the player the triblast effect. The effect entity only lives for 10 seconds, but while it is alive, it gives a value of 3 to the NumPlasmaBolts collector, changing the behavior of the player's ship to fire three plasma bolts instead of one. See Collectors to learn more.

Randomly choosing between powerups

Let's make it so that either a triblast or shield powerup appears randomly.

In main.easel, within your Main function, replace the code inside Spawn powerup with the highlighted code below:

pub game fn World.Main() {
// ...

with Tick(5s) {
const MaxPowerups = 1
if QueryCount(filter=Category:Powerup) < MaxPowerups {
Spawn powerup {
surprise {
ShieldPowerup,
TriblastPowerup,
}
}
}
}

// ...
}
info

A surprise block is a way to randomly choose between multiple options. In this case, we are randomly choosing between a ShieldPowerup and a TriblastPowerup. You can even give different weights to each potential option - see surprise to learn more.

Click Preview and start playing. You should see either a Triblast or Shield powerup appear. Try exiting and re-entering the game a few times to see different powerups appear.