-updated wasm build scripts

-multitheaded wasm tests
-updated dub.json
-fixed thread pool assert issue
-added windows libraries
-added demos:
 *launcher
 *simple
 *snake
 *space invaders (WIP)
 *statistics window
 *changable gui styles
 *tips window
 *profile window
 *demo window with automatic generation
 *multithreaded job updater
This commit is contained in:
Mergul 2019-11-19 10:37:34 +01:00
parent cb7609dcaa
commit 5894e76540
26 changed files with 2562 additions and 20 deletions

View file

@ -0,0 +1,19 @@
module demos.bullet_madnes;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):

View file

@ -0,0 +1,19 @@
module demos.chipmunk2d;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):

View file

@ -0,0 +1,19 @@
module demos.events;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):

View file

@ -0,0 +1,19 @@
module demos.flag_component;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):

View file

@ -0,0 +1,19 @@
module demos.physics;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):

171
demos/source/demos/simple.d Normal file
View file

@ -0,0 +1,171 @@
module demos.simple;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):
struct Simple
{
__gshared const (char)* tips = "Use \"space\" to spwan entities.\n\nSystems can be enabled/disabled from \"Simple\" window.";
EntityTemplate* tmpl;
Texture texture;
bool move_system = true;
bool draw_system = true;
}
struct CLocation
{
mixin ECS.Component;
alias location this;
vec2 location;
}
struct CTexture
{
mixin ECS.Component;
Texture tex;
}
struct DrawSystem
{
mixin ECS.System!1;
struct EntitiesData
{
uint length;
@readonly CTexture[] textures;
@readonly CLocation[] locations;
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.length)
{
launcher.renderer.draw(data.textures[i].tex, data.locations[i].location, vec2(32,32), vec4(0,0,1,1), 0, 0 , 0);
//draw(renderer, data.textures[i].tex, data.locations[i], vec2(32,32), vec4(0,0,1,1));
}
}
}
struct MoveSystem
{
mixin ECS.System!64;
struct EntitiesData
{
uint length;
CLocation[] locations;
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.length)
{
data.locations[i].location.y = data.locations[i].location.y + 1;
if(data.locations[i].location.y > 400)data.locations[i].location.y = 0;
}
}
}
Simple* simple;
void simpleStart()
{
simple = Mallocator.make!Simple;
simple.texture.create();
simple.texture.load("assets/textures/buckler.png");
launcher.manager.beginRegister();
launcher.manager.registerComponent!CLocation;
launcher.manager.registerComponent!CTexture;
launcher.manager.registerSystem!MoveSystem(0);
launcher.manager.registerSystem!DrawSystem(1);
launcher.manager.endRegister();
launcher.gui_manager.addSystem(MoveSystem.system_id,"Move System");
launcher.gui_manager.addSystem(DrawSystem.system_id,"Draw System");
ushort[2] components = [CLocation.component_id, CTexture.component_id];
simple.tmpl = launcher.manager.allocateTemplate(components);
CTexture* tex_comp = simple.tmpl.getComponent!CTexture;
tex_comp.tex = simple.texture;
CLocation* loc_comp = simple.tmpl.getComponent!CLocation;
foreach(i; 0..10)
foreach(j; 0..10)
{
loc_comp.location = vec2(i*32+64,j*32+64);
launcher.manager.addEntity(simple.tmpl);
}
}
void simpleEnd()
{
launcher.manager.getSystem(MoveSystem.system_id).disable();
launcher.manager.getSystem(DrawSystem.system_id).disable();
simple.texture.destroy();
launcher.manager.freeTemplate(simple.tmpl);
Mallocator.dispose(simple);
}
void simpleEvent(SDL_Event* event)
{
}
void spawnEntity()
{
CLocation* loc_comp = simple.tmpl.getComponent!CLocation;
loc_comp.location = vec2(randomf() * 600,0);
launcher.manager.addEntity(simple.tmpl);
}
bool simpleLoop()
{
if(launcher.getKeyState(SDL_SCANCODE_SPACE))
{
spawnEntity();
}
launcher.manager.begin();
if(launcher.multithreading)
{
launcher.job_updater.begin();
launcher.manager.updateMT();
launcher.job_updater.call();
launcher.multithreading = false;
}
else
{
launcher.manager.update();
}
launcher.manager.end();
return true;
}

522
demos/source/demos/snake.d Normal file
View file

@ -0,0 +1,522 @@
module demos.snake;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs.vector;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):
struct MapElement
{
enum Type
{
empty = 0,
snake = 1,
apple = 2,
wall = 3
}
Type type;
EntityID id;
}
struct Snake
{
__gshared const (char)* tips = "Use \"WASD\" keys to move.";
EntityTemplate* apple_tmpl;
EntityTemplate* snake_tmpl;
Texture snake_texture;
Texture wall_texture;
Texture apple_texture;
bool move_system = true;
bool draw_system = true;
const int map_size = 18;
MapElement[map_size * map_size] map;
MapElement element(ivec2 pos)
{
return map[pos.x + pos.y * map_size];
}
void element(MapElement el, ivec2 pos)
{
map[pos.x + pos.y * map_size] = el;
}
void addApple()
{
ivec2 random_pos = ivec2(rand()%map_size,rand()%map_size);
while(element(random_pos).type != MapElement.Type.empty)
{
random_pos.x += 1;
if(random_pos.x > map_size)
{
random_pos.y = 0;
random_pos.y += 1;
if(random_pos.y > map_size)random_pos.y = 0;
}
}
Entity* apple = launcher.manager.addEntity(apple_tmpl);
element(MapElement(MapElement.Type.apple,apple.id),random_pos);
}
void drawMap()
{
foreach(x; 0 .. map_size)
{
foreach(y; 0 .. map_size)
{
switch(element(ivec2(x,y)).type)
{
case MapElement.Type.apple:launcher.renderer.draw(apple_texture, vec2(x*32,y*32), vec2(32,32), vec4(0,0,1,1), 0, 0 , 0);break;
case MapElement.Type.snake:launcher.renderer.draw(snake_texture, vec2(x*32,y*32), vec2(32,32), vec4(0,0,1,1), 0, 0 , 0);break;
case MapElement.Type.wall:launcher.renderer.draw(wall_texture, vec2(x*32,y*32), vec2(32,32), vec4(0,0,1,1), 0, 0 , 0);break;
default:break;
}
}
}
}
}
struct CILocation
{
mixin ECS.Component;
alias location this;
ivec2 location;
}
struct CLocation
{
mixin ECS.Component;
alias location this;
vec2 location;
}
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;
}
struct CApple
{
mixin ECS.Component;
}
struct CParticle
{
mixin ECS.Component;
}
struct CParticleVector
{
mixin ECS.Component;
vec2 velocity;
}
struct CMovement
{
mixin ECS.Component;
enum Direction : byte
{
up,
down,
left,
right
}
Direction direction;
}
struct CInput
{
mixin ECS.Component;
}
struct MoveSystem
{
mixin ECS.System!64;
struct EntitiesData
{
uint length;
@readonly Entity[] entities;
@readonly CMovement[] movement;
@optional CSnake[] snakes;
CILocation[] location;
}
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)
{
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:
launcher.manager.removeEntity(data.entities[i].id);
break;
case MapElement.Type.wall:
launcher.manager.removeEntity(data.entities[i].id);
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);
break;
case MapElement.Type.apple:
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);
snake.addApple();
break;
}
}
}
else
{
foreach(i; 0..data.length)
{
final switch(data.movement[i].direction)
{
case CMovement.Direction.down:data.location[i].location.y -= 1;break;
case CMovement.Direction.up:data.location[i].location.y += 1;break;
case CMovement.Direction.left:data.location[i].location.x -= 1;break;
case CMovement.Direction.right:data.location[i].location.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 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);
}
}
}
Snake* snake;
void snakeStart()
{
snake = Mallocator.make!Snake;
snake.snake_texture.create();
snake.snake_texture.load("assets/textures/buckler.png");
snake.apple_texture.create();
snake.apple_texture.load("assets/textures/buckler.png");
snake.wall_texture.create();
snake.wall_texture.load("assets/textures/buckler.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!CMovement;
launcher.manager.registerComponent!CInput;
launcher.manager.registerSystem!MoveSystem(0,"fixed");
launcher.manager.registerSystem!InputSystem(-100);
launcher.manager.registerSystem!FixSnakeDirectionSystem(-1,"fixed");
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");
{
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.location = ivec2(2,2);
launcher.manager.addEntity(snake.snake_tmpl);
}
{
ushort[2] components = [CILocation.component_id, CApple.component_id];
snake.apple_tmpl = launcher.manager.allocateTemplate(components);
snake.addApple();
}
/*foreach(i; 0..10)
foreach(j; 0..10)
{
loc_comp.location = vec2(i*32+64,j*32+64);
launcher.manager.addEntity(simple.tmpl);
}*/
}
void snakeEnd()
{
snake.wall_texture.destroy();
snake.apple_texture.destroy();
snake.snake_texture.destroy();
//launcher.manager.freeTemplate(simple.tmpl);
Mallocator.dispose(snake);
}
void snakeEvent(SDL_Event* event)
{
}
bool snakeLoop()
{
/*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;
static 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;
}

View file

@ -0,0 +1,764 @@
module demos.space_invaders;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):
/*#######################################################################################################################
------------------------------------------------ Types ------------------------------------------------------------------
#######################################################################################################################*/
struct SpaceInvaders
{
__gshared const (char)* tips = "Use \"WASD\" keys to move and \"Space\" for shooting.";
EntityTemplate* enemy_tmpl;
EntityTemplate* ship_tmpl;
EntityTemplate* laser_tmpl;
Texture enemy_tex;
Texture ship_tex;
Texture laser_tex;
bool move_system = true;
bool draw_system = true;
const vec2 map_size = vec2(600,600);
const float cell_size = 60;
}
struct SceneGrid
{
struct Element
{
EntityID entity;
int guild;
vec2 min;
vec2 max;
}
struct Cell
{
Element[20] elements;
}
void create()
{
cells_count.x = cast(int)((space_invaders.map_size.x - 0.01f) / space_invaders.cell_size) + 1;
cells_count.y = cast(int)((space_invaders.map_size.y - 0.01f) / space_invaders.cell_size) + 1;
cells = Mallocator.makeArray!Cell(cells_count.x * cells_count.y);
}
void destory()
{
if(cells)
{
Mallocator.dispose(cells);
cells = null;
}
}
ivec2 cells_count;
Cell[] cells;
}
enum Direction : byte
{
up,
down,
left,
right
}
/*#######################################################################################################################
------------------------------------------------ Components ------------------------------------------------------------------
#######################################################################################################################*/
struct CLocation
{
mixin ECS.Component;
alias value this;
vec2 value;
}
struct CScale
{
mixin ECS.Component;
///use component as it value
alias value this;
vec2 value = vec2(32,32);
}
struct CTexture
{
mixin ECS.Component;
Texture tex;
}
struct CVelocity
{
mixin ECS.Component;
alias value this;
vec2 value = vec2(0,0);
}
struct CInput
{
mixin ECS.Component;
}
struct CEnemy
{
mixin ECS.Component;
}
struct CShip
{
mixin ECS.Component;
}
struct CAutoShoot
{
mixin ECS.Component;
}
struct CGuild
{
mixin ECS.Component;
int guild;
}
struct CLaser
{
mixin ECS.Component;
float damage = 1.0f;
}
struct CLaserWeapon
{
mixin ECS.Component;
ubyte level = 1;
float shoot_time = 0;
}
struct CShootDirection
{
mixin ECS.Component;
Direction direction;
}
struct CSideMove
{
mixin ECS.Component;
byte group = -1;
}
/*#######################################################################################################################
------------------------------------------------ Events ------------------------------------------------------------------
#######################################################################################################################*/
struct EChangeDirection
{
mixin ECS.Event;
this(Direction direction)
{
this.direction = direction;
}
Direction direction;
}
/*#######################################################################################################################
------------------------------------------------ Systems ------------------------------------------------------------------
#######################################################################################################################*/
struct DrawSystem
{
mixin ECS.System!1;
struct EntitiesData
{
uint length;
@readonly CTexture[] textures;
@readonly CLocation[] locations;
@readonly CScale[] scale;
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.length)
{
launcher.renderer.draw(data.textures[i].tex, data.locations[i].value, data.scale[i], vec4(0,0,1,1), 0, 0 , 0);
//draw(renderer, data.textures[i].tex, data.locations[i], vec2(32,32), vec4(0,0,1,1));
}
}
}
struct CollisionSystem
{
mixin ECS.System;
struct EntitiesData
{
}
void onUpdate(EntitiesData data)
{
}
}
struct LaserShootingSystem
{
mixin ECS.System!32;
bool shoot = false;
float[10] laser_shoot_times = [2000,1500,1000,700,500,300,100,50,10,1];
CLocation* laser_location;
CVelocity* laser_velocity;
struct EntitiesData
{
///variable named "length" contain entites count
uint length;
@readonly CShootDirection[] shoot_direction;
@readonly @optional CAutoShoot[] auto_shoot;
@readonly CLocation[] location;
CLaserWeapon[] laser;
}
///Called inside "registerSystem" function
/*void onCreate()
{
laser_location = space_invaders.laser_tmpl.getComponent!CLocation;
}*/
bool onBegin()
{
laser_location = space_invaders.laser_tmpl.getComponent!CLocation;
laser_velocity = space_invaders.laser_tmpl.getComponent!CVelocity;
if(launcher.getKeyState(SDL_SCANCODE_SPACE))
{
shoot = true;
}
else shoot = false;
return true;
}
void onUpdate(EntitiesData data)
{
//conditional branch for whole entities block
if(shoot || data.auto_shoot)
{
foreach(i;0..data.length)
{
CLaserWeapon* laser = &data.laser[i];
laser.shoot_time += launcher.delta_time;
while(laser.shoot_time > laser_shoot_times[laser.level - 1])
{
laser.shoot_time -= laser_shoot_times[laser.level - 1];
laser_location.value = data.location[i];
laser_velocity.value = vec2(randomf()*0.5-0.25,data.shoot_direction[i].direction == Direction.up ? 1.0 : -1.0);
launcher.manager.addEntity(space_invaders.laser_tmpl);
}
}
}
else
{
foreach(i;0..data.length)
{
CLaserWeapon* laser = &data.laser[i];
laser.shoot_time += launcher.delta_time;
if(laser.shoot_time > laser_shoot_times[laser.level - 1])laser.shoot_time = laser_shoot_times[laser.level - 1];
}
}
}
}
struct ChangeDirectionSystem
{
mixin ECS.System;
Direction[8] groups_directions;
struct EntitiesData
{
uint length;
const (Entity)[] entities;
CVelocity[] velocity;
@optional const(CSideMove)[] side_move;
}
void onCreate()
{
foreach(ref direction; groups_directions)
{
direction = cast(Direction)-1;
}
}
bool onBegin()
{
foreach(direction; groups_directions)
{
if(direction != cast(Direction)-1)return true;
}
return false;
}
void onEnd()
{
foreach(ref direction; groups_directions)
{
direction = cast(Direction)-1;
}
}
void onUpdate(EntitiesData data)
{
if(!data.side_move)return;
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)
{
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;
}
}
}
}
void handleEvent(Entity* entity, EChangeDirection event)
{
CSideMove* side_move = entity.getComponent!CSideMove;
if(side_move && side_move.group != -1)
{
groups_directions[side_move.group] = event.direction;
return;
}
//Entity* entity = launcher.manager.getEntity(event.entity_id);
CVelocity* velocity = entity.getComponent!CVelocity;
final switch(event.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;
}
}
}
struct ClampPositionSystem
{
mixin ECS.System!32;
struct EntitiesData
{
uint length;
const (Entity)[] entities;
//components are treated as required by default
CLocation[] locations;
@optional const (CLaser)[] laser;
@optional const (CSideMove)[] side_move;
}
void onUpdate(EntitiesData data)
{
if(data.laser)
{
foreach(i;0..data.length)
{
if(data.locations[i].x < 0 || data.locations[i].x > space_invaders.map_size.x ||
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)
{
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));
}
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));
}
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)
{
if(data.locations[i].x < 0)data.locations[i].x = 0;
else if(data.locations[i].x > space_invaders.map_size.x)data.locations[i].x = space_invaders.map_size.x;
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;
}
}
}
}
struct MovementSystem
{
mixin ECS.System!32;
struct EntitiesData
{
uint length;
//read only components can be marked with @readonly attribute or with const expression instead
const (CVelocity)[] velocity;
//components are treated as required by default
CLocation[] locations;
//@optional const (CLaser)[] laser;
const (Entity)[] entities;
@optional CSideMove[] side_move;
}
void onUpdate(EntitiesData data)
{
foreach(i;0..data.length)
{
data.locations[i].x += data.velocity[i].x * launcher.delta_time;
data.locations[i].y += data.velocity[i].y * launcher.delta_time;
}
}
}
/**
*System is responsible for movement of objects with CInput component.
*In this example every entity has same speed when using movement system.
*/
struct InputMovementSystem
{
mixin ECS.System!32;
vec2 move_vector;
struct EntitiesData
{
uint length;
//read only components can be marked with @readonly attribute or with const expression instead
const (CInput)[] input;
//components are treated as required by default
CLocation[] locations;
}
/**
*onBegin gives opportunity to check keys once and call update on entities only when
*one key is pressed.
*/
bool onBegin()
{
if(launcher.getKeyState(SDL_SCANCODE_W))
{
move_vector = vec2(0,1);
return true;
}
else if(launcher.getKeyState(SDL_SCANCODE_S))
{
move_vector = vec2(0,-1);
return true;
}
else if(launcher.getKeyState(SDL_SCANCODE_A))
{
move_vector = vec2(-1,0);
return true;
}
else if(launcher.getKeyState(SDL_SCANCODE_D))
{
move_vector = vec2(1,0);
return true;
}
//don't call system update because no key pressed
return false;
}
/**
*Update is called multiple times in one "manager.update()" call.
*Number of "onUpdate" calls is count of buffers which must be updated during pass.
*When multithreading is used, number of "onUpdate" calls can be greater due to fact that
*JobSystem can split buffers for better data packing.
*/
void onUpdate(EntitiesData data)
{
//move every entity using movement vector
foreach(i; 0..data.length)
{
data.locations[i].x += move_vector.x * launcher.delta_time * 0.5;
data.locations[i].y += move_vector.y * launcher.delta_time * 0.5;
}
}
}
/*#######################################################################################################################
------------------------------------------------ Functions ------------------------------------------------------------------
#######################################################################################################################*/
__gshared SpaceInvaders* space_invaders;
void spaceInvadersStart()
{
space_invaders = Mallocator.make!SpaceInvaders;
space_invaders.ship_tex.create();
space_invaders.ship_tex.load("assets/textures/buckler.png");
space_invaders.laser_tex.create();
space_invaders.laser_tex.load("assets/textures/buckler.png");
launcher.manager.beginRegister();
launcher.manager.registerComponent!CLocation;
launcher.manager.registerComponent!CTexture;
launcher.manager.registerComponent!CInput;
launcher.manager.registerComponent!CShip;
launcher.manager.registerComponent!CEnemy;
launcher.manager.registerComponent!CScale;
launcher.manager.registerComponent!CShootDirection;
launcher.manager.registerComponent!CAutoShoot;
launcher.manager.registerComponent!CLaserWeapon;
launcher.manager.registerComponent!CVelocity;
launcher.manager.registerComponent!CLaser;
launcher.manager.registerComponent!CSideMove;
launcher.manager.registerEvent!EChangeDirection;
//launcher.manager.registerSystem!MoveSystem(0);
launcher.manager.registerSystem!DrawSystem(100);
launcher.manager.registerSystem!InputMovementSystem(-100);
launcher.manager.registerSystem!LaserShootingSystem(0);
launcher.manager.registerSystem!MovementSystem(0);
launcher.manager.registerSystem!ClampPositionSystem(1);
launcher.manager.registerSystem!ChangeDirectionSystem(0);
launcher.manager.endRegister();
launcher.gui_manager.addSystem(DrawSystem.system_id,"Draw System");
launcher.gui_manager.addSystem(InputMovementSystem.system_id,"Input Movement");
launcher.gui_manager.addSystem(LaserShootingSystem.system_id,"Laser Shooting");
launcher.gui_manager.addSystem(MovementSystem.system_id,"Movement System");
launcher.gui_manager.addSystem(ClampPositionSystem.system_id,"Clamp Position System");
launcher.gui_manager.addSystem(ChangeDirectionSystem.system_id,"Change Direction System");
//launcher.manager.getSystem(CleanSystem.system_id).disable();
{
ushort[7] components = [CLocation.component_id, CTexture.component_id, CInput.component_id, CShip.component_id, CScale.component_id, CLaserWeapon.component_id, CShootDirection.component_id];
space_invaders.ship_tmpl = launcher.manager.allocateTemplate(components);
CTexture* tex_comp = space_invaders.ship_tmpl.getComponent!CTexture;
tex_comp.tex = space_invaders.ship_tex;
CLocation* loc_comp = space_invaders.ship_tmpl.getComponent!CLocation;
loc_comp.value = vec2(64,64);
CLaserWeapon* weapon = space_invaders.ship_tmpl.getComponent!CLaserWeapon;
weapon.level = 10;
launcher.manager.addEntity(space_invaders.ship_tmpl);
}
{
ushort[5] components = [CLocation.component_id, CTexture.component_id, CVelocity.component_id, CScale.component_id, CLaser.component_id];
space_invaders.laser_tmpl = launcher.manager.allocateTemplate(components);
CTexture* tex_comp = space_invaders.laser_tmpl.getComponent!CTexture;
tex_comp.tex = space_invaders.laser_tex;
CScale* scale_comp = space_invaders.laser_tmpl.getComponent!CScale;
scale_comp.value = vec2(4,16);
CVelocity* vel_comp = space_invaders.laser_tmpl.getComponent!CVelocity;
vel_comp.value = vec2(0,1);
}
{
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);
CTexture* tex_comp = space_invaders.enemy_tmpl.getComponent!CTexture;
tex_comp.tex = space_invaders.ship_tex;
CLocation* loc_comp = space_invaders.enemy_tmpl.getComponent!CLocation;
loc_comp.value = vec2(64,space_invaders.map_size.y - 64);
CShootDirection* shoot_dir_comp = space_invaders.enemy_tmpl.getComponent!CShootDirection;
shoot_dir_comp.direction = Direction.down;
CVelocity* vel_comp = space_invaders.enemy_tmpl.getComponent!CVelocity;
vel_comp.value = vec2(0.1,0);
Entity* current_entity;
current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl);
launcher.manager.addComponents(current_entity.id,CSideMove(0));
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));
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));
}
}
void spaceInvadersEnd()
{
launcher.manager.getSystem(DrawSystem.system_id).disable();
launcher.manager.getSystem(InputMovementSystem.system_id).disable();
launcher.manager.getSystem(LaserShootingSystem.system_id).disable();
launcher.manager.getSystem(MovementSystem.system_id).disable();
launcher.manager.getSystem(ClampPositionSystem.system_id).disable();
space_invaders.ship_tex.destroy();
launcher.manager.freeTemplate(space_invaders.ship_tmpl);
Mallocator.dispose(space_invaders);
}
void spaceInvadersEvent(SDL_Event* event)
{
}
bool spaceInvadersLoop()
{
/*if(launcher.show_demo_wnd)
{
igSetNextWindowPos(ImVec2(800 - 260, 30), ImGuiCond_Once, ImVec2(0,0));
igSetNextWindowSize(ImVec2(250, 0), ImGuiCond_Once);
if(igBegin("Simple",&launcher.show_demo_wnd,0))
{
if(igCheckbox("Move system",&simple.move_system))
{
if(simple.move_system)launcher.manager.getSystem(MoveSystem.system_id).enable();
else launcher.manager.getSystem(MoveSystem.system_id).disable();
}
if(igCheckbox("Draw system",&simple.draw_system))
{
if(simple.draw_system)launcher.manager.getSystem(DrawSystem.system_id).enable();
else launcher.manager.getSystem(DrawSystem.system_id).disable();
}
igPushButtonRepeat(true);
igColumns(3,null,0);
if(igButton("Spawn",ImVec2(-1,0)))
{
spawnEntity();
}
igNextColumn();
if(igButton("+10",ImVec2(-1,0)))
{
foreach(i;0..10)spawnEntity();
}
igNextColumn();
if(igButton("+100",ImVec2(-1,0)))
{
foreach(i;0..100)spawnEntity();
}
igPopButtonRepeat();
igColumns(1,null,0);
if(igButton("Clear",ImVec2(-1,0)))
{
launcher.manager.getSystem(CleanSystem.system_id).enable();
launcher.manager.begin();
launcher.manager.update();
launcher.manager.end();
launcher.manager.getSystem(CleanSystem.system_id).disable();
}
}
igEnd();
}*/
/*if(launcher.show_tips)
{
igSetNextWindowPos(ImVec2(800 - 550, 80), ImGuiCond_Once, ImVec2(0,0));
igSetNextWindowSize(ImVec2(300, 0), ImGuiCond_Once);
if(igBegin("Tips",&launcher.show_tips,ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings))
{
igTextWrapped("Use \"space\" to spwan entities.\n\nSystems can be enabled/disabled from \"Simple\" window.");
}
igEnd();
}*/
launcher.manager.begin();
if(launcher.multithreading)
{
launcher.job_updater.begin();
launcher.manager.updateMT();
launcher.job_updater.call();
launcher.multithreading = false;
printf("Disable mt\n");
}
else
{
launcher.manager.update();
}
launcher.manager.end();
/*foreach(i;0..1000)//13000)
{
launcher.renderer.draw(simple.texture,vec2(i%100*32,i/100*32),vec2(32,32),vec4(0,0,1,1),0.0);
}*/
return true;
}