This script implements a classic controller for horizontal spaceship shooters, inspired by games like Space Invaders, Phoenix, Galaga and Galaxian. The player only moves left and right and can fire with a controlled rate of fire.
// ======================================================
// 🚀 HORIZONTAL_SPACE_PLAYER.js — Horizontal movement + shooting
// ======================================================
let speed = 120;
// ------------------------------------------------------
// 🏁 Start — initialization
// ------------------------------------------------------
function Start(obj) {
obj.type = "player";
obj.vx = 0;
obj.vy = 0;
obj.canShoot = true; // prevents too fast continuous shooting
obj.shootDelay = 0.25; // minimum time between shots
obj.shootTimer = 0;
}
// ------------------------------------------------------
// 🔄 Update — main player logic
// ------------------------------------------------------
function Update(obj, dt) {
// ======================================================
// 🎮 MOVEMENT — keyboard + gamepad
// ======================================================
obj.vx = 0;
if (IsAnyPressed("ArrowLeft", "a", "gamepad_left", "gamepad_dpad_left")) {
obj.vx = -speed;
}
if (IsAnyPressed("ArrowRight", "d", "gamepad_right", "gamepad_dpad_right")) {
obj.vx = speed;
}
// apply horizontal movement
obj.x += obj.vx * dt;
// ======================================================
// 🧱 LIMIT movement within camera bounds
// ======================================================
const viewLeft = -offsetX;
const viewTop = -offsetY;
const viewRight = -offsetX + cameraWidth;
const viewBottom = -offsetY + cameraHeight;
obj.x = clamp(obj.x, viewLeft, viewRight - obj.w);
obj.y = clamp(obj.y, viewTop, viewBottom - obj.h);
// ======================================================
// 🔫 SHOOTING — keyboard + gamepad
// ======================================================
if (obj.shootTimer > 0) obj.shootTimer -= dt;
if (obj.shootTimer <= 0 &&
IsAnyPressed(" ", "gamepad_a", "gamepad_button_a")) {
CallFunction(obj, "Shoot");
obj.shootTimer = obj.shootDelay;
}
}
// ------------------------------------------------------
// 💥 OnTriggerEnter / OnCollisionEnter
// ------------------------------------------------------
function OnTriggerEnter(obj, other) {
// ...
}
function OnCollisionEnter(obj, other) {
// ...
}
// ------------------------------------------------------
// 🔫 Shooting function
// ------------------------------------------------------
function Shoot(obj) {
// Create bullet going upward, example:
// CreateObject("Bullet", obj.x + obj.w/2, obj.y - 10);
}
shootDelay prevents unlimited rapid fire.PlayAnimation(obj, "Shoot");