module demos.snake;

import app;

import bindbc.sdl;

import cimgui.cimgui;

import bubel.ecs.attributes;
import bubel.ecs.core;
import bubel.ecs.entity;
import bubel.ecs.manager;
import bubel.ecs.std;
import bubel.ecs.vector;

import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;

import std.array : staticArray;

enum float px = 1.0/512.0;

extern(C):

struct MapElement
{
    enum Type
    {
        empty = 0,
        apple = 1,
        wall = 2,
        snake = 3,
        
       /* snake_head_up = 5,
        snake_head_down = 6,
        snake_head_left = 7,
        snake_head_right = 8,
        snake_tail_up = 9,
        snake_tail_down = 10,
        snake_tail_left = 11,
        snake_tail_right = 12,
        snake_turn_ld = 13,
        snake_turn_lu = 14,
        snake_turn_rd = 15,
        snake_turn_ru = 16,
        snake_vertical = 17,
        snake_horizontal = 18*/

    }
    Type type;
    EntityID id;
}

enum SnakePart : ubyte
{
    head_up = 0,
    head_down = 1,
    head_left = 2,
    head_right = 3,
    tail_up = 4,
    tail_down = 5,
    tail_left = 6,
    tail_right = 7,
    turn_ld = 8,
    turn_lu = 9,
    turn_rd = 10,
    turn_ru = 11,
    vertical = 12,
    horizontal = 13
}

struct Snake
{
    __gshared const (char)* tips = "Use \"WASD\" keys to move.";

    EntityTemplate* apple_tmpl;
    EntityTemplate* snake_tmpl;
    EntityTemplate* snake_destroy_particle;
    Texture texture;

    vec4[] snake_destroy_particle_frames;
    vec4[] smoke_frames;


    bool move_system = true;
    bool draw_system = true;

    const int map_size = 18;

    MapElement[map_size * map_size] map;

    ~this() @nogc nothrow
    {
        if(snake_destroy_particle_frames)Mallocator.dispose(snake_destroy_particle_frames);
        if(smoke_frames)Mallocator.dispose(smoke_frames);
        if(apple_tmpl)launcher.manager.freeTemplate(apple_tmpl);
        if(snake_tmpl)launcher.manager.freeTemplate(snake_tmpl);
        if(snake_destroy_particle)launcher.manager.freeTemplate(snake_destroy_particle);
        texture.destory();
    }

    MapElement element(ivec2 pos)
    {
        uint index = pos.x + pos.y * map_size;
        if(index >= map.length)index = map.length - 1;
        return map[index];
    }

    void element(MapElement el, ivec2 pos)
    {
        uint index = pos.x + pos.y * map_size;
        if(index >= map.length)index = map.length - 1;
        map[index] = el;
    }

    void addApple()
    {
        ivec2 random_pos = ivec2(rand()%map_size,rand()%map_size);
        ivec2 base_pos = random_pos;
        while(element(random_pos).type != MapElement.Type.empty)
        {
            random_pos.x += 1;
            if(random_pos.x > map_size)
            {
                random_pos.x = 0;
                random_pos.y += 1;
                if(random_pos.y > map_size)random_pos.y = 0;
            }
            if(base_pos.x == random_pos.x && base_pos.y == random_pos.y)return;
        }
        CILocation* location = apple_tmpl.getComponent!CILocation;
        *location = random_pos;
        Entity* apple = launcher.manager.addEntity(apple_tmpl);
    }
}

struct Animation
{

}

struct CAnimation
{
    mixin ECS.Component;
    
    vec4[] frames;
    float time = 0;
}

struct CILocation
{
    mixin ECS.Component;

    alias location this;

    ivec2 location;
}

struct CLocation
{
    mixin ECS.Component;

    alias location this;

    vec2 location = vec2(0,0);
}

struct CSnake
{
    void onCreate()
    {
        parts.array = Mallocator.makeArray!ivec2(100);
    }

    void onDestroy()
    {
        Mallocator.dispose(parts.array);
    }

    mixin ECS.Component;


    struct Parts
    {
        uint length = 0;
        ivec2[] array;

        ivec2 opIndex(size_t ind) const
        {
            return array[ind];
        }

        ivec2[] opSlice() 
        {
            return array[0 .. length];
        }

        void opIndexAssign(ivec2 vec, size_t ind) 
        {
            array[ind] = vec;
        }

