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

extern crate alloc;

mod exit_on_escape;
mod move_snake;
mod render;
mod snake_controller;

use alloc::vec::Vec;
use core::time::Duration;
use tortuise_script::{entity::Entity, time::Instant, TortuiseScene};

use serde::{Deserialize, Serialize};

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

const GRID_SIZE: u16 = 16;

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: &["snake_controller", "exit_on_escape", "move_snake", "render"],
        }
    },
    scripts: {
        "snake_controller" => snake_controller::SNAKE_CONTROLLER,
        "exit_on_escape" => exit_on_escape::EXIT_ON_ESCAPE,
        "move_snake" => move_snake::MOVE_SNAKE,
        "render" => render::RENDER
    }
}