-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

@ -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();
}

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)
{

View file

@ -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,49 +329,113 @@ 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)
{
direction = cast(Direction)-1;
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;
Direction direction = groups_directions[group];
if(direction != cast(Direction)-1)
if(group == -1)
{
CVelocity* velocity = &data.velocity[i];
final switch(direction)
if(data.locations[i].x < 0)
{
case Direction.up:
if(velocity.value.y > 0)velocity.value.y = -velocity.value.y;
break;
case Direction.down:
if(velocity.value.y < 0)velocity.value.y = -velocity.value.y;
break;
case Direction.left:
if(velocity.value.x > 0)velocity.value.x = -velocity.value.x;
break;
case Direction.right:
if(velocity.value.x < 0)velocity.value.x = -velocity.value.x;
break;
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)
{
CVelocity* velocity = &data.velocity[i];
final switch(direction)
{
case Direction.up:
if(velocity.value.y > 0)velocity.value.y = -velocity.value.y;
break;
case Direction.down:
if(velocity.value.y < 0)velocity.value.y = -velocity.value.y;
break;
case Direction.left:
if(velocity.value.x > 0)velocity.value.x = -velocity.value.x;
break;
case Direction.right:
if(velocity.value.x < 0)velocity.value.x = -velocity.value.x;
break;
}
}
}
}
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;
}
}
}
@ -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);
@ -649,6 +740,9 @@ void spaceInvadersStart()
loc_comp.value = vec2(128,space_invaders.map_size.y - 64);
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);
@ -656,7 +750,19 @@ void spaceInvadersStart()
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)
{