Added laser collision response to SpaceInvaders

This commit is contained in:
Mergul 2020-05-13 21:27:09 +02:00
parent d257a6c9f8
commit c29ace661b
4 changed files with 131 additions and 29 deletions

View file

@ -99,7 +99,7 @@ void main() {
uv = tex_coords * uv_transform.zw + uv_transform.xy; uv = tex_coords * uv_transform.zw + uv_transform.xy;
#endif #endif
color = vcolor; color = vcolor * 2;
gl_Position = vec4(position.xy,depth,1.0); gl_Position = vec4(position.xy,depth,1.0);

View file

@ -62,7 +62,7 @@ struct DrawSystem
if(launcher.renderer.prepared_items >= launcher.renderer.MaxObjects)return;//simple leave loop if max visible objects count was reached if(launcher.renderer.prepared_items >= launcher.renderer.MaxObjects)return;//simple leave loop if max visible objects count was reached
foreach(i; 0..data.length) foreach(i; 0..data.length)
{ {
launcher.renderer.draw(data.textures[i].tex, data.locations[i].location, vec2(16,16), vec4(0,0,1,1), cast(ushort)(data.locations[i].y*64+data.locations[i].x), uint.max, 0, 0, 0, data.thread_id); launcher.renderer.draw(data.textures[i].tex, data.locations[i].location, vec2(16,16), vec4(0,0,1,1), cast(ushort)(data.locations[i].y*64+data.locations[i].x), 0x80808080, 0, 0, 0, data.thread_id);
//draw(renderer, data.textures[i].tex, data.locations[i], vec2(32,32), vec4(0,0,1,1)); //draw(renderer, data.textures[i].tex, data.locations[i], vec2(32,32), vec4(0,0,1,1));
} }
if(data.thread_id == 0)launcher.renderer.pushData(); if(data.thread_id == 0)launcher.renderer.pushData();

View file

@ -356,7 +356,7 @@ struct AnimationRenderSystem
{ {
foreach(i;0..data.length) 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); 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)], -1, 0x80808080);
} }
} }
} }
@ -617,7 +617,7 @@ struct DrawAppleSystem
{ {
foreach(i; 0..data.location.length) foreach(i; 0..data.location.length)
{ {
launcher.renderer.draw(snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(0,32*px,16*px,16*px), 0, uint.max, 0); launcher.renderer.draw(snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(0,32*px,16*px,16*px), 0, 0x80808080, 0);
} }
} }
} }
@ -687,16 +687,16 @@ struct DrawSnakeSystem
{ {
final switch(cast(ubyte)part) final switch(cast(ubyte)part)
{ {
case SnakePart.tail_up:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,112,16,16)*px, 0, uint.max, 0);break; case SnakePart.tail_up:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,112,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.tail_down:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(0,112,16,16)*px, 0, uint.max, 0);break; case SnakePart.tail_down:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(0,112,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.tail_left:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(32,112,16,16)*px, 0, uint.max, 0);break; case SnakePart.tail_left:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(32,112,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.tail_right:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(0,144,16,16)*px, 0, uint.max, 0);break; case SnakePart.tail_right:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(0,144,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.turn_ld:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(64,128,16,16)*px, 0, uint.max, 0);break; case SnakePart.turn_ld:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(64,128,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.turn_lu:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(32,144,16,16)*px, 0, uint.max, 0);break; case SnakePart.turn_lu:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(32,144,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.turn_rd:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,144,16,16)*px, 0, uint.max, 0);break; case SnakePart.turn_rd:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,144,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.turn_ru:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(64,112,16,16)*px, 0, uint.max, 0);break; case SnakePart.turn_ru:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(64,112,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.vertical:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,128,16,16)*px, 0, uint.max, 0);break; case SnakePart.vertical:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,128,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.horizontal:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(48,128,16,16)*px, 0, uint.max, 0);break; case SnakePart.horizontal:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(48,128,16,16)*px, 0, 0x80808080, 0);break;
} }
} }
@ -708,10 +708,10 @@ struct DrawSnakeSystem
scope vec2 loc = cast(vec2)(data.location[i].location * 16); scope vec2 loc = cast(vec2)(data.location[i].location * 16);
final switch(snake.direction) final switch(snake.direction)
{ {
case CMovement.Direction.up:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(48,112,16,16)*px, 0, uint.max, 0);break; case CMovement.Direction.up:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(48,112,16,16)*px, 0, 0x80808080, 0);break;
case CMovement.Direction.down:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(48,144,16,16)*px, 0, uint.max, 0);break; case CMovement.Direction.down:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(48,144,16,16)*px, 0, 0x80808080, 0);break;
case CMovement.Direction.left:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(0,128,16,16)*px, 0, uint.max, 0);break; case CMovement.Direction.left:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(0,128,16,16)*px, 0, 0x80808080, 0);break;
case CMovement.Direction.right:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(32,128,16,16)*px, 0, uint.max, 0);break; case CMovement.Direction.right:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(32,128,16,16)*px, 0, 0x80808080, 0);break;
} }
if(snake.parts.length >1) if(snake.parts.length >1)
{ {

View file

@ -208,6 +208,24 @@ struct CShootGrid
mixin ECS.Component; mixin ECS.Component;
} }
struct CHitPoints
{
mixin ECS.Component;
alias value this;
int value = 10;
}
struct CHitMark
{
mixin ECS.Component;
alias value this;
ubyte value = 0;
}
/*####################################################################################################################### /*#######################################################################################################################
------------------------------------------------ Events ------------------------------------------------------------------ ------------------------------------------------ Events ------------------------------------------------------------------
#######################################################################################################################*/ #######################################################################################################################*/
@ -224,6 +242,18 @@ struct EChangeDirection
Direction direction; Direction direction;
} }
struct EDamage
{
mixin ECS.Event;
this(uint damage)
{
this.damage = damage;
}
uint damage = 0;
}
/*####################################################################################################################### /*#######################################################################################################################
------------------------------------------------ Systems ------------------------------------------------------------------ ------------------------------------------------ Systems ------------------------------------------------------------------
#######################################################################################################################*/ #######################################################################################################################*/
@ -392,22 +422,47 @@ struct DrawSystem
@readonly CLocation[] locations; @readonly CLocation[] locations;
@readonly CScale[] scale; @readonly CScale[] scale;
@readonly @optional CDepth[] depth; @readonly @optional CDepth[] depth;
@readonly @optional CHitMark[] hit_mark;
} }
void onUpdate(EntitiesData data) void onUpdate(EntitiesData data)
{ {
if(!data.depth) if(!data.depth)
foreach(i; 0..data.length) {
if(data.hit_mark)
{ {
launcher.renderer.draw(data.textures[i].tex, data.locations[i].value, data.scale[i], data.textures[i].coords, cast(short)data.locations[i].y, uint.max, 0, 0, 0, data.thread_id); foreach(i; 0..data.length)
//draw(renderer, data.textures[i].tex, data.locations[i], vec2(32,32), vec4(0,0,1,1)); {
uint color = 0x80808080 + 0x01010101 * data.hit_mark[i];
launcher.renderer.draw(data.textures[i].tex, data.locations[i].value, data.scale[i], data.textures[i].coords, cast(short)data.locations[i].y, color, 0, 0, 0, data.thread_id);
}
} }
else
{
foreach(i; 0..data.length)
{
launcher.renderer.draw(data.textures[i].tex, data.locations[i].value, data.scale[i], data.textures[i].coords, cast(short)data.locations[i].y, 0x80808080, 0, 0, 0, data.thread_id);
}
}
}
else else
foreach(i; 0..data.length) {
if(data.hit_mark)
{ {
launcher.renderer.draw(data.textures[i].tex, data.locations[i].value, data.scale[i], data.textures[i].coords, cast(short)(data.depth[i] * 64 + data.locations[i].y), uint.max, 0, 0, 0, data.thread_id); foreach(i; 0..data.length)
//draw(renderer, data.textures[i].tex, data.locations[i], vec2(32,32), vec4(0,0,1,1)); {
uint color = 0x80808080 + 0x01010101 * data.hit_mark[i];
launcher.renderer.draw(data.textures[i].tex, data.locations[i].value, data.scale[i], data.textures[i].coords, cast(short)(data.depth[i] * 64 + data.locations[i].y), color, 0, 0, 0, data.thread_id);
}
} }
else
{
foreach(i; 0..data.length)
{
launcher.renderer.draw(data.textures[i].tex, data.locations[i].value, data.scale[i], data.textures[i].coords, cast(short)(data.depth[i] * 64 + data.locations[i].y), 0x80808080, 0, 0, 0, data.thread_id);
}
}
}
if(data.thread_id == 0)launcher.renderer.pushData(); if(data.thread_id == 0)launcher.renderer.pushData();
} }
} }
@ -432,7 +487,7 @@ struct LaserShootingSystem
mixin ECS.System!32; mixin ECS.System!32;
bool shoot = false; bool shoot = false;
static float[10] laser_shoot_times = [2000,1500,1000,700,500,300,100,50,10,1]; static float[22] laser_shoot_times = [2000,1500,1000,700,500,350,250,170,130,80,50,25,15,10,5,2.5,2,1,0.8,0.5,0.3,0.1];
CLocation* laser_location; CLocation* laser_location;
CVelocity* laser_velocity; CVelocity* laser_velocity;
@ -523,6 +578,7 @@ struct LaserCollisionSystem
{ {
if(space_invaders.shoot_grid.test(id, data.location[i], cast(ubyte)(~(1 << data.guild[i].guild)))) if(space_invaders.shoot_grid.test(id, data.location[i], cast(ubyte)(~(1 << data.guild[i].guild))))
{ {
launcher.manager.sendEvent(id, EDamage(1));
launcher.manager.removeEntity(data.entity[i].id); launcher.manager.removeEntity(data.entity[i].id);
} }
} }
@ -666,7 +722,7 @@ struct ChangeDirectionSystem
} }
} }
void handleEvent(Entity* entity, EChangeDirection event) /*void handleEvent(Entity* entity, EChangeDirection event)
{ {
CSideMove* side_move = entity.getComponent!CSideMove; CSideMove* side_move = entity.getComponent!CSideMove;
if(side_move && side_move.group != -1) if(side_move && side_move.group != -1)
@ -691,6 +747,46 @@ struct ChangeDirectionSystem
if(velocity.value.x < 0)velocity.value.x = -velocity.value.x; if(velocity.value.x < 0)velocity.value.x = -velocity.value.x;
break; break;
} }
}*/
}
struct HitMarkingSystem
{
mixin ECS.System!16;
struct EntitiesData
{
uint length;
CHitMark[] mark;
}
void onUpdate(EntitiesData data)
{
foreach(i;0..data.length)
{
//if(data.mark[i] < 10)data.mark[i] = 0;
//else data.mark[i] -= 1;
data.mark[i] = cast(ubyte)(data.mark[i] * 0.9);
}
}
}
struct HitPointsSystem
{
mixin ECS.System;
struct EntitiesData
{
CHitPoints[] hp;
}
void handleEvent(Entity* entity, EDamage event)
{
CHitPoints* hp = entity.getComponent!CHitPoints;
*hp -= event.damage;
if(*hp < 0)launcher.manager.removeEntity(entity.id);
CHitMark* hit_mark = entity.getComponent!CHitMark;
if(hit_mark)hit_mark.value = 127;
} }
} }
@ -914,8 +1010,11 @@ void spaceInvadersStart()
launcher.manager.registerComponent!CDepth; launcher.manager.registerComponent!CDepth;
launcher.manager.registerComponent!CShootGrid; launcher.manager.registerComponent!CShootGrid;
launcher.manager.registerComponent!CGuild; launcher.manager.registerComponent!CGuild;
launcher.manager.registerComponent!CHitPoints;
launcher.manager.registerComponent!CHitMark;
launcher.manager.registerEvent!EChangeDirection; launcher.manager.registerEvent!EChangeDirection;
launcher.manager.registerEvent!EDamage;
//launcher.manager.registerSystem!MoveSystem(0); //launcher.manager.registerSystem!MoveSystem(0);
launcher.manager.registerSystem!DrawSystem(100); launcher.manager.registerSystem!DrawSystem(100);
@ -927,6 +1026,8 @@ void spaceInvadersStart()
launcher.manager.registerSystem!LaserCollisionSystem(-70); launcher.manager.registerSystem!LaserCollisionSystem(-70);
launcher.manager.registerSystem!ShootGridManager(-80); launcher.manager.registerSystem!ShootGridManager(-80);
launcher.manager.registerSystem!ShootGridCleaner(-101); launcher.manager.registerSystem!ShootGridCleaner(-101);
launcher.manager.registerSystem!HitPointsSystem(0);
launcher.manager.registerSystem!HitMarkingSystem(-100);
launcher.manager.endRegister(); launcher.manager.endRegister();
@ -939,7 +1040,7 @@ void spaceInvadersStart()
//launcher.manager.getSystem(CleanSystem.system_id).disable(); //launcher.manager.getSystem(CleanSystem.system_id).disable();
{ {
ushort[9] components = [CLocation.component_id, CTexture.component_id, CInput.component_id, CShip.component_id, CScale.component_id, CLaserWeapon.component_id, CShootDirection.component_id, CShootGrid.component_id, CGuild.component_id]; ushort[11] components = [CHitMark.component_id, CHitPoints.component_id, CLocation.component_id, CTexture.component_id, CInput.component_id, CShip.component_id, CScale.component_id, CLaserWeapon.component_id, CShootDirection.component_id, CShootGrid.component_id, CGuild.component_id];
space_invaders.ship_tmpl = launcher.manager.allocateTemplate(components); space_invaders.ship_tmpl = launcher.manager.allocateTemplate(components);
CScale* scale_comp = space_invaders.ship_tmpl.getComponent!CScale; CScale* scale_comp = space_invaders.ship_tmpl.getComponent!CScale;
@ -950,7 +1051,8 @@ void spaceInvadersStart()
CLocation* loc_comp = space_invaders.ship_tmpl.getComponent!CLocation; CLocation* loc_comp = space_invaders.ship_tmpl.getComponent!CLocation;
loc_comp.value = vec2(64,64); loc_comp.value = vec2(64,64);
CLaserWeapon* weapon = space_invaders.ship_tmpl.getComponent!CLaserWeapon; CLaserWeapon* weapon = space_invaders.ship_tmpl.getComponent!CLaserWeapon;
weapon.level = 10; weapon.level = 22;
space_invaders.ship_tmpl.getComponent!CHitPoints().value = 1000;
launcher.manager.addEntity(space_invaders.ship_tmpl); launcher.manager.addEntity(space_invaders.ship_tmpl);
} }
@ -974,7 +1076,7 @@ void spaceInvadersStart()
EntityID grouped_id; EntityID grouped_id;
{ {
ushort[10] 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, CShootGrid.component_id, CGuild.component_id]; ushort[12] components = [CHitMark.component_id, CHitPoints.component_id, CVelocity.component_id, CAutoShoot.component_id, CLocation.component_id, CTexture.component_id, CScale.component_id, CLaserWeapon.component_id, CEnemy.component_id, CShootDirection.component_id, CShootGrid.component_id, CGuild.component_id];
space_invaders.enemy_tmpl = launcher.manager.allocateTemplate(components); space_invaders.enemy_tmpl = launcher.manager.allocateTemplate(components);
CTexture* tex_comp = space_invaders.enemy_tmpl.getComponent!CTexture; CTexture* tex_comp = space_invaders.enemy_tmpl.getComponent!CTexture;