summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: bf6eae400a869d5481f6f7a0f95d10f98ed9cd45 (plain)
#![no_std]
#![cfg_attr(target_arch = "wasm32", no_main)]

extern crate alloc;

mod detect_controller;
mod exit_on_escape;
mod move_snake;
mod render;
mod render_frametime;
mod snake_controller;
mod update_high_score;

use alloc::vec::Vec;
use core::time::Duration;
use tortuise_script::{
    entity::Entity,
    input::Gamepad,
    storage::{Config, Storage},
    time::Instant,
    TortuiseScene,
};

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Position {
    pub x: i16,
    pub y: i16,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct SaveData {
    high_score: usize,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct ConfigOptions {
    #[serde(default)]
    framerate_limit: Option<u32>,
}

const GRID_SIZE: u16 = 16;

static SAVE_DATA: Storage<SaveData> = Storage::new(SaveData::default);
static CONFIG: Config<ConfigOptions> = Config::new(ConfigOptions::default);

static CONNECTED_GAMEPAD_QUEUE: Entity<Vec<Gamepad>> =
    Entity::new("connected_gamepad_queue", |_| Vec::new());
static IS_PAUSED: Entity<bool> = Entity::new("is_paused", |_| false);
static APPLE_LOCATION: Entity<Position> =
    Entity::new("apple_location", |_| Position { x: 2, y: 2 });
static SNAKE_CELLS: Entity<Vec<Position>> = Entity::new("snake_cells", |_| {
    alloc::vec![Position { x: 10, y: 5 }, Position { x: 9, y: 5 }]
});
static DIRECTION: Entity<Position> = Entity::new("direction", |_| Position { x: 1, y: 0 });
static NEXT_MOVE_TIME: Entity<Instant> = Entity::new("next_move_time", |cx| {
    Instant::now(cx) + Duration::from_millis(200)
});

tortuise_script::game! {
    name: "snake",
    author: "botahamec",
    tld: "dev",
    first_scene: "main",
    scenes: {
        "main" => TortuiseScene {
            scripts: &[
                "frametime",
                "detect_controller",
                "update_high_score",
                "snake_controller",
                "exit_on_escape",
                "move_snake",
                "render"
            ],
        }
    },
    scripts: {
        "frametime" => render_frametime::RENDER_FRAMETIME,
        "detect_controller" => detect_controller::DETECT_CONTROLLER,
        "update_high_score" => update_high_score::UPDATE_HIGH_SCORE,
        "snake_controller" => snake_controller::SNAKE_CONTROLLER,
        "exit_on_escape" => exit_on_escape::EXIT_ON_ESCAPE,
        "move_snake" => move_snake::MOVE_SNAKE,
        "render" => render::RENDER
    }
}