Web assembly #6

Merged
Mergul merged 38 commits from WebAssembly into master 2020-04-14 17:44:27 +02:00
9 changed files with 436 additions and 60 deletions
Showing only changes of commit 1f78f2506c - Show all commits

View file

@ -33,6 +33,12 @@ enum Tool
__gshared const (char)*[3] tool_strings = ["Entity spawner", "Component manipulator", "Selector"]; __gshared const (char)*[3] tool_strings = ["Entity spawner", "Component manipulator", "Selector"];
struct Mouse
{
vec2 position;
bool left, right, middle;
}
struct Launcher struct Launcher
{ {
ECSJobUpdater* job_updater; ECSJobUpdater* job_updater;
@ -44,6 +50,7 @@ struct Launcher
bool function() loop; bool function() loop;
void function() end; void function() end;
void function(SDL_Event*) event; void function(SDL_Event*) event;
void function(vec2, Tool, int) tool;
ivec2 window_size = ivec2(1024,768); ivec2 window_size = ivec2(1024,768);
Renderer renderer; Renderer renderer;
ubyte[] keys; ubyte[] keys;
@ -55,6 +62,9 @@ struct Launcher
uint fps; uint fps;
Tool used_tool; Tool used_tool;
int tool_size = 0;
float tool_repeat = 0;
float repeat_time = 0;
bool swap_interval = true; bool swap_interval = true;
@ -71,6 +81,8 @@ struct Launcher
int plot_index; int plot_index;
PlotStruct[] plot_values; PlotStruct[] plot_values;
Mouse mouse;
struct PlotStruct struct PlotStruct
{ {
float delta_time = 0; float delta_time = 0;
@ -78,7 +90,7 @@ struct Launcher
float draw_time = 0; float draw_time = 0;
} }
void switchDemo(void function() start, bool function() loop, void function() end, void function(SDL_Event*) event, const (char)* tips) void switchDemo(void function() start, bool function() loop, void function() end, void function(SDL_Event*) event, void function(vec2, Tool, int) tool, const (char)* tips)
{ {
gui_manager.clear(); gui_manager.clear();
@ -93,6 +105,7 @@ struct Launcher
this.end = end; this.end = end;
this.event = event; this.event = event;
this.tips = tips; this.tips = tips;
this.tool = tool;
} }
bool getKeyState(SDL_Scancode key) bool getKeyState(SDL_Scancode key)
@ -204,6 +217,46 @@ void mainLoop(void* arg)
default:break; default:break;
} }
} }
else if(event.type == SDL_MOUSEBUTTONDOWN)
{
switch(event.button.button)
{
case SDL_BUTTON_LEFT:launcher.mouse.left = true;break;
case SDL_BUTTON_RIGHT:launcher.mouse.right = true;break;
case SDL_BUTTON_MIDDLE:launcher.mouse.middle = true;break;
default:break;
}
if(launcher.tool && event.button.button == SDL_BUTTON_LEFT && launcher.tool_repeat == 0 && !igIsWindowHovered(ImGuiHoveredFlags_AnyWindow))
{
launcher.tool(vec2(event.button.x, launcher.window_size.y - event.button.y), launcher.used_tool, launcher.tool_size);
}
}
else if(event.type == SDL_MOUSEBUTTONUP)
{
switch(event.button.button)
{
case SDL_BUTTON_LEFT:launcher.mouse.left = false;break;
case SDL_BUTTON_RIGHT:launcher.mouse.right = false;break;
case SDL_BUTTON_MIDDLE:launcher.mouse.middle = false;break;
default:break;
}
}
else if(event.type == SDL_MOUSEMOTION)
{
launcher.mouse.position = vec2(event.motion.x, launcher.window_size.y - event.motion.y);
}
}
if(launcher.tool && launcher.tool_repeat != 0 && launcher.mouse.left && !igIsWindowHovered(ImGuiHoveredFlags_AnyWindow) && !igIsWindowFocused(ImGuiFocusedFlags_AnyWindow))
{
float range = 500.0 / cast(float)launcher.tool_repeat;
launcher.repeat_time += launcher.delta_time;
while(launcher.repeat_time > range)
{
launcher.repeat_time -= range;
launcher.tool(launcher.mouse.position, launcher.used_tool, launcher.tool_size);
}
} }
version(WebAssembly) version(WebAssembly)
@ -236,17 +289,17 @@ void mainLoop(void* arg)
if(igMenuItemBool("Simpe",null,false,true)) if(igMenuItemBool("Simpe",null,false,true))
{ {
import demos.simple; import demos.simple;
launcher.switchDemo(&simpleStart,&simpleLoop,&simpleEnd,&simpleEvent,Simple.tips); launcher.switchDemo(&simpleStart,&simpleLoop,&simpleEnd,&simpleEvent,&simpleTool,Simple.tips);
} }
if(igMenuItemBool("Snake",null,false,true)) if(igMenuItemBool("Snake",null,false,true))
{ {
import demos.snake; import demos.snake;
launcher.switchDemo(&snakeStart,&snakeLoop,&snakeEnd,&snakeEvent,Snake.tips); launcher.switchDemo(&snakeStart,&snakeLoop,&snakeEnd,&snakeEvent,&snakeTool,Snake.tips);
} }
if(igMenuItemBool("Space invaders",null,false,true)) if(igMenuItemBool("Space invaders",null,false,true))
{ {
import demos.space_invaders; import demos.space_invaders;
launcher.switchDemo(&spaceInvadersStart,&spaceInvadersLoop,&spaceInvadersEnd,&spaceInvadersEvent,SpaceInvaders.tips); launcher.switchDemo(&spaceInvadersStart,&spaceInvadersLoop,&spaceInvadersEnd,&spaceInvadersEvent,&spaceInvadersTool,SpaceInvaders.tips);
} }
igEndMenu(); igEndMenu();
} }
@ -386,7 +439,17 @@ void mainLoop(void* arg)
igSetNextWindowSize(ImVec2(250, 250), ImGuiCond_Once); igSetNextWindowSize(ImVec2(250, 250), ImGuiCond_Once);
if(igBegin("Demo",&launcher.show_demo_wnd,0)) if(igBegin("Demo",&launcher.show_demo_wnd,0))
{ {
ImDrawList* draw_list = igGetWindowDrawList();
//igBeginGroup();
launcher.gui_manager.gui(); launcher.gui_manager.gui();
//igEndGroup();
//ImDrawList_AddRect(draw_list, igGetItemRectMin(), igGetItemRectMax(), igColorConvertFloat4ToU32(ImVec4(0.4,0.4,0.4,0.4)), -1, 0, 1);
//igBeginChildFrame(1,ImVec2(0,-1),ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_ChildWindow);
//igBeginChild("Tool frame",ImVec2(-1,-1),true,0);
if(igCollapsingHeader("Tool##ToolHeader", ImGuiTreeNodeFlags_SpanAvailWidth))
{
igIndent(8);
igBeginGroup();
if(igBeginCombo("Tool",tool_strings[launcher.used_tool],0)) if(igBeginCombo("Tool",tool_strings[launcher.used_tool],0))
{ {
if(igSelectable("Entity spawner",false,0,ImVec2(0,0))) if(igSelectable("Entity spawner",false,0,ImVec2(0,0)))
@ -403,6 +466,17 @@ void mainLoop(void* arg)
} }
igEndCombo(); igEndCombo();
} }
igSliderInt("Tool size", &launcher.tool_size, 0, 256, null);
igSliderFloat("Tool repeat", &launcher.tool_repeat, 0, 1024, null, 4);
launcher.gui_manager.toolGui();
igEndGroup();
ImDrawList_AddRect(draw_list, igGetItemRectMin(), igGetItemRectMax(), igColorConvertFloat4ToU32(ImVec4(0.4,0.4,0.4,0.4)), 4, ImDrawCornerFlags_All, 1);
igUnindent(8);
}
//igEndChild();
//igEndChildFrame();
if(igButton("Clear",ImVec2(-1,0))) if(igButton("Clear",ImVec2(-1,0)))
{ {
launcher.manager.begin(); launcher.manager.begin();
@ -629,7 +703,7 @@ int main(int argc, char** argv)
{ {
import demos.simple; import demos.simple;
launcher.switchDemo(&simpleStart,&simpleLoop,&simpleEnd,&simpleEvent,Simple.tips); launcher.switchDemo(&simpleStart,&simpleLoop,&simpleEnd,&simpleEvent,&simpleTool,Simple.tips);
} }
int key_num; int key_num;

View file

@ -114,6 +114,8 @@ void simpleStart()
tex_comp.tex = simple.texture; tex_comp.tex = simple.texture;
CLocation* loc_comp = simple.tmpl.getComponent!CLocation; CLocation* loc_comp = simple.tmpl.getComponent!CLocation;
launcher.gui_manager.addTemplate(simple.tmpl, "Basic");
foreach(i; 0..10) foreach(i; 0..10)
foreach(j; 0..10) foreach(j; 0..10)
{ {
@ -129,13 +131,39 @@ void simpleEnd()
simple.texture.destroy(); simple.texture.destroy();
launcher.manager.freeTemplate(simple.tmpl); //launcher.manager.freeTemplate(simple.tmpl);
Mallocator.dispose(simple); Mallocator.dispose(simple);
} }
void simpleTool(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;
}
launcher.manager.addEntity(tmpl);
}
break;
default:
break;
}
}
void simpleEvent(SDL_Event* event) void simpleEvent(SDL_Event* event)
{ {
/*if(event.type == event.button)
{
vec2 position = vec2(event.button.x, event.button.y);
}*/
} }
void spawnEntity() void spawnEntity()
@ -149,7 +177,7 @@ bool simpleLoop()
{ {
if(launcher.getKeyState(SDL_SCANCODE_SPACE)) if(launcher.getKeyState(SDL_SCANCODE_SPACE))
{ {
foreach(i;0..10000)spawnEntity(); foreach(i;0..1)spawnEntity();
} }

View file

@ -51,29 +51,36 @@ struct Snake
MapElement element(ivec2 pos) 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) 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() void addApple()
{ {
ivec2 random_pos = ivec2(rand()%map_size,rand()%map_size); ivec2 random_pos = ivec2(rand()%map_size,rand()%map_size);
ivec2 base_pos = random_pos;
while(element(random_pos).type != MapElement.Type.empty) while(element(random_pos).type != MapElement.Type.empty)
{ {
random_pos.x += 1; random_pos.x += 1;
if(random_pos.x > map_size) if(random_pos.x > map_size)
{ {
random_pos.y = 0; random_pos.x = 0;
random_pos.y += 1; random_pos.y += 1;
if(random_pos.y > map_size)random_pos.y = 0; 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); Entity* apple = launcher.manager.addEntity(apple_tmpl);
element(MapElement(MapElement.Type.apple,apple.id),random_pos);
} }
void drawMap() void drawMap()
@ -199,6 +206,27 @@ struct CInput
mixin ECS.Component; 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 struct MoveSystem
{ {
mixin ECS.System!64; mixin ECS.System!64;
@ -270,6 +298,7 @@ struct MoveSystem
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.location[i].location); snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.location[i].location);
break; break;
case MapElement.Type.apple: case MapElement.Type.apple:
launcher.manager.removeEntity(snake.element(data.location[i].location).id);
data.snakes[i].parts.add(new_location); 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),new_location);
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.location[i].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!MoveSystem(0,"fixed");
launcher.manager.registerSystem!InputSystem(-100); launcher.manager.registerSystem!InputSystem(-100);
launcher.manager.registerSystem!FixSnakeDirectionSystem(-1,"fixed"); launcher.manager.registerSystem!FixSnakeDirectionSystem(-1,"fixed");
launcher.manager.registerSystem!AppleSystem(-1,"fixed");
launcher.manager.endRegister(); launcher.manager.endRegister();
@ -460,6 +490,9 @@ void snakeStart()
snake.addApple(); snake.addApple();
} }
launcher.gui_manager.addTemplate(snake.snake_tmpl, "Snake");
launcher.gui_manager.addTemplate(snake.apple_tmpl, "Apple");
/*foreach(i; 0..10) /*foreach(i; 0..10)
foreach(j; 0..10) foreach(j; 0..10)
{ {
@ -478,6 +511,39 @@ void snakeEnd()
Mallocator.dispose(snake); 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) void snakeEvent(SDL_Event* event)
{ {

View file

@ -306,17 +306,19 @@ struct LaserShootingSystem
struct ChangeDirectionSystem struct ChangeDirectionSystem
{ {
mixin ECS.System; mixin ECS.System!32;
Direction[8] groups_directions; Direction[8] groups_directions;
bool has_changes;
struct EntitiesData struct EntitiesData
{ {
uint length; uint length;
const (Entity)[] entities; const (Entity)[] entities;
const (CLocation)[] locations;
CVelocity[] velocity; CVelocity[] velocity;
@optional const(CSideMove)[] side_move; const(CSideMove)[] side_move;
} }
void onCreate() void onCreate()
@ -327,30 +329,63 @@ struct ChangeDirectionSystem
} }
} }
bool onBegin() /*bool onBegin()
{ {
foreach(direction; groups_directions) foreach(direction; groups_directions)
{ {
if(direction != cast(Direction)-1)return true; if(direction != cast(Direction)-1)//return true;
{
has_changes = true;
break;
} }
return false;
} }
return true;
}*/
void onEnd() void onEnd()
{
if(has_changes)
{ {
foreach(ref direction; groups_directions) foreach(ref direction; groups_directions)
{ {
direction = cast(Direction)-1; direction = cast(Direction)-1;
} }
} }
has_changes = false;
foreach(ref direction; groups_directions)
{
if(direction != cast(Direction)-1)
{
has_changes = true;
//direction = cast(Direction)-1;
}
}
/*foreach(ref direction; groups_directions)
{
direction = cast(Direction)-1;
}*/
}
void onUpdate(EntitiesData data) void onUpdate(EntitiesData data)
{ {
if(!data.side_move)return; //if(!data.side_move)return;
if(has_changes)
foreach(i;0..data.length) foreach(i;0..data.length)
{ {
byte group = data.side_move[i].group; byte group = data.side_move[i].group;
if(group == -1)continue; if(group == -1)
{
if(data.locations[i].x < 0)
{
if(data.velocity[i].x < 0)data.velocity[i].x = -data.velocity[i].x;
}
else if(data.locations[i].x > space_invaders.map_size.x)
{
if(data.velocity[i].x > 0)data.velocity[i].x = -data.velocity[i].x;
}
}
else
{
Direction direction = groups_directions[group]; Direction direction = groups_directions[group];
if(direction != cast(Direction)-1) if(direction != cast(Direction)-1)
{ {
@ -373,6 +408,37 @@ struct ChangeDirectionSystem
} }
} }
} }
else
{
foreach(i;0..data.length)
{
if(data.locations[i].x < 0)
{
if(data.side_move[i].group == -1)
{
if(data.velocity[i].x < 0)data.velocity[i].x = -data.velocity[i].x;
}
else
{
groups_directions[data.side_move[i].group] = Direction.right;
}
}
else if(data.locations[i].x > space_invaders.map_size.x)
{
if(data.side_move[i].group == -1)
{
if(data.velocity[i].x > 0)data.velocity[i].x = -data.velocity[i].x;
}
else
{
groups_directions[data.side_move[i].group] = Direction.left;
}
}
//if(data.locations[i].y < 0) data.locations[i].y = 0;
//else if(data.locations[i].y > space_invaders.map_size.y)data.locations[i].y = space_invaders.map_size.y;
}
}
}
void handleEvent(Entity* entity, EChangeDirection event) void handleEvent(Entity* entity, EChangeDirection event)
{ {
@ -405,6 +471,7 @@ struct ChangeDirectionSystem
struct ClampPositionSystem struct ClampPositionSystem
{ {
mixin ECS.System!32; mixin ECS.System!32;
mixin ECS.ExcludedComponents!(CSideMove);
struct EntitiesData struct EntitiesData
{ {
@ -414,9 +481,12 @@ struct ClampPositionSystem
CLocation[] locations; CLocation[] locations;
@optional const (CLaser)[] laser; @optional const (CLaser)[] laser;
@optional const (CSideMove)[] side_move; //@optional CVelocity[] velocity;
//@optional const (CSideMove)[] side_move;
} }
//ChangeDirectionSystem change_direction_system;
void onUpdate(EntitiesData data) void onUpdate(EntitiesData data)
{ {
if(data.laser) if(data.laser)
@ -427,24 +497,40 @@ struct ClampPositionSystem
data.locations[i].y < 0 || data.locations[i].y > space_invaders.map_size.y)launcher.manager.removeEntity(data.entities[i].id); data.locations[i].y < 0 || data.locations[i].y > space_invaders.map_size.y)launcher.manager.removeEntity(data.entities[i].id);
} }
} }
else if(data.side_move) /*else if(data.side_move)
{ {
foreach(i;0..data.length) foreach(i;0..data.length)
{ {
if(data.locations[i].x < 0) if(data.locations[i].x < 0)
{ {
//data.locations[i].x = 0; //data.locations[i].x = 0;
launcher.manager.sendEvent(data.entities[i].id,EChangeDirection(Direction.right)); //launcher.manager.sendEvent(data.entities[i].id,EChangeDirection(Direction.right));
if(data.side_move[i].group == -1)
{
if(data.velocity[i].x < 0)data.velocity[i].x = -data.velocity[i].x;
}
else
{
change_direction_system.groups_directions[data.side_move[i].group] = Direction.left;
}
} }
else if(data.locations[i].x > space_invaders.map_size.x) else if(data.locations[i].x > space_invaders.map_size.x)
{ {
//data.locations[i].x = space_invaders.map_size.x; //data.locations[i].x = space_invaders.map_size.x;
launcher.manager.sendEvent(data.entities[i].id,EChangeDirection(Direction.left)); //launcher.manager.sendEvent(data.entities[i].id,EChangeDirection(Direction.left));
if(data.side_move[i].group == -1)
{
if(data.velocity[i].x > 0)data.velocity[i].x = -data.velocity[i].x;
}
else
{
change_direction_system.groups_directions[data.side_move[i].group] = Direction.right;
}
} }
if(data.locations[i].y < 0) data.locations[i].y = 0; if(data.locations[i].y < 0) data.locations[i].y = 0;
else if(data.locations[i].y > space_invaders.map_size.y)data.locations[i].y = space_invaders.map_size.y; else if(data.locations[i].y > space_invaders.map_size.y)data.locations[i].y = space_invaders.map_size.y;
} }
} }*/
else else
{ {
foreach(i;0..data.length) foreach(i;0..data.length)
@ -628,6 +714,11 @@ void spaceInvadersStart()
vel_comp.value = vec2(0,1); vel_comp.value = vec2(0,1);
} }
EntityTemplate* enemy_tmpl;
EntityTemplate* grouped_tmpl;
EntityID enemy_id;
EntityID grouped_id;
{ {
ushort[8] components = [CVelocity.component_id, CAutoShoot.component_id, CLocation.component_id, CTexture.component_id, CScale.component_id, CLaserWeapon.component_id, CEnemy.component_id, CShootDirection.component_id];//, CVelocity.component_id]; ushort[8] components = [CVelocity.component_id, CAutoShoot.component_id, CLocation.component_id, CTexture.component_id, CScale.component_id, CLaserWeapon.component_id, CEnemy.component_id, CShootDirection.component_id];//, CVelocity.component_id];
space_invaders.enemy_tmpl = launcher.manager.allocateTemplate(components); space_invaders.enemy_tmpl = launcher.manager.allocateTemplate(components);
@ -650,13 +741,28 @@ void spaceInvadersStart()
current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl); current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl);
launcher.manager.addComponents(current_entity.id,CSideMove(-1)); launcher.manager.addComponents(current_entity.id,CSideMove(-1));
enemy_id = current_entity.id;
//enemy_tmpl = launcher.manager.allocateTemplate(current_entity.id);
loc_comp.value = vec2(256,space_invaders.map_size.y - 64); loc_comp.value = vec2(256,space_invaders.map_size.y - 64);
launcher.manager.addEntity(space_invaders.enemy_tmpl); launcher.manager.addEntity(space_invaders.enemy_tmpl);
loc_comp.value = vec2(0,space_invaders.map_size.y - 64); loc_comp.value = vec2(0,space_invaders.map_size.y - 64);
current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl); current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl);
launcher.manager.addComponents(current_entity.id,CSideMove(0)); launcher.manager.addComponents(current_entity.id,CSideMove(0));
grouped_id = current_entity.id;
//grouped_tmpl = launcher.manager.allocateTemplate(current_entity.id);
} }
launcher.manager.commit();
enemy_tmpl = launcher.manager.allocateTemplate(enemy_id);
grouped_tmpl = launcher.manager.allocateTemplate(grouped_id);
launcher.gui_manager.addTemplate(space_invaders.ship_tmpl,"Ship");
launcher.gui_manager.addTemplate(enemy_tmpl,"Enemy");
launcher.gui_manager.addTemplate(grouped_tmpl,"Grouped enemy");
launcher.gui_manager.addTemplate(space_invaders.laser_tmpl,"Laser");
} }
@ -671,10 +777,32 @@ void spaceInvadersEnd()
space_invaders.ship_tex.destroy(); space_invaders.ship_tex.destroy();
launcher.manager.freeTemplate(space_invaders.ship_tmpl); launcher.manager.freeTemplate(space_invaders.enemy_tmpl);
Mallocator.dispose(space_invaders); Mallocator.dispose(space_invaders);
} }
void spaceInvadersTool(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;
}
launcher.manager.addEntity(tmpl);
}
break;
default:
break;
}
}
void spaceInvadersEvent(SDL_Event* event) void spaceInvadersEvent(SDL_Event* event)
{ {

View file

@ -7,18 +7,35 @@ import cimgui.cimgui;
import ecs.std; import ecs.std;
import ecs.system; import ecs.system;
import ecs.vector; import ecs.vector;
import ecs.entity;
import gui.system; import gui.system;
import gui.template_;
extern(C): extern(C):
struct GUIManager struct GUIManager
{ {
Vector!SystemGUI systems; Vector!SystemGUI systems;
Vector!TemplateGUI templates;
uint selected_tempalte = 0;
void clear() void clear()
{ {
foreach(tmpl; templates)
{
launcher.manager.freeTemplate(tmpl.tmpl);
}
systems.clear(); systems.clear();
templates.clear();
}
EntityTemplate* getSelectedTemplate()
{
if(templates.length > selected_tempalte)return templates[selected_tempalte].tmpl;
else return null;
} }
void addSystem(ushort id, const (char)* name, bool enabled = true) void addSystem(ushort id, const (char)* name, bool enabled = true)
@ -28,9 +45,19 @@ struct GUIManager
systems.add(SystemGUI(name,system,enabled)); systems.add(SystemGUI(name,system,enabled));
} }
void addTemplate(ushort[] components, const (char)* name)
{
templates.add(TemplateGUI(name, launcher.manager.allocateTemplate(components)));
}
void addTemplate(EntityTemplate* tmpl, const (char)* name)
{
templates.add(TemplateGUI(name, tmpl));
}
void gui() void gui()
{ {
if(igCollapsingHeader("Systems", ImGuiTreeNodeFlags_Framed)) if(igCollapsingHeader("Systems", ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_FramePadding))
{ {
bool true_ = true; bool true_ = true;
igIndent(8); igIndent(8);
@ -44,6 +71,27 @@ struct GUIManager
} }
igUnindent(8); igUnindent(8);
} }
}
void toolGui()
{
if(templates.length)
{
if(igBeginCombo("Template",templates[selected_tempalte].name,0))
{
foreach(i, tmpl; templates)
{
if(igSelectable(tmpl.name,false,0,ImVec2(0,0)))
{
selected_tempalte = cast(uint)i;
}
}
igEndCombo();
}
}
else
{
if(igBeginCombo("Template",null,0))igEndCombo();
}
} }
} }

