Building a Basic Game
To get you started, this guide walks you through building an apple collection game!
Last updated
// This creates the component that makes the apple interactable,
// we'll add it to the Apple in the next step of the tutorial.
Pickup_Apple :: class : Interactable {
is_picked_up: bool;
ao_start :: method() {
this.set_listener(this);
this.set_text("Pick up");
}
can_use :: method(player: Player) -> bool {
if is_picked_up return false;
return true;
}
on_interact :: method(player: Player) {
if is_picked_up return;
is_picked_up = true;
// Start the pop effect
effect := new(Apple_Pop_Effect);
entity.set_active_effect(effect);
}
}
// A cool animation for when you eat it :D
Apple_Pop_Effect :: class : Effect_Base {
sprite: Sprite_Renderer;
start_scale: v2;
start_pos: v2;
effect_start :: method() {
sprite = entity.get_component(Sprite_Renderer);
start_scale = entity.local_scale;
start_pos = entity.local_position;
set_duration(0.4);
}
effect_update :: method(dt: float) {
t := get_elapsed_time() / 0.4;
// Scale up then quickly shrink to nothing
scale_curve: float;
if t < 0.3 {
// Quick pop up (scale to 1.3x)
scale_curve = lerp(1.0, 1.3, Ease.out_back(t / 0.3));
} else {
// Shrink to nothing
scale_curve = lerp(1.3, 0.0, Ease.in_back((t - 0.3) / 0.7));
}
entity.set_local_scale(start_scale * scale_curve);
// Float upward slightly
rise := Ease.out_quad(t) * 0.5;
entity.set_local_position({start_pos.x, start_pos.y + rise});
// Fade out near the end
if sprite != null {
alpha := 1.0 - Ease.in_quad(max(0.0, (t - 0.5) / 0.5));
sprite.color.w = alpha;
}
}
effect_end :: method(interrupt: bool) {
entity.destroy();
}
}