        size_t opDollar() const 
        {
            return length;
        }

        void add(ivec2 v)
        {
            length++;
            array[length-1] = v;
        }
    }

    Parts parts;
    CMovement.Direction direction;
}

struct CApple
{
    mixin ECS.Component;
}

struct CParticle
{
    mixin ECS.Component;

    float life = 0;
}

struct CParticleVector
{
    mixin ECS.Component;

    vec2 velocity = vec2(0,0);
}

struct CMovement
{
    mixin ECS.Component;

    enum Direction : byte
    {
        up,
        down,
        left,
        right
    }   

    Direction direction;
}

struct CInput
{
    mixin ECS.Component;
}

struct AppleSystem
{
    mixin ECS.System!1;

    struct EntitiesData
    {
        uint length;
        @readonly Entity[] entities;
        @readonly CApple[] movement;
        @readonly CILocation[] location;
    }

    void onAddEntity(EntitiesData data)
    {
        foreach(i;0..data.length)
        {
            snake.element(MapElement(MapElement.Type.apple,data.entities[i].id),data.location[i]);
        }
    }
}

struct ParticleSystem
{
    mixin ECS.System!1;

    struct EntitiesData
    {
        uint length;
        @readonly Entity[] entities;
        @readonly CParticle[] particle;
    }

    void onUpdate(EntitiesData data)
    {
        foreach(i;0..data.length)
        {
            data.particle[i].life -= launcher.delta_time;
            if(data.particle[i].life < 0)launcher.manager.removeEntity(data.entities[i].id);
        }
    }
}

struct ParticleMovementSystem
{
    mixin ECS.System!1;

    struct EntitiesData
    {
        uint length;
        @readonly Entity[] entities;
        @readonly CParticleVector[] movement;
        CLocation[] location;
    }

    void onUpdate(EntitiesData data)
    {
        foreach(i;0..data.length)
        {
            data.location[i].location -= data.movement[i].velocity;
        }
    }
}


struct AnimationSystem
{
    mixin ECS.System!1;

    struct EntitiesData
    {
        uint length;
        CAnimation[] animation;
    }

    void onUpdate(EntitiesData data)
    {
        foreach(i;0..data.length)
        {
            data.animation[i].time += launcher.delta_time * 0.01;
            while(data.animation[i].time >= data.animation[i].frames.length)data.animation[i].time -= cast(float)data.animation[i].frames.length;
        }
    }
}


struct AnimationRenderSystem
{
    mixin ECS.System!1;

    struct EntitiesData
    {
        uint length;
        @readonly CAnimation[] animation;
        @readonly CLocation[] location;
    }

    void onUpdate(EntitiesData data)
    {
        foreach(i;0..data.length)
        {
            launcher.renderer.draw(snake.texture, cast(vec2)cast(ivec2)data.location[i].location, vec2(16,16), data.animation[i].frames[cast(int)(data.animation[i].time)], -1, 0x80808080);
        }
    }
}

struct MoveSystem
{
    mixin ECS.System!64;

    EntityTemplate* destroy_template;
    CLocation* destroy_location;
    CParticleVector* destroy_vector;

    struct EntitiesData
    {
        uint length;
        @readonly Entity[] entities;
        @readonly CMovement[] movement;
        @optional CSnake[] snakes;
        CILocation[] location;
    }

    void setTemplates()
    {
        destroy_template = snake.snake_destroy_particle;
        destroy_location = destroy_template.getComponent!CLocation;
        destroy_vector = destroy_template.getComponent!CParticleVector;
    }

    void moveLocation(ref CILocation location, CMovement.Direction direction)
    {
        final switch(direction)
        {
            case CMovement.Direction.down:
                location.y -= 1;
                if(location.y < 0)location.y = snake.map_size - 1;
                break;
            case CMovement.Direction.up:
                location.y += 1;
                if(location.y >= snake.map_size)location.y = 0;
                break;
            case CMovement.Direction.left:
                location.x -= 1;
                if(location.x < 0)location.x = snake.map_size - 1;
                break;
            case CMovement.Direction.right:
                location.x += 1;
                if(location.x >= snake.map_size)location.x = 0;
                break;
        }
    }

    void moveSnake(ref CSnake snake, ivec2 location)
    {
        if(snake.parts.length)
        {
            .snake.element(MapElement(),snake.parts[0]);
            foreach(j; 0 .. snake.parts.length - 1)
            {
                snake.parts[j] = snake.parts[j + 1];
            }
            snake.parts[$-1] = location;
        }
        else .snake.element(MapElement(),location);
    }

    void onUpdate(EntitiesData data)
    {
        if(data.snakes)
        {
            foreach(i; 0..data.length)
            {
                data.snakes[i].direction = data.movement[i].direction;
                ivec2 new_location = data.location[i];
                moveLocation(data.location[i], data.movement[i].direction);
                final switch(snake.element(data.location[i].location).type)
                {
                    case MapElement.Type.snake:
                        foreach(loc; data.snakes[i].parts)
                        {
                            destroy_location.x = loc.x * 16;
                            destroy_location.y = loc.y * 16;
                            snake.element(MapElement(MapElement.Type.empty, EntityID()),loc);
                            launcher.manager.addEntity(snake.snake_destroy_particle);
                            foreach(j;0..10)
                            {
                                destroy_location.x = loc.x * 16 + randomf() * 8 - 4;
                                destroy_location.y = loc.y * 16 + randomf() * 8 - 4;
                                destroy_vector.velocity = vec2(randomf(),randomf())*0.4-0.2;
                                snake.element(MapElement(MapElement.Type.empty, EntityID()),loc);
                                launcher.manager.addEntity(snake.snake_destroy_particle);
                            }
                            
                        }
                        destroy_location.x = new_location.x * 16;
                        destroy_location.y = new_location.y * 16;
                        snake.element(MapElement(MapElement.Type.empty, EntityID()),new_location);
                        launcher.manager.addEntity(snake.snake_destroy_particle);
                        launcher.manager.removeEntity(data.entities[i].id);
                        break;

                    case MapElement.Type.wall:break;

                    case MapElement.Type.empty:
                        moveSnake(data.snakes[i], new_location);
                        snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.location[i].location);
                        if(data.snakes[i].parts.length > 1)
                        {
                            snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[$-1]);
                            snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[0]);
                        }
                        else if(data.snakes[i].parts.length == 1)
                        {
                            snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[0]);
                        }
                        break;
                    case MapElement.Type.apple:
                        launcher.manager.removeEntity(snake.element(data.location[i].location).id);
                        if(data.snakes[i].parts.length < 100)data.snakes[i].parts.add(new_location);

                        if(data.snakes[i].parts.length > 1)
                        {
                            snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[$-1]);
                        }
                        else if(data.snakes[i].parts.length == 1)
                        {
                            snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),new_location);
                        }
                        snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.location[i].location);
                        snake.addApple();
                        break;
                }
            }
        }
        else
        {
            foreach(i; 0..data.length)
            {
                final switch(data.movement[i].direction)
                {
                    case CMovement.Direction.down:data.location[i].y -= 1;break;
                    case CMovement.Direction.up:data.location[i].y += 1;break;
                    case CMovement.Direction.left:data.location[i].x -= 1;break;
                    case CMovement.Direction.right:data.location[i].x += 1;break;
                }
            }
        }
        
    }
}

struct InputSystem
{
    mixin ECS.System!64;

    struct EntitiesData
    {
        uint length;
        CMovement[] movement;
        @readonly CInput[] input;
    }

    void onUpdate(EntitiesData data)
    {
        foreach(i; 0..data.length)
        {
            if(launcher.getKeyState(SDL_SCANCODE_W))
            {
                data.movement[i].direction = CMovement.Direction.up;
            }
            else if(launcher.getKeyState(SDL_SCANCODE_S))
            {
                data.movement[i].direction = CMovement.Direction.down;
            }
            else if(launcher.getKeyState(SDL_SCANCODE_A))
            {
                data.movement[i].direction = CMovement.Direction.left;
            }
            else if(launcher.getKeyState(SDL_SCANCODE_D))
            {
                data.movement[i].direction = CMovement.Direction.right;
            }
        }
    }
}

struct FixSnakeDirectionSystem
{
    mixin ECS.System!64;

    struct EntitiesData
    {
        uint length;
        CMovement[] movement;
        @readonly CILocation[] location;
        const (CSnake)[] snake;
    }

    void onUpdate(EntitiesData data)
    {
        foreach(i; 0..data.length)
        {
            ivec2 last_location;
            if(data.snake[i].parts.length)last_location = data.snake[i].parts[$ - 1];
            else continue;
            ivec2 next_location = data.location[i];
            
            final switch(data.movement[i].direction)
            {
                case CMovement.Direction.up:
                    next_location.y += 1;
                    if(next_location.y >= snake.map_size)next_location.y = 0;
                    if(next_location.x == last_location.x && next_location.y == last_location.y)
                    {
                        data.movement[i].direction = CMovement.Direction.down;
                    }
                    break;
                case CMovement.Direction.down:
                    next_location.y -= 1;
                    if(next_location.y < 0)next_location.y = snake.map_size - 1;
                    if(next_location.x == last_location.x && next_location.y == last_location.y)
                    {
                        data.movement[i].direction = CMovement.Direction.up;
                    }
                    break;
                case CMovement.Direction.left:
                    next_location.x -= 1;
                    if(next_location.x < 0)next_location.x = snake.map_size - 1;
                    if(next_location.x == last_location.x && next_location.y == last_location.y)
                    {
                        data.movement[i].direction = CMovement.Direction.right;
                    }
                    break;
                case CMovement.Direction.right:
                    next_location.x += 1;
                    if(next_location.x >= snake.map_size)next_location.x = 0;
                    if(next_location.x == last_location.x && next_location.y == last_location.y)
                    {
                        data.movement[i].direction = CMovement.Direction.left;
                    }
                    break;
            }
        }
    }
}

struct DrawAppleSystem
{
    mixin ECS.System!1;

    struct EntitiesData
    {
        uint length;
        @readonly CILocation[] location;
        const (CApple)[] apple;
    }

    void onUpdate(EntitiesData data)
    {
        foreach(i; 0..data.location.length)
        {
            launcher.renderer.draw(snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(0,32*px,16*px,16*px), 0, 0x80808080, 0);
        }
    }
}

struct DrawSnakeSystem
{
    mixin ECS.System!1;

    struct EntitiesData
    {
        uint length;
        @readonly CILocation[] location;
        const (CSnake)[] snake;
    }

    static CMovement.Direction getDirection(ivec2 p1, ivec2 p2)
    {
        if(p1.x - p2.x == -1)return CMovement.direction.right;
        else if(p1.x - p2.x == 1)return CMovement.direction.left;
        else if(p1.y - p2.y == -1)return CMovement.direction.up;
        else if(p1.y - p2.y == 1)return CMovement.direction.down;
        else if(p1.x - p2.x > 1)return CMovement.direction.right;
        else if(p1.x - p2.x < -1)return CMovement.direction.left;
        else if(p1.y - p2.y > 1)return CMovement.direction.up;
        else return CMovement.direction.down;
    }

    static SnakePart snakePart(ivec2 p1, ivec2 p2, ivec2 p3)
    {
        CMovement.Direction direction = getDirection(p1, p2);
        CMovement.Direction direction2 = getDirection(p1, p3);
        uint case_ = direction*4 + direction2;
        final switch(case_)
        {
            case 0:return SnakePart.horizontal;
            case 1:return SnakePart.horizontal;
            case 2:return SnakePart.turn_lu;
            case 3:return SnakePart.turn_ru;
            case 4:return SnakePart.horizontal;
            case 5:return SnakePart.horizontal;
            case 6:return SnakePart.turn_ld;
            case 7:return SnakePart.turn_rd;
            case 8:return SnakePart.turn_lu;
            case 9:return SnakePart.turn_ld;
            case 10:return SnakePart.vertical;
            case 11:return SnakePart.vertical;
            case 12:return SnakePart.turn_ru;
            case 13:return SnakePart.turn_rd;
            case 14:return SnakePart.vertical;
            case 15:return SnakePart.vertical;
        }
    }

    static SnakePart snakeTail(ivec2 p1, ivec2 p2)
    {
        CMovement.Direction direction = getDirection(p1, p2);
        final switch(direction)
        {
            case CMovement.Direction.up:return SnakePart.tail_up;
            case CMovement.Direction.down:return SnakePart.tail_down;
            case CMovement.Direction.left:return SnakePart.tail_left;
            case CMovement.Direction.right:return SnakePart.tail_right;
        }
    }