View file

@ -0,0 +1,9 @@
module gui.template_;
import ecs.entity;
struct TemplateGUI
{
const (char)* name;
EntityTemplate* tmpl;
}

View file

@ -21,4 +21,9 @@ static struct ECS
__gshared ushort event_id; __gshared ushort event_id;
EntityID entity_id; EntityID entity_id;
} }
mixin template ExcludedComponents(T...)
{
alias ExcludedComponents = T;
}
} }

View file

@ -211,7 +211,7 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
return cast(float) forElementsNum / (elements.length); return cast(float) forElementsNum / (elements.length);
} }
export void rehash() { export void rehash()() {
mixin(doNotInline); mixin(doNotInline);
// Get all elements // Get all elements
Vector!KeyVal allElements; Vector!KeyVal allElements;

View file

@ -223,6 +223,24 @@ export struct EntityManager
} }
} }
extern (C) static int comapreEventCaller(const void* a, const void* b) nothrow @nogc
{
EventCaller _a = *cast(EventCaller*) a;
EventCaller _b = *cast(EventCaller*) b;
if (_a.system.priority < _b.system.priority)
return -1;
else if (_a.system.priority == _b.system.priority)
return 0;
else
return 1;
}
foreach(ref event; events)
{
qsort(event.callers.ptr, event.callers.length, EventCaller.sizeof, &comapreEventCaller);
}
//qsort(event_callers.ptr, event_callers.length, EventInfo.sizeof, &compareUShorts);
foreach(EntityInfo* info;&entities_infos.byValue) foreach(EntityInfo* info;&entities_infos.byValue)
{ {
generateListeners(info); generateListeners(info);
@ -569,11 +587,11 @@ export struct EntityManager
//components_info.excluded ~= CompInfo(str,str); //components_info.excluded ~= CompInfo(str,str);
} }
} }
else static if (checkExcludedComponentsSomething!Sys) else //static if (checkExcludedComponentsSomething!Sys)
{ {
foreach (str; Sys.ExcludedComponents) foreach (str; Sys.ExcludedComponents)
{ {
components_info.addExcluded(CompInfo(str,str)); components_info.addExcluded(CompInfo(str.stringof,str.stringof));
//components_info.excluded ~= CompInfo(str,str); //components_info.excluded ~= CompInfo(str,str);
} }