-Events system update order is now choosen according to system priority

-added mixin for adding exluded components using it's type
-demos:
 *added GUI for selecting templates and choosing tools
 *change SpaceInvades SideMove system to not using events for better performance and multithreading
 *added Entites spawning support
 *fixed some Snake demo bugs
 *GUI work's better now
This commit is contained in:
Mergul 2020-02-09 15:24:26 +01:00
parent 19fc440ed6
commit 1f78f2506c
9 changed files with 436 additions and 60 deletions

View file

@ -51,29 +51,36 @@ struct Snake
MapElement element(ivec2 pos)
{
return map[pos.x + pos.y * map_size];
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)
{
map[pos.x + pos.y * map_size] = el;
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.y = 0;
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);
element(MapElement(MapElement.Type.apple,apple.id),random_pos);
}
void drawMap()
@ -199,6 +206,27 @@ 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 MoveSystem
{
mixin ECS.System!64;
@ -270,6 +298,7 @@ struct MoveSystem
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.location[i].location);
break;
case MapElement.Type.apple:
launcher.manager.removeEntity(snake.element(data.location[i].location).id);
data.snakes[i].parts.add(new_location);
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);
@ -439,6 +468,7 @@ void snakeStart()
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.endRegister();
@ -460,6 +490,9 @@ void snakeStart()
snake.addApple();
}
launcher.gui_manager.addTemplate(snake.snake_tmpl, "Snake");
launcher.gui_manager.addTemplate(snake.apple_tmpl, "Apple");
/*foreach(i; 0..10)
foreach(j; 0..10)
{
@ -478,6 +511,39 @@ void snakeEnd()
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 / 32);
ipos.y = cast(int)(position.y / 32);
*ilocation = ipos;
if(snake.element(ipos).type != MapElement.Type.empty)return;
}
launcher.manager.addEntity(tmpl);
}
break;
default:
break;
}
}
void snakeEvent(SDL_Event* event)
{