    static void drawElement(ivec2 loc, SnakePart part)
    {
        final switch(cast(ubyte)part)
        {
            case SnakePart.tail_up:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,112,16,16)*px, 0, 0x80808080, 0);break;
            case SnakePart.tail_down:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(0,112,16,16)*px, 0, 0x80808080, 0);break;
            case SnakePart.tail_left:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(32,112,16,16)*px, 0, 0x80808080, 0);break;
            case SnakePart.tail_right:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(0,144,16,16)*px, 0, 0x80808080, 0);break;
            case SnakePart.turn_ld:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(64,128,16,16)*px, 0, 0x80808080, 0);break;
            case SnakePart.turn_lu:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(32,144,16,16)*px, 0, 0x80808080, 0);break;
            case SnakePart.turn_rd:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,144,16,16)*px, 0, 0x80808080, 0);break;
            case SnakePart.turn_ru:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(64,112,16,16)*px, 0, 0x80808080, 0);break;
            case SnakePart.vertical:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,128,16,16)*px, 0, 0x80808080, 0);break;
            case SnakePart.horizontal:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(48,128,16,16)*px, 0, 0x80808080, 0);break;
        }
    }

    void onUpdate(EntitiesData data)
    {
        foreach(i; 0..data.length)
        {
            const (CSnake)* snake = &data.snake[i];
            scope vec2 loc = cast(vec2)(data.location[i].location * 16);
            final switch(snake.direction)
            {
                case CMovement.Direction.up:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(48,112,16,16)*px, 0, 0x80808080, 0);break;
                case CMovement.Direction.down:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(48,144,16,16)*px, 0, 0x80808080, 0);break;
                case CMovement.Direction.left:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(0,128,16,16)*px, 0, 0x80808080, 0);break;
                case CMovement.Direction.right:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(32,128,16,16)*px, 0, 0x80808080, 0);break;
            }
            if(snake.parts.length >1)
            {
                foreach(j;1..snake.parts.length - 1)drawElement(snake.parts[j]*16, snakePart(snake.parts[j], snake.parts[j+1], snake.parts[j-1]));
                drawElement(snake.parts[$-1]*16, snakePart(snake.parts[$-1], data.location[i], snake.parts[$-2]));
                drawElement(snake.parts[0]*16, snakeTail(snake.parts[1], snake.parts[0]));
            }
            else if(snake.parts.length == 1)
            {
                drawElement(snake.parts[0]*16, snakeTail(data.location[i], snake.parts[0]));
            }
            
        }
    }
}

struct CleanSystem
{
    mixin ECS.System!64;

    struct EntitiesData
    {
        uint length;
        Entity[] entities;
    }

    void onUpdate(EntitiesData data)
    {
        foreach(i; 0..data.length)
        {
            launcher.manager.removeEntity(data.entities[i].id);
        }
    }
}

__gshared Snake* snake;

