-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:
parent
19fc440ed6
commit
1f78f2506c
9 changed files with 436 additions and 60 deletions
|
|
@ -33,6 +33,12 @@ enum Tool
|
|||
|
||||
__gshared const (char)*[3] tool_strings = ["Entity spawner", "Component manipulator", "Selector"];
|
||||
|
||||
struct Mouse
|
||||
{
|
||||
vec2 position;
|
||||
bool left, right, middle;
|
||||
}
|
||||
|
||||
struct Launcher
|
||||
{
|
||||
ECSJobUpdater* job_updater;
|
||||
|
|
@ -44,6 +50,7 @@ struct Launcher
|
|||
bool function() loop;
|
||||
void function() end;
|
||||
void function(SDL_Event*) event;
|
||||
void function(vec2, Tool, int) tool;
|
||||
ivec2 window_size = ivec2(1024,768);
|
||||
Renderer renderer;
|
||||
ubyte[] keys;
|
||||
|
|
@ -55,6 +62,9 @@ struct Launcher
|
|||
uint fps;
|
||||
|
||||
Tool used_tool;
|
||||
int tool_size = 0;
|
||||
float tool_repeat = 0;
|
||||
float repeat_time = 0;
|
||||
|
||||
bool swap_interval = true;
|
||||
|
||||
|
|
@ -71,6 +81,8 @@ struct Launcher
|
|||
int plot_index;
|
||||
PlotStruct[] plot_values;
|
||||
|
||||
Mouse mouse;
|
||||
|
||||
struct PlotStruct
|
||||
{
|
||||
float delta_time = 0;
|
||||
|
|
@ -78,7 +90,7 @@ struct Launcher
|
|||
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();
|
||||
|
||||
|
|
@ -93,6 +105,7 @@ struct Launcher
|
|||
this.end = end;
|
||||
this.event = event;
|
||||
this.tips = tips;
|
||||
this.tool = tool;
|
||||
}
|
||||
|
||||
bool getKeyState(SDL_Scancode key)
|
||||
|
|
@ -204,6 +217,46 @@ void mainLoop(void* arg)
|
|||
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)
|
||||
|
|
@ -236,17 +289,17 @@ void mainLoop(void* arg)
|
|||
if(igMenuItemBool("Simpe",null,false,true))
|
||||
{
|
||||
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))
|
||||
{
|
||||
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))
|
||||
{
|
||||
import demos.space_invaders;
|
||||
launcher.switchDemo(&spaceInvadersStart,&spaceInvadersLoop,&spaceInvadersEnd,&spaceInvadersEvent,SpaceInvaders.tips);
|
||||
launcher.switchDemo(&spaceInvadersStart,&spaceInvadersLoop,&spaceInvadersEnd,&spaceInvadersEvent,&spaceInvadersTool,SpaceInvaders.tips);
|
||||
}
|
||||
igEndMenu();
|
||||
}
|
||||
|
|
@ -386,7 +439,17 @@ void mainLoop(void* arg)
|
|||
igSetNextWindowSize(ImVec2(250, 250), ImGuiCond_Once);
|
||||
if(igBegin("Demo",&launcher.show_demo_wnd,0))
|
||||
{
|
||||
ImDrawList* draw_list = igGetWindowDrawList();
|
||||
//igBeginGroup();
|
||||
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(igSelectable("Entity spawner",false,0,ImVec2(0,0)))
|
||||
|
|
@ -403,6 +466,17 @@ void mainLoop(void* arg)
|
|||
}
|
||||
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)))
|
||||
{
|
||||
launcher.manager.begin();
|
||||
|
|
@ -629,7 +703,7 @@ int main(int argc, char** argv)
|
|||
|
||||
{
|
||||
import demos.simple;
|
||||
launcher.switchDemo(&simpleStart,&simpleLoop,&simpleEnd,&simpleEvent,Simple.tips);
|
||||
launcher.switchDemo(&simpleStart,&simpleLoop,&simpleEnd,&simpleEvent,&simpleTool,Simple.tips);
|
||||
}
|
||||
|
||||
int key_num;
|
||||
|
|
|
|||
|
|
@ -114,6 +114,8 @@ void simpleStart()
|
|||
tex_comp.tex = simple.texture;
|
||||
CLocation* loc_comp = simple.tmpl.getComponent!CLocation;
|
||||
|
||||
launcher.gui_manager.addTemplate(simple.tmpl, "Basic");
|
||||
|
||||
foreach(i; 0..10)
|
||||
foreach(j; 0..10)
|
||||
{
|
||||
|
|
@ -129,13 +131,39 @@ void simpleEnd()
|
|||
|
||||
simple.texture.destroy();
|
||||
|
||||
launcher.manager.freeTemplate(simple.tmpl);
|
||||
//launcher.manager.freeTemplate(simple.tmpl);
|
||||
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)
|
||||
{
|
||||
/*if(event.type == event.button)
|
||||
{
|
||||
vec2 position = vec2(event.button.x, event.button.y);
|
||||
|
||||
}*/
|
||||
}
|
||||
|
||||
void spawnEntity()
|
||||
|
|
@ -149,7 +177,7 @@ bool simpleLoop()
|
|||
{
|
||||
if(launcher.getKeyState(SDL_SCANCODE_SPACE))
|
||||
{
|
||||
foreach(i;0..10000)spawnEntity();
|
||||
foreach(i;0..1)spawnEntity();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
||||
|
|
|
|||
|
|
@ -306,17 +306,19 @@ struct LaserShootingSystem
|
|||
|
||||
struct ChangeDirectionSystem
|
||||
{
|
||||
mixin ECS.System;
|
||||
mixin ECS.System!32;
|
||||
|
||||
Direction[8] groups_directions;
|
||||
bool has_changes;
|
||||
|
||||
struct EntitiesData
|
||||
{
|
||||
uint length;
|
||||
const (Entity)[] entities;
|
||||
const (CLocation)[] locations;
|
||||
CVelocity[] velocity;
|
||||
|
||||
@optional const(CSideMove)[] side_move;
|
||||
const(CSideMove)[] side_move;
|
||||
}
|
||||
|
||||
void onCreate()
|
||||
|
|
@ -327,30 +329,63 @@ struct ChangeDirectionSystem
|
|||
}
|
||||
}
|
||||
|
||||
bool onBegin()
|
||||
/*bool onBegin()
|
||||
{
|
||||
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()
|
||||
{
|
||||
if(has_changes)
|
||||
{
|
||||
foreach(ref direction; groups_directions)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if(!data.side_move)return;
|
||||
//if(!data.side_move)return;
|
||||
if(has_changes)
|
||||
foreach(i;0..data.length)
|
||||
{
|
||||
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];
|
||||
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)
|
||||
{
|
||||
|
|
@ -405,6 +471,7 @@ struct ChangeDirectionSystem
|
|||
struct ClampPositionSystem
|
||||
{
|
||||
mixin ECS.System!32;
|
||||
mixin ECS.ExcludedComponents!(CSideMove);
|
||||
|
||||
struct EntitiesData
|
||||
{
|
||||
|
|
@ -414,9 +481,12 @@ struct ClampPositionSystem
|
|||
CLocation[] locations;
|
||||
|
||||
@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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
else if(data.side_move)
|
||||
/*else if(data.side_move)
|
||||
{
|
||||
foreach(i;0..data.length)
|
||||
{
|
||||
if(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)
|
||||
{
|
||||
//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;
|
||||
else if(data.locations[i].y > space_invaders.map_size.y)data.locations[i].y = space_invaders.map_size.y;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
else
|
||||
{
|
||||
foreach(i;0..data.length)
|
||||
|
|
@ -628,6 +714,11 @@ void spaceInvadersStart()
|
|||
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];
|
||||
space_invaders.enemy_tmpl = launcher.manager.allocateTemplate(components);
|
||||
|
|
@ -650,13 +741,28 @@ void spaceInvadersStart()
|
|||
current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl);
|
||||
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);
|
||||
launcher.manager.addEntity(space_invaders.enemy_tmpl);
|
||||
|
||||
loc_comp.value = vec2(0,space_invaders.map_size.y - 64);
|
||||
current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl);
|
||||
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();
|
||||
|
||||
launcher.manager.freeTemplate(space_invaders.ship_tmpl);
|
||||
launcher.manager.freeTemplate(space_invaders.enemy_tmpl);
|
||||
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)
|
||||
{
|
||||
|
||||
|
|
|
|||
|
|
@ -7,18 +7,35 @@ import cimgui.cimgui;
|
|||
import ecs.std;
|
||||
import ecs.system;
|
||||
import ecs.vector;
|
||||
import ecs.entity;
|
||||
|
||||
import gui.system;
|
||||
import gui.template_;
|
||||
|
||||
extern(C):
|
||||
|
||||
struct GUIManager
|
||||
{
|
||||
Vector!SystemGUI systems;
|
||||
Vector!TemplateGUI templates;
|
||||
|
||||
uint selected_tempalte = 0;
|
||||
|
||||
void clear()
|
||||
{
|
||||
foreach(tmpl; templates)
|
||||
{
|
||||
launcher.manager.freeTemplate(tmpl.tmpl);
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
@ -28,9 +45,19 @@ struct GUIManager
|
|||
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()
|
||||
{
|
||||
if(igCollapsingHeader("Systems", ImGuiTreeNodeFlags_Framed))
|
||||
if(igCollapsingHeader("Systems", ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_FramePadding))
|
||||
{
|
||||
bool true_ = true;
|
||||
igIndent(8);
|
||||
|
|
@ -44,6 +71,27 @@ struct GUIManager
|
|||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
9
demos/source/gui/template_.d
Normal file
9
demos/source/gui/template_.d
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
module gui.template_;
|
||||
|
||||
import ecs.entity;
|
||||
|
||||
struct TemplateGUI
|
||||
{
|
||||
const (char)* name;
|
||||
EntityTemplate* tmpl;
|
||||
}
|
||||
|
|
@ -21,4 +21,9 @@ static struct ECS
|
|||
__gshared ushort event_id;
|
||||
EntityID entity_id;
|
||||
}
|
||||
|
||||
mixin template ExcludedComponents(T...)
|
||||
{
|
||||
alias ExcludedComponents = T;
|
||||
}
|
||||
}
|
||||
|
|
@ -211,7 +211,7 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
return cast(float) forElementsNum / (elements.length);
|
||||
}
|
||||
|
||||
export void rehash() {
|
||||
export void rehash()() {
|
||||
mixin(doNotInline);
|
||||
// Get all elements
|
||||
Vector!KeyVal allElements;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
generateListeners(info);
|
||||
|
|
@ -569,11 +587,11 @@ export struct EntityManager
|
|||
//components_info.excluded ~= CompInfo(str,str);
|
||||
}
|
||||
}
|
||||
else static if (checkExcludedComponentsSomething!Sys)
|
||||
else //static if (checkExcludedComponentsSomething!Sys)
|
||||
{
|
||||
foreach (str; Sys.ExcludedComponents)
|
||||
{
|
||||
components_info.addExcluded(CompInfo(str,str));
|
||||
components_info.addExcluded(CompInfo(str.stringof,str.stringof));
|
||||
//components_info.excluded ~= CompInfo(str,str);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue