CI and common update:

-added webpage deploymnet stage
-added separate build stage which build all binaries and generate documentation
-added Emscripten build stage for merge to master only
-added VBO batch rendering (current default, no render mode switch yet)
-fixed camera positioning calculation
-fixed buffer issue with WebGL
-added viewport scalling (at least 300 pixels height). Pixels are scalled if screen is bigger.
-center demos gameplay area
-added fullpage html template for Emscripten build
This commit is contained in:
Dawid Masiukiewicz 2020-05-01 19:26:21 +00:00
parent f67eb452cc
commit 54a6d5dec2
29 changed files with 1167 additions and 322 deletions

View file

@ -51,6 +51,7 @@ struct Launcher
void function() end;
void function(SDL_Event*) event;
void function(vec2, Tool, int) tool;
float scalling;
ivec2 window_size = ivec2(1024,768);
Renderer renderer;
ubyte[] keys;
@ -60,6 +61,7 @@ struct Launcher
ulong timer_freq;
double delta_time;
uint fps;
vec2 render_position;
Tool used_tool;
int tool_size = 0;
@ -229,7 +231,7 @@ void mainLoop(void* arg)
}
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);
launcher.tool(vec2(event.button.x, launcher.window_size.y - event.button.y) * launcher.scalling - launcher.render_position, launcher.used_tool, launcher.tool_size);
}
}
else if(event.type == SDL_MOUSEBUTTONUP)
@ -255,7 +257,7 @@ void mainLoop(void* arg)
while(launcher.repeat_time > range)
{
launcher.repeat_time -= range;
launcher.tool(launcher.mouse.position, launcher.used_tool, launcher.tool_size);
launcher.tool((launcher.mouse.position*launcher.scalling)-launcher.render_position, launcher.used_tool, launcher.tool_size);
}
}
@ -525,7 +527,14 @@ void mainLoop(void* arg)
}
launcher.renderer.resize(launcher.window_size);
launcher.renderer.view(vec2(0,0),vec2(launcher.window_size.x,launcher.window_size.y));
//launcher.renderer.view(vec2(0,0),vec2(launcher.window_size.x,launcher.window_size.y));
//if(384, 768, 1152, 1536)
//576 960 1344 1728
//float scalling;
if(launcher.window_size.y < 360)launcher.scalling = 1;
else launcher.scalling = 1.0 / ((launcher.window_size.y+120)/360);
launcher.renderer.view(launcher.render_position,vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling);
//launcher.renderer.view(vec2(0,0),vec2(1024*launcher.window_size.x/launcher.window_size.y,768));
//glClear(GL_COLOR_BUFFER_BIT);
launcher.renderer.clear();

View file