void snakeStart()
{
    snake = Mallocator.make!Snake;

    snake.texture.create();
    snake.texture.load("assets/textures/atlas.png");

    launcher.manager.beginRegister();

    launcher.manager.registerPass("fixed");

    launcher.manager.registerComponent!CLocation;
    launcher.manager.registerComponent!CILocation;
    launcher.manager.registerComponent!CSnake;
    launcher.manager.registerComponent!CApple;
    launcher.manager.registerComponent!CParticle;
    launcher.manager.registerComponent!CParticleVector;
    launcher.manager.registerComponent!CMovement;
    launcher.manager.registerComponent!CInput;
    launcher.manager.registerComponent!CAnimation;

    launcher.manager.registerSystem!MoveSystem(0,"fixed");
    launcher.manager.registerSystem!InputSystem(-100);
    launcher.manager.registerSystem!FixSnakeDirectionSystem(-1,"fixed");
    launcher.manager.registerSystem!AppleSystem(-1,"fixed");
    launcher.manager.registerSystem!AnimationRenderSystem(100);
    launcher.manager.registerSystem!AnimationSystem(-1);
    launcher.manager.registerSystem!ParticleSystem(-1);
    launcher.manager.registerSystem!ParticleMovementSystem(-1);
    launcher.manager.registerSystem!DrawAppleSystem(99);
    launcher.manager.registerSystem!DrawSnakeSystem(101);

    launcher.manager.endRegister();

    launcher.gui_manager.addSystem(MoveSystem.system_id,"Move System");
    launcher.gui_manager.addSystem(InputSystem.system_id,"Input System");
    launcher.gui_manager.addSystem(FixSnakeDirectionSystem.system_id,"Fix Direction System");
    launcher.gui_manager.addSystem(AnimationRenderSystem.system_id,"Animation Render System");
    launcher.gui_manager.addSystem(AnimationSystem.system_id,"Animation System");
    launcher.gui_manager.addSystem(ParticleSystem.system_id,"Particle Life System");
    launcher.gui_manager.addSystem(ParticleMovementSystem.system_id,"Particle Movement System");

    snake.snake_destroy_particle_frames = Mallocator.makeArray([vec4(64,144,16,16)*px,vec4(80,144,16,16)*px,vec4(96,144,16,16)*px,vec4(112,144,16,16)*px].staticArray);

    {
        ushort[4] components = [CILocation.component_id, CSnake.component_id, CMovement.component_id, CInput.component_id];
        snake.snake_tmpl = launcher.manager.allocateTemplate(components);
        CILocation* loc_comp = snake.snake_tmpl.getComponent!CILocation;
        *loc_comp = ivec2(2,2);
        launcher.manager.addEntity(snake.snake_tmpl);
    }

    {
        snake.snake_destroy_particle = launcher.manager.allocateTemplate([CLocation.component_id, CParticle.component_id, CParticleVector.component_id, CAnimation.component_id].staticArray);
        CAnimation* canim = snake.snake_destroy_particle.getComponent!CAnimation;
        canim.frames = snake.snake_destroy_particle_frames;
        CParticle* particle = snake.snake_destroy_particle.getComponent!CParticle;
        particle.life = 400;
    }

    {
        ushort[2] components = [CILocation.component_id, CApple.component_id];
        snake.apple_tmpl = launcher.manager.allocateTemplate(components);
        snake.addApple();
    }
    
    launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(snake.snake_tmpl), "Snake");
    launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(snake.apple_tmpl), "Apple");
    launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(snake.snake_destroy_particle), "Particle");

    MoveSystem* move_system = launcher.manager.getSystem!MoveSystem();
    move_system.setTemplates();

    /*foreach(i; 0..10)
        foreach(j; 0..10)
        {
            loc_compation = vec2(i*32+64,j*32+64);
            launcher.manager.addEntity(simple.tmpl);
        }*/
}

void snakeEnd()
{
    //launcher.manager.freeTemplate(simple.tmpl);
    Mallocator.dispose(snake);
}

void snakeTool(vec2 position, Tool tool, int size)
{
    switch(tool)
    {
        case Tool.entity_spawner:
            {
                EntityTemplate* tmpl = launcher.gui_manager.getSelectedTemplate();
                CLocation* location = tmpl.getComponent!CLocation;
                if(location)
                {
                    position.x += (randomf - 0.5) * size;
                    position.y += (randomf - 0.5) * size;
                    *location = position;
                }
                CILocation* ilocation = tmpl.getComponent!CILocation;
                if(ilocation)
                {
                    position.x += (randomf - 0.5) * size;
                    position.y += (randomf - 0.5) * size;
                    ivec2 ipos;
                    ipos.x = cast(int)(position.x / 16);
                    ipos.y = cast(int)(position.y / 16);
                    *ilocation = ipos;
                    if(snake.element(ipos).type != MapElement.Type.empty)return;
                }
                launcher.manager.addEntity(tmpl);
            }
            break;
        default:
            break;
    }
}

void snakeEvent(SDL_Event* event)
{

}

bool snakeLoop()
{
    launcher.render_position = (vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling - vec2(288,288)) * 0.5;

    /*if(launcher.show_demo_wnd)
    {
        igSetNextWindowPos(ImVec2(800 - 260, 30), ImGuiCond_Once, ImVec2(0,0));
        igSetNextWindowSize(ImVec2(250, 0), ImGuiCond_Once);
        if(igBegin("Snake",&launcher.show_demo_wnd,0))
        {
            
        }
        igEnd();
    }*/

    launcher.manager.begin();

    float delta_time = launcher.delta_time;
    if(delta_time > 2000)delta_time = 2000;
    __gshared float time = 0;

    if(launcher.getKeyState(SDL_SCANCODE_SPACE))time += delta_time * 3;
    else time += delta_time;
    
    while(time > 100)
    {
        time -= 100;
        
        launcher.manager.update("fixed");
    }

    launcher.manager.update();

    launcher.manager.end();

    //snake.drawMap();

    return true;
}