@ -60,7 +60,7 @@ struct DrawSystem
{
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);
launcher.renderer.draw(data.textures[i].tex, data.locations[i].location, vec2(16,16), 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));
}
}
@ -81,7 +81,7 @@ struct MoveSystem
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;
if(data.locations[i].location.y > 300)data.locations[i].location.y = 0;
}
}
}
@ -119,7 +119,7 @@ void simpleStart()
foreach(i; 0..10)
foreach(j; 0..10)
{
loc_comp.location = vec2(i*32+64,j*32+64);
loc_comp.location = vec2(i*16+64,j*16+64);
launcher.manager.addEntity(simple.tmpl);
}
}
@ -147,6 +147,10 @@ void simpleTool(vec2 position, Tool tool, int size)
{
position.x += (randomf - 0.5) * size;
position.y += (randomf - 0.5) * size;
if(position.x > 400)position.x -= 400;
else if(position.x < 0)position.x += 400;
if(position.y > 300)position.y -= 300;
else if(position.y < 0)position.y += 300;
*location = position;
}
launcher.manager.addEntity(tmpl);
@ -169,17 +173,18 @@ void simpleEvent(SDL_Event* event)
void spawnEntity()
{
CLocation* loc_comp = simple.tmpl.getComponent!CLocation;
loc_comp.location = vec2(randomf() * 600,0);
loc_comp.location = vec2(randomf() * 400,0);
launcher.manager.addEntity(simple.tmpl);
}
bool simpleLoop()
{
launcher.render_position = (vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling - vec2(400,300)) * 0.5;
if(launcher.getKeyState(SDL_SCANCODE_SPACE))
{
foreach(i;0..1)spawnEntity();
}
launcher.manager.begin();
if(launcher.multithreading)

View file

@ -17,6 +17,10 @@ import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
import std.array : staticArray;
enum float px = 1.0/512.0;
extern(C):
struct MapElement
@ -24,9 +28,24 @@ struct MapElement
enum Type
{
empty = 0,
snake = 1,
apple = 2,
wall = 3
apple = 1,
wall = 2,
snake_head_up = 5,
snake_head_down = 6,
snake_head_left = 7,
snake_head_right = 8,
snake_tail_up = 9,
snake_tail_down = 10,
snake_tail_left = 11,
snake_tail_right = 12,
snake_turn_ld = 13,
snake_turn_lu = 14,
snake_turn_rd = 15,
snake_turn_ru = 16,
snake_vertical = 17,
snake_horizontal = 18
}
Type type;
EntityID id;
@ -38,8 +57,13 @@ struct Snake
EntityTemplate* apple_tmpl;
EntityTemplate* snake_tmpl;
EntityTemplate* snake_destroy_particle;
Texture texture;
vec4[] snake_destroy_particle_frames;
vec4[] smoke_frames;
bool move_system = true;
bool draw_system = true;
@ -83,16 +107,30 @@ struct Snake
void drawMap()
{
const float px = 1.0/512.0;
foreach(x; 0 .. map_size)
{
foreach(y; 0 .. map_size)
{
switch(element(ivec2(x,y)).type)
{
case MapElement.Type.apple:launcher.renderer.draw(texture, vec2(x*32,y*32), vec2(32,32), vec4(0,32*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake:launcher.renderer.draw(texture, vec2(x*32,y*32), vec2(32,32), vec4(0,48*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.wall:launcher.renderer.draw(texture, vec2(x*32,y*32), vec2(32,32), vec4(0,0,1,1), 0, 0 , 0);break;
case MapElement.Type.apple:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,32*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_head_up:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(48*px,112*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_head_down:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(48*px,144*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_head_left:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,128*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_head_right:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(32*px,128*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_tail_up:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(16*px,112*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_tail_down:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,112*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_tail_left:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(32*px,112*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_tail_right:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,144*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_turn_ld:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(64*px,128*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_turn_lu:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(32*px,144*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_turn_rd:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(16*px,144*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_turn_ru:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(64*px,112*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_vertical:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(16*px,128*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_horizontal:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(48*px,128*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.wall:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,0,1,1), 0, 0 , 0);break;
default:break;
}
}
@ -101,6 +139,19 @@ struct Snake
}
struct Animation
{
}
struct CAnimation
{
mixin ECS.Component;
vec4[] frames;
float time = 0;
}
struct CILocation
{
mixin ECS.Component;
@ -116,7 +167,7 @@ struct CLocation
alias location this;
vec2 location;
vec2 location = vec2(0,0);
}
struct CSnake
@ -176,13 +227,15 @@ struct CApple
struct CParticle
{
mixin ECS.Component;
float life = 0;
}
struct CParticleVector
{
mixin ECS.Component;
vec2 velocity;
vec2 velocity = vec2(0,0);
}
struct CMovement
@ -226,10 +279,98 @@ struct AppleSystem
}
}
struct ParticleSystem
{
mixin ECS.System!1;
struct EntitiesData
{
uint length;
@readonly Entity[] entities;
@readonly CParticle[] particle;
}
void onUpdate(EntitiesData data)
{
foreach(i;0..data.length)
{
data.particle[i].life -= launcher.delta_time;
if(data.particle[i].life < 0)launcher.manager.removeEntity(data.entities[i].id);
}
}
}
struct ParticleMovementSystem
{
mixin ECS.System!1;
struct EntitiesData
{
uint length;
@readonly Entity[] entities;
@readonly CParticleVector[] movement;
CLocation[] location;
}
void onUpdate(EntitiesData data)
{
foreach(i;0..data.length)
{
data.location[i].location -= data.movement[i].velocity;
}
}
}
struct AnimationSystem
{
mixin ECS.System!1;
struct EntitiesData
{
uint length;
CAnimation[] animation;
}
void onUpdate(EntitiesData data)
{
foreach(i;0..data.length)
{
data.animation[i].time += launcher.delta_time * 0.01;
while(data.animation[i].time >= data.animation[i].frames.length)data.animation[i].time -= cast(float)data.animation[i].frames.length;
}
}
}
struct AnimationRenderSystem
{
mixin ECS.System!1;
struct EntitiesData
{
uint length;
@readonly CAnimation[] animation;
@readonly CLocation[] location;
}
void onUpdate(EntitiesData data)
{
foreach(i;0..data.length)
{
launcher.renderer.draw(snake.texture, cast(vec2)cast(ivec2)data.location[i].location, vec2(16,16), data.animation[i].frames[cast(int)(data.animation[i].time)], 0, 0 , 0);
}
}
}
struct MoveSystem
{
mixin ECS.System!64;
EntityTemplate* destroy_template;
CLocation* destroy_location;
CParticleVector* destroy_vector;
struct EntitiesData
{
uint length;
@ -239,6 +380,13 @@ struct MoveSystem
CILocation[] location;
}
void setTemplates()
{
destroy_template = snake.snake_destroy_particle;
destroy_location = destroy_template.getComponent!CLocation;
destroy_vector = destroy_template.getComponent!CParticleVector;
}
void moveLocation(ref CILocation location, CMovement.Direction direction)
{
final switch(direction)
@ -276,6 +424,56 @@ struct MoveSystem
else .snake.element(MapElement(),location);
}
static CMovement.Direction getDirection(ivec2 p1, ivec2 p2)
{
if(p1.x - p2.x == -1)return CMovement.direction.right;
else if(p1.x - p2.x == 1)return CMovement.direction.left;
else if(p1.y - p2.y == -1)return CMovement.direction.up;
else if(p1.y - p2.y == 1)return CMovement.direction.down;
else if(p1.x - p2.x > 1)return CMovement.direction.right;
else if(p1.x - p2.x < -1)return CMovement.direction.left;
else if(p1.y - p2.y > 1)return CMovement.direction.up;
else return CMovement.direction.down;
}
static MapElement.Type snakePart(ivec2 p1, ivec2 p2, ivec2 p3)
{
CMovement.Direction direction = getDirection(p1, p2);
CMovement.Direction direction2 = getDirection(p1, p3);
uint case_ = direction*4 + direction2;
final switch(case_)
{
case 0:return MapElement.Type.snake_horizontal;
case 1:return MapElement.Type.snake_horizontal;
case 2:return MapElement.Type.snake_turn_lu;
case 3:return MapElement.Type.snake_turn_ru;
case 4:return MapElement.Type.snake_horizontal;
case 5:return MapElement.Type.snake_horizontal;
case 6:return MapElement.Type.snake_turn_ld;
case 7:return MapElement.Type.snake_turn_rd;
case 8:return MapElement.Type.snake_turn_lu;
case 9:return MapElement.Type.snake_turn_ld;
case 10:return MapElement.Type.snake_vertical;
case 11:return MapElement.Type.snake_vertical;
case 12:return MapElement.Type.snake_turn_ru;
case 13:return MapElement.Type.snake_turn_rd;
case 14:return MapElement.Type.snake_vertical;
case 15:return MapElement.Type.snake_vertical;
}
}
static MapElement.Type snakeTail(ivec2 p1, ivec2 p2)
{
CMovement.Direction direction = getDirection(p1, p2);
final switch(direction)
{
case CMovement.Direction.up:return MapElement.Type.snake_tail_up;
case CMovement.Direction.down:return MapElement.Type.snake_tail_down;
case CMovement.Direction.left:return MapElement.Type.snake_tail_left;
case CMovement.Direction.right:return MapElement.Type.snake_tail_right;
}
}
void onUpdate(EntitiesData data)
{
if(data.snakes)
@ -286,21 +484,104 @@ struct MoveSystem
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:
case MapElement.Type.snake_head_up:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_head_down:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_head_left:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_head_right:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_tail_up:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_tail_down:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_tail_left:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_tail_right:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_turn_ld:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_turn_lu:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_turn_rd:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_turn_ru:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_vertical:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_horizontal:
foreach(ivec2 loc; data.snakes[i].parts)
{
destroy_location.x = loc.x * 16;
destroy_location.y = loc.y * 16;
snake.element(MapElement(MapElement.Type.empty, EntityID()),loc);
launcher.manager.addEntity(snake.snake_destroy_particle);
foreach(j;0..10)
{
destroy_location.x = loc.x * 16 + randomf() * 8 - 4;
destroy_location.y = loc.y * 16 + randomf() * 8 - 4;
destroy_vector.velocity = vec2(randomf(),randomf())*0.4-0.2;
snake.element(MapElement(MapElement.Type.empty, EntityID()),loc);
launcher.manager.addEntity(snake.snake_destroy_particle);
}
}
destroy_location.x = new_location.x * 16;
destroy_location.y = new_location.y * 16;
snake.element(MapElement(MapElement.Type.empty, EntityID()),new_location);
launcher.manager.addEntity(snake.snake_destroy_particle);
launcher.manager.removeEntity(data.entities[i].id);
break;
case MapElement.Type.wall:break;
//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);
final switch(data.movement[i].direction)
{
case CMovement.Direction.up:
snake.element(MapElement(MapElement.Type.snake_head_up, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.right:
snake.element(MapElement(MapElement.Type.snake_head_right, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.down:
snake.element(MapElement(MapElement.Type.snake_head_down, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.left:
snake.element(MapElement(MapElement.Type.snake_head_left, data.entities[i].id),data.location[i].location);
break;
}
if(data.snakes[i].parts.length > 1)
{
MapElement.Type elem_type = snakePart(data.snakes[i].parts[$-1],data.location[i],data.snakes[i].parts[$-2]);
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[$-1]);
elem_type = snakeTail(data.snakes[i].parts[1], data.snakes[i].parts[0]);
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[0]);
}
else if(data.snakes[i].parts.length == 1)
{
MapElement.Type elem_type = snakeTail(data.location[i], data.snakes[i].parts[0]);
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[0]);
}
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);
if(data.snakes[i].parts.length < 100)data.snakes[i].parts.add(new_location);
if(data.snakes[i].parts.length > 1)
{
MapElement.Type elem_type = snakePart(data.snakes[i].parts[$-1],data.location[i],data.snakes[i].parts[$-2]);
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[$-1]);
}
else if(data.snakes[i].parts.length == 1)
{
MapElement.Type elem_type = snakeTail(data.location[i], new_location);
snake.element(MapElement(elem_type, data.entities[i].id),new_location);
}
final switch(data.movement[i].direction)
{
case CMovement.Direction.up:
snake.element(MapElement(MapElement.Type.snake_head_up, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.right:
snake.element(MapElement(MapElement.Type.snake_head_right, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.down:
snake.element(MapElement(MapElement.Type.snake_head_down, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.left:
snake.element(MapElement(MapElement.Type.snake_head_left, data.entities[i].id),data.location[i].location);
break;
}
snake.addApple();
break;
}
@ -455,19 +736,31 @@ void snakeStart()
launcher.manager.registerComponent!CSnake;
launcher.manager.registerComponent!CApple;
launcher.manager.registerComponent!CParticle;
launcher.manager.registerComponent!CParticleVector;
launcher.manager.registerComponent!CMovement;
launcher.manager.registerComponent!CInput;
launcher.manager.registerComponent!CAnimation;
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.registerSystem!AnimationRenderSystem(100);
launcher.manager.registerSystem!AnimationSystem(-1);
launcher.manager.registerSystem!ParticleSystem(-1);
launcher.manager.registerSystem!ParticleMovementSystem(-1);
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");
launcher.gui_manager.addSystem(AnimationRenderSystem.system_id,"Animation Render System");
launcher.gui_manager.addSystem(AnimationSystem.system_id,"Animation System");
launcher.gui_manager.addSystem(ParticleSystem.system_id,"Particle Life System");
launcher.gui_manager.addSystem(ParticleMovementSystem.system_id,"Particle Movement System");
snake.snake_destroy_particle_frames = Mallocator.makeArray([vec4(64,144,16,16)*px,vec4(80,144,16,16)*px,vec4(96,144,16,16)*px,vec4(112,144,16,16)*px].staticArray);
{
ushort[4] components = [CILocation.component_id, CSnake.component_id, CMovement.component_id, CInput.component_id];
@ -477,14 +770,26 @@ void snakeStart()
launcher.manager.addEntity(snake.snake_tmpl);
}
{
snake.snake_destroy_particle = launcher.manager.allocateTemplate([CLocation.component_id, CParticle.component_id, CParticleVector.component_id, CAnimation.component_id].staticArray);
CAnimation* canim = snake.snake_destroy_particle.getComponent!CAnimation;
canim.frames = snake.snake_destroy_particle_frames;
CParticle* particle = snake.snake_destroy_particle.getComponent!CParticle;
particle.life = 400;
}
{
ushort[2] components = [CILocation.component_id, CApple.component_id];
snake.apple_tmpl = launcher.manager.allocateTemplate(components);
snake.addApple();
}
launcher.gui_manager.addTemplate(snake.snake_tmpl, "Snake");
launcher.gui_manager.addTemplate(snake.apple_tmpl, "Apple");
launcher.gui_manager.addTemplate(snake.snake_destroy_particle, "Particle");
MoveSystem* move_system = launcher.manager.getSystem!MoveSystem();
move_system.setTemplates();
/*foreach(i; 0..10)
foreach(j; 0..10)
@ -520,8 +825,8 @@ void snakeTool(vec2 position, Tool tool, int size)
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);
ipos.x = cast(int)(position.x / 16);
ipos.y = cast(int)(position.y / 16);
*ilocation = ipos;
if(snake.element(ipos).type != MapElement.Type.empty)return;
}
@ -540,6 +845,8 @@ void snakeEvent(SDL_Event* event)
bool snakeLoop()
{
launcher.render_position = (vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling - vec2(288,288)) * 0.5;
/*if(launcher.show_demo_wnd)
{
igSetNextWindowPos(ImVec2(800 - 260, 30), ImGuiCond_Once, ImVec2(0,0));

View file

@ -16,6 +16,8 @@ import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
enum float px = 1.0/512.0;
extern(C):
/*#######################################################################################################################
@ -34,7 +36,7 @@ struct SpaceInvaders
bool move_system = true;
bool draw_system = true;
const vec2 map_size = vec2(600,600);
const vec2 map_size = vec2(400,300);
const float cell_size = 60;
}
@ -101,7 +103,7 @@ struct CScale
///use component as it value
alias value this;
vec2 value = vec2(32,32);
vec2 value = vec2(16,16);
}
struct CTexture
@ -564,8 +566,8 @@ struct MovementSystem
{
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;
data.locations[i].x += data.velocity[i].x * launcher.delta_time * 0.5;
data.locations[i].y += data.velocity[i].y * launcher.delta_time * 0.5;
}
}
}
@ -587,6 +589,7 @@ struct InputMovementSystem
const (CInput)[] input;
//components are treated as required by default
CLocation[] locations;
CTexture[] textures;
}
/**
@ -595,6 +598,7 @@ struct InputMovementSystem
*/
bool onBegin()
{
move_vector = vec2(0,0);
if(launcher.getKeyState(SDL_SCANCODE_W))
{
move_vector = vec2(0,1);
@ -616,7 +620,7 @@ struct InputMovementSystem
return true;
}
//don't call system update because no key pressed
return false;
return true;
}
/**
@ -627,11 +631,21 @@ struct InputMovementSystem
*/
void onUpdate(EntitiesData data)
{
if(move_vector.x == 0)
{
foreach(i; 0..data.length)
{
data.textures[i].coords = vec4(0*px,80*px,48*px,32*px);
}
return;
}
//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;
data.locations[i].x += move_vector.x * launcher.delta_time * 0.25;
data.locations[i].y += move_vector.y * launcher.delta_time * 0.25;
if(move_vector.x > 0)data.textures[i].coords = vec4(48*px,80*px,48*px,32*px);
else data.textures[i].coords = vec4(0*px,80*px,48*px,32*px);
}
}
}
@ -644,8 +658,6 @@ __gshared SpaceInvaders* space_invaders;
void spaceInvadersStart()
{
const float px = 1.0/512.0;
space_invaders = Mallocator.make!SpaceInvaders;
space_invaders.texture.create();
@ -690,9 +702,11 @@ void spaceInvadersStart()
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);
CScale* scale_comp = space_invaders.ship_tmpl.getComponent!CScale;
scale_comp.value = vec2(48,32);
CTexture* tex_comp = space_invaders.ship_tmpl.getComponent!CTexture;
tex_comp.tex = space_invaders.texture;//ship_tex;
tex_comp.coords = vec4(0*px,48*px,16*px,16*px);
tex_comp.coords = vec4(0*px,80*px,48*px,32*px);
CLocation* loc_comp = space_invaders.ship_tmpl.getComponent!CLocation;
loc_comp.value = vec2(64,64);
CLaserWeapon* weapon = space_invaders.ship_tmpl.getComponent!CLaserWeapon;
@ -707,9 +721,9 @@ void spaceInvadersStart()
CTexture* tex_comp = space_invaders.laser_tmpl.getComponent!CTexture;
tex_comp.tex = space_invaders.texture;//laser_tex;
tex_comp.coords = vec4(0*px,48*px,16*px,16*px);
tex_comp.coords = vec4(0*px,24*px,2*px,8*px);
CScale* scale_comp = space_invaders.laser_tmpl.getComponent!CScale;
scale_comp.value = vec2(4,16);
scale_comp.value = vec2(2,8);
CVelocity* vel_comp = space_invaders.laser_tmpl.getComponent!CVelocity;
vel_comp.value = vec2(0,1);
}
@ -727,7 +741,7 @@ void spaceInvadersStart()
tex_comp.tex = space_invaders.texture;//ship_tex;
tex_comp.coords = vec4(32*px,32*px,16*px,16*px);
CLocation* loc_comp = space_invaders.enemy_tmpl.getComponent!CLocation;
loc_comp.value = vec2(64,space_invaders.map_size.y - 64);
loc_comp.value = vec2(64,space_invaders.map_size.y - 16);
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;
@ -738,17 +752,17 @@ void spaceInvadersStart()
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);
loc_comp.value = vec2(128,space_invaders.map_size.y - 16);
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);
loc_comp.value = vec2(256,space_invaders.map_size.y - 16);
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 - 16);
current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl);
launcher.manager.addComponents(current_entity.id,CSideMove(0));
@ -809,6 +823,7 @@ void spaceInvadersEvent(SDL_Event* event)
bool spaceInvadersLoop()
{
launcher.render_position = (vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling - vec2(400,300)) * 0.5;
/*if(launcher.show_demo_wnd)
{