survival game wip

efd/v1
Dominik Madarász 2023-02-01 15:28:30 +01:00
parent 719b002989
commit 3f5d999d44
17 changed files with 344 additions and 210 deletions

BIN
art/gen/player.png 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -23,6 +23,9 @@ void game_input();
void game_update();
void game_render();
void game_player_joined(uint64_t ent);
void game_player_departed(uint64_t ent);
//~ Called from platform.c
void game_draw_ui();

View File

@ -139,7 +139,6 @@ void tooltip_clear(void) {
inline void tooltip_draw_contents(tooltip *desc) {
if (!desc) return;
nk_layout_row_dynamic(game_ui, 0, 1);
nk_label_wrap(game_ui, desc->content);
}

View File

@ -32,6 +32,7 @@ static asset assets[] = {
ASSET_TEX(ASSET_BLUEPRINT),
ASSET_TEX(ASSET_BLUEPRINT_DEMO_HOUSE),
ASSET_TEX(ASSET_MOB),
ASSET_TEX(ASSET_PLAYER),
// NOTE(zaklaus): blocks
ASSET_TEX(ASSET_FENCE),

View File

@ -6,6 +6,8 @@
#include "models/entity.h"
#include "models/components.h"
#include "core/game.h"
#define PLAYER_MAX_HP 100.0f
uint64_t player_spawn(char *name) {
@ -30,5 +32,6 @@ uint64_t player_spawn(char *name) {
}
void player_despawn(uint64_t ent_id) {
game_player_departed(ent_id);
entity_despawn(ent_id);
}

View File

@ -31,6 +31,8 @@ int32_t pkt_00_init_handler(pkt_header *header) {
entity_set_position(ent_id, world_dim()/2.0f + rand()%15*15.0f, world_dim()/2.0f + rand()%15*15.0f);
game_player_joined(ent_id);
zpl_printf("[INFO] initializing player entity id: %d with view id: %d for peer id: %d...\n", ent_id, table.view_id, peer_id);
ecs_set(world_ecs(), ent_id, ClientInfo, {.peer = peer_id, .view_id = header->view_id, .active = false });
pkt_01_welcome_send(world_seed(), peer_id, header->view_id, ent_id, world_chunk_size(), world_chunk_amount());

View File

@ -14,12 +14,12 @@ ZPL_DIAGNOSTIC_PUSH_WARNLEVEL(0)
ZPL_DIAGNOSTIC_POP
#define PHY_BLOCK_COLLISION 1
#define PHY_C2_BLOCK_COLLISION 0
#define PHY_WALK_DRAG 4.23f
#define PHY_LOOKAHEAD(x) (zpl_sign(x)*16.0f)
ecs_query_t *ecs_rigidbodies;
#define ECO2D_TICK_RATE (1.0f/20.f)
ecs_query_t *ecs_rigidbodies = 0;
ecs_entity_t ecs_timer = 0;
#include "modules/system_onfoot.c"
#include "modules/system_demo.c"
@ -28,7 +28,6 @@ ecs_query_t *ecs_rigidbodies;
#include "modules/system_logistics.c"
#include "modules/system_producer.c"
#include "modules/system_blueprint.c"
#include "modules/system_mob.c"
static inline float physics_correction(float x, float vx, float bounce, float dim) {
float r = (((zpl_max(0.0f, dim - zpl_abs(x))*zpl_sign(x)))*dim);
@ -39,6 +38,23 @@ static inline bool physics_check_aabb(float a1x, float a2x, float a1y, float a2y
return (a1x < b2x && a2x > b1x && a1y < b2y && a2y > b1y);
}
static inline bool BlockCollisionIslandTest(Position *p, librg_chunk ch_p) {
// collect islands
collision_island islands[16];
uint8_t num_islands = world_chunk_collision_islands(ch_p, islands);
for (uint8_t i = 0; i < num_islands; i++) {
#if 1
{
zpl_printf("px %f py %f minx %f miny %f\n", p->x, p->y, islands[i].minx, islands[i].miny);
debug_v2 a = {islands[i].minx, islands[i].miny};
debug_v2 b = {islands[i].maxx+WORLD_BLOCK_SIZE, islands[i].maxy+WORLD_BLOCK_SIZE};
debug_push_rect(a, b, 0xFFFFFFFF);
}
#endif
}
return 0;
}
void BlockCollisions(ecs_iter_t *it) {
profile(PROF_PHYS_BLOCK_COLS) {
@ -59,6 +75,16 @@ void BlockCollisions(ecs_iter_t *it) {
p[i].y = zpl_clamp(p[i].y, 0, w-1);
}
#if PHY_C2_BLOCK_COLLISION==1
// collision islands
{
librg_chunk chunk_id = librg_chunk_from_realpos(world_tracker(), p[i].x, p[i].y, 0);
if (BlockCollisionIslandTest((p+i), chunk_id))
continue;
}
#endif
#if PHY_BLOCK_COLLISION==1
// NOTE(zaklaus): X axis
{
@ -141,17 +167,16 @@ void BodyCollisions(ecs_iter_t *it) {
float p2_y = p2[j].y /*+ v2[j].y*/;
c2AABB box_a = {
.min = { p_x - WORLD_BLOCK_SIZE / 2, p_y - WORLD_BLOCK_SIZE / 2 },
.max = { p_x + WORLD_BLOCK_SIZE / 2, p_y + WORLD_BLOCK_SIZE / 2 },
.min = { p_x - WORLD_BLOCK_SIZE / 2, p_y - WORLD_BLOCK_SIZE / 4 },
.max = { p_x + WORLD_BLOCK_SIZE / 2, p_y + WORLD_BLOCK_SIZE / 4 },
};
c2AABB box_b = {
.min = { p2_x - WORLD_BLOCK_SIZE / 2, p2_y - WORLD_BLOCK_SIZE / 2 },
.max = { p2_x + WORLD_BLOCK_SIZE / 2, p2_y + WORLD_BLOCK_SIZE / 2 },
.min = { p2_x - WORLD_BLOCK_SIZE / 2, p2_y - WORLD_BLOCK_SIZE / 4 },
.max = { p2_x + WORLD_BLOCK_SIZE / 2, p2_y + WORLD_BLOCK_SIZE / 4 },
};
// do a basic sweep first
{
float r1x = (box_a.max.x-box_a.min.x);
float r1y = (box_a.max.y-box_a.min.y);
float r1 = (r1x*r1x + r1y*r1y)*.5f;
@ -160,6 +185,7 @@ void BodyCollisions(ecs_iter_t *it) {
float r2y = (box_b.max.y-box_b.min.y);
float r2 = (r2x*r2x + r2y*r2y)*.5f;
{
float dx = (p2_x-p_x);
float dy = (p2_y-p_y);
float d = (dx*dx + dy*dy);
@ -170,12 +196,12 @@ void BodyCollisions(ecs_iter_t *it) {
c2Circle circle_a = {
.p = { p_x, p_y },
.r = b[i].circle.r,
.r = r1/2.f,
};
c2Circle circle_b = {
.p = { p2_x, p2_y },
.r = b2[j].circle.r,
.r = r2/2.f,
};
const void *shapes_a[] = { &circle_a, &box_a };
@ -365,19 +391,10 @@ void DisableWorldEdit(ecs_iter_t *it) {
world_set_stage(NULL);
}
#define ECS_SYSTEM_TICKED(world, id, stage, ...)\
ECS_SYSTEM(world, id, stage, __VA_ARGS__);\
ecs_set_tick_source(world, id, timer);
#define ECS_SYSTEM_TICKED_EX(world, id, stage, time, ...)\
ECS_SYSTEM(world, id, stage, __VA_ARGS__);\
ecs_entity_t timer_##id = ecs_set_interval(ecs, 0, ECO2D_TICK_RATE*time);\
ecs_set_tick_source(world, id, timer_##id);
void SystemsImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Systems);
ecs_entity_t timer = ecs_set_interval(ecs, 0, ECO2D_TICK_RATE);
ecs_timer = ecs_set_interval(ecs, 0, ECO2D_TICK_RATE);
ecs_rigidbodies = ecs_query_new(ecs, "components.Position, components.Velocity, components.PhysicsBody");
@ -419,10 +436,6 @@ void SystemsImport(ecs_world_t *ecs) {
ECS_SYSTEM_TICKED(ecs, CreatureSeekCompanion, EcsPostUpdate, components.Creature, components.Position, components.Velocity, components.SeeksCompanion, !components.SeeksFood);
ECS_SYSTEM(ecs, CreatureRoamAround, EcsPostUpdate, components.Velocity, components.Creature, !components.SeeksFood, !components.SeeksCompanion);
ECS_SYSTEM_TICKED_EX(ecs, MobDetectPlayers, EcsPostUpdate, 100.0f, components.Position, components.Mob);
ECS_SYSTEM(ecs, MobMovement, EcsPostUpdate, components.Velocity, components.Position, components.MobHuntPlayer);
ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee);
ECS_SYSTEM(ecs, ResetActivators, EcsPostUpdate, components.Input);
ECS_SYSTEM(ecs, ClearVehicle, EcsUnSet, components.Vehicle);

View File

@ -5,6 +5,21 @@ static inline float safe_dt(ecs_iter_t *it) {
return zpl_min(it->delta_time, 0.03334f);
}
extern ecs_query_t *ecs_rigidbodies;
extern ecs_entity_t ecs_timer;
#define TICK_VAR(var) (var) = zpl_max((var)-1, 0)
#define ECO2D_TICK_RATE (1.0f/20.f)
#define ECS_SYSTEM_TICKED(world, id, stage, ...)\
ECS_SYSTEM(world, id, stage, __VA_ARGS__);\
ecs_set_tick_source(world, id, ecs_timer);
#define ECS_SYSTEM_TICKED_EX(world, id, stage, time, ...)\
ECS_SYSTEM(world, id, stage, __VA_ARGS__);\
ecs_entity_t timer_##id = ecs_set_interval(ecs, 0, ECO2D_TICK_RATE*time);\
ecs_set_tick_source(world, id, timer_##id);
void SystemsImport(ecs_world_t *ecs);

View File

@ -200,6 +200,46 @@ void world_setup_pkt_handlers(world_pkt_reader_proc *reader_proc, world_pkt_writ
world.writer_proc = writer_proc;
}
void world_rebuild_chunk_islands(librg_chunk chunk_id) {
uint16_t ch_x, ch_y;
librg_chunk_to_chunkpos(world.tracker, chunk_id, &ch_x, &ch_y, NULL);
float wp_x = (float)ch_x * world_dim(), wp_y = (float)ch_y * world_dim();
world.islands_count[chunk_id] = 0;
collision_island clr_island = { ZPL_F32_MAX, ZPL_F32_MAX, ZPL_F32_MIN, ZPL_F32_MIN };
collision_island new_island = clr_island;
for (int y = 0; y < world.chunk_size; y += 1) {
for (int x = 0; x < world.chunk_size; x += 1) {
block_id c = world.block_mapping[chunk_id][(y * world.chunk_size) + x];
float wx = x * WORLD_BLOCK_SIZE + wp_x;
float wy = y * WORLD_BLOCK_SIZE + wp_y;
if (blocks_get_flags(c) & BLOCK_FLAG_COLLISION) {
if (new_island.minx > wx)
new_island.minx = wx;
if (new_island.miny > wy)
new_island.miny = wy;
if (new_island.maxx < wx)
new_island.maxx = wx;
if (new_island.maxy < wy)
new_island.maxy = wy;
}
else if (zpl_memcompare(&new_island, &clr_island, sizeof(collision_island))) {
world.islands[chunk_id * 16 + world.islands_count[chunk_id]] = new_island;
world.islands_count[chunk_id]++;
new_island = clr_island;
}
}
}
if (zpl_memcompare(&new_island, &clr_island, sizeof(collision_island))) {
world.islands[chunk_id * 16 + world.islands_count[chunk_id]] = new_island;
world.islands_count[chunk_id]++;
}
}
static inline
void world_chunk_setup_grid(void) {
for (int i = 0; i < zpl_square(world.chunk_amount); ++i) {
@ -227,6 +267,8 @@ void world_chunk_setup_grid(void) {
*c = world.outer_data[(chk_y + y) * world.dim + (chk_x + x)];
}
}
world_rebuild_chunk_islands(i);
}
}
@ -269,6 +311,8 @@ void world_init_mapping(void) {
world.chunk_mapping = zpl_malloc(sizeof(ecs_entity_t) * zpl_square(world.chunk_amount));
world.block_mapping = zpl_malloc(sizeof(block_id*) * zpl_square(world.chunk_amount));
world.outer_block_mapping = zpl_malloc(sizeof(block_id*) * zpl_square(world.chunk_amount));
world.islands_count = zpl_malloc(sizeof(world.islands_count[0]) * zpl_square(world.chunk_amount));
world.islands = zpl_malloc(sizeof(collision_island) * 16 * zpl_square(world.chunk_amount));
world_snapshot_init(&streamer_snapshot, zpl_heap());
world_component_cache_init(&component_cache, zpl_heap());
}
@ -326,6 +370,8 @@ int32_t world_destroy(void) {
}
zpl_mfree(world.block_mapping);
zpl_mfree(world.outer_block_mapping);
zpl_mfree(world.islands_count);
zpl_mfree(world.islands);
world_snapshot_destroy(&streamer_snapshot);
world_component_cache_destroy(&component_cache);
zpl_memset(&world, 0, sizeof(world));
@ -358,7 +404,8 @@ static void world_tracker_update(uint8_t ticker, float freq, uint8_t radius) {
if (result > 0) {
zpl_printf("[info] buffer size was not enough, please increase it by at least: %d\n", result);
} else if (result < 0) {
}
else if (result < 0) {
zpl_printf("[error] an error happened writing the world %d\n", result);
}
@ -582,6 +629,11 @@ int64_t world_chunk_from_realpos(float x, float y) {
return world.chunk_mapping[chunk_id];
}
uint8_t world_chunk_collision_islands(librg_chunk id, collision_island* islands) {
zpl_memcopy(islands, world.islands + id * 16, sizeof(collision_island) * world.islands_count[id]);
return world.islands_count[id];
}
int64_t world_chunk_from_entity(ecs_entity_t id) {
return librg_entity_chunk_get(world.tracker, id);
}
@ -599,7 +651,8 @@ void world_chunk_replace_block(int64_t id, uint16_t block_idx, block_id bid) {
ecs_entity_t e = entity_spawn_id(blocks_get_asset(bid));
world_block_lookup l = world_block_from_index(id, block_idx);
entity_set_position(e, l.ox, l.oy);
} else {
}
else {
world.outer_block_mapping[id][block_idx] = bid;
world_chunk_mark_dirty(world.chunk_mapping[id]);
}
@ -612,7 +665,8 @@ bool world_chunk_place_block(int64_t id, uint16_t block_idx, block_id bid) {
ecs_entity_t e = entity_spawn_id(blocks_get_asset(bid));
world_block_lookup l = world_block_from_index(id, block_idx);
entity_set_position(e, l.ox, l.oy);
} else {
}
else {
world.outer_block_mapping[id][block_idx] = bid;
world_chunk_mark_dirty(world.chunk_mapping[id]);
}

View File

@ -29,6 +29,11 @@ typedef WORLD_PKT_READER(world_pkt_reader_proc);
#define WORLD_PKT_WRITER(name) int32_t name(pkt_header *pkt, void *udata)
typedef WORLD_PKT_WRITER(world_pkt_writer_proc);
typedef struct {
float minx, miny;
float maxx, maxy;
} collision_island;
typedef struct {
bool is_paused;
block_id *data;
@ -40,6 +45,8 @@ typedef struct {
block_id **block_mapping;
block_id **outer_block_mapping;
uint16_t dim;
uint8_t *islands_count;
collision_island *islands;
float tracker_update[3];
uint8_t active_layer_id;
ecs_world_t *ecs;
@ -78,6 +85,8 @@ uint16_t world_chunk_size(void);
uint16_t world_chunk_amount(void);
uint16_t world_dim(void);
ecs_entity_t world_chunk_mapping(librg_chunk id);
void world_rebuild_chunk_islands(librg_chunk chunk_id);
uint8_t world_chunk_collision_islands(librg_chunk id, collision_island *islands);
typedef struct {
uint16_t id;

View File

@ -80,3 +80,7 @@ int main(int argc, char** argv) {
zpl_opts_free(&opts);
return 0;
}
//------------------------------------------------------------------------
void game_player_joined(uint64_t ent) {}
void game_player_departed(uint64_t ent) {}

View File

@ -84,3 +84,7 @@ int main(int argc, char** argv) {
zpl_opts_free(&opts);
return 0;
}
//------------------------------------------------------------------------
void game_player_joined(uint64_t ent) {}
void game_player_departed(uint64_t ent) {}

View File

@ -16,6 +16,10 @@
#include "platform/arch.h"
ZPL_DIAGNOSTIC_PUSH_WARNLEVEL(0)
#include "tinyc2.h"
ZPL_DIAGNOSTIC_POP
#define DEFAULT_WORLD_SEED 302097
#define DEFAULT_CHUNK_SIZE 16 /* amount of blocks within a chunk (single axis) */
#define DEFAULT_WORLD_SIZE 5 /* amount of chunks within a world (single axis) */
@ -33,6 +37,15 @@
- somewhat believable world gen, small hamlets with cols, etc
*/
#include "system_mob.c"
void mob_systems(ecs_world_t *ecs) {
ECS_SYSTEM_TICKED_EX(ecs, MobDetectPlayers, EcsPostUpdate, 100.0f, components.Position, components.Mob);
ECS_SYSTEM(ecs, MobMovement, EcsPostUpdate, components.Velocity, components.Position, components.MobHuntPlayer);
ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee);
ECS_OBSERVER(ecs, MobDetectPlayers1, EcsOnAdd, components.Mob);
}
int main(int argc, char** argv) {
zpl_opts opts={0};
zpl_opts_init(&opts, zpl_heap(), argv[0]);
@ -84,6 +97,10 @@ int main(int argc, char** argv) {
sighandler_register();
game_init(host, port, play_mode, 1, seed, chunk_size, world_size, 0);
{
mob_systems(world_ecs());
}
game_run();
game_shutdown();
@ -93,3 +110,7 @@ int main(int argc, char** argv) {
zpl_opts_free(&opts);
return 0;
}
//------------------------------------------------------------------------
void game_player_joined(uint64_t ent) {}
void game_player_departed(uint64_t ent) {}

View File

@ -88,18 +88,19 @@ void renderer_draw_entry(uint64_t key, entity_view *data, game_world_render_entr
float x = data->x;
float y = data->y;
float health = (data->hp / data->max_hp);
DrawNametag("Player", key, data, x, y);
DrawCircleEco(x, y, size, ColorAlpha(YELLOW, data->tran_time));
DrawNametag("Player", key, data, x, y-16);
DrawTextureRec(GetSpriteTexture2D(assets_find(ASSET_PLAYER)), ASSET_SRC_RECT(), (Vector2){data->x-(WORLD_BLOCK_SIZE/2), data->y-(WORLD_BLOCK_SIZE/2)}, ColorAlpha(WHITE, data->tran_time));
//DrawCircleEco(x, y, size, ColorAlpha(YELLOW, data->tran_time));
if (data->has_items && !data->inside_vehicle) {
float ix = data->x;
float iy = data->y;
if (data->items[data->selected_item].quantity > 0) {
asset_id it_kind = data->items[data->selected_item].kind;
uint32_t qty = data->items[data->selected_item].quantity;
DrawTexturePro(GetSpriteTexture2D(assets_find(it_kind)), ASSET_SRC_RECT(), ((Rectangle){ix, iy, 32, 32}), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE));
}
}
//if (data->has_items && !data->inside_vehicle) {
// float ix = data->x;
// float iy = data->y;
// if (data->items[data->selected_item].quantity > 0) {
// asset_id it_kind = data->items[data->selected_item].kind;
// uint32_t qty = data->items[data->selected_item].quantity;
// DrawTexturePro(GetSpriteTexture2D(assets_find(it_kind)), ASSET_SRC_RECT(), ((Rectangle){ix, iy, 32, 32}), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE));
// }
//}
}break;
case EKIND_ITEM: {
float x = data->x - 32.f;

View File

@ -27,6 +27,10 @@ void MobDetectPlayers(ecs_iter_t *it) {
}
}
void MobDetectPlayers1(ecs_iter_t *it) {
MobDetectPlayers(it);
}
#define MOB_MOVEMENT_SPEED 300.0f
void MobMovement(ecs_iter_t *it) {
@ -50,7 +54,7 @@ void MobMovement(ecs_iter_t *it) {
}
#define MOB_MELEE_DIST 4000.0f
#define MOB_MELEE_DMG 5.0f
#define MOB_MELEE_DMG 1.5f
#define MOB_ATK_DELAY 10
void MobMeleeAtk(ecs_iter_t *it) {
@ -73,7 +77,7 @@ void MobMeleeAtk(ecs_iter_t *it) {
Health *hp = ecs_get_mut_ex(it->world, m->plr, Health);
hp->hp = zpl_max(hp->hp-MOB_MELEE_DMG, 0.0f);
ecs_add(it->world, m->plr, HealthDecreased);
}
mob[i].atk_delay = MOB_ATK_DELAY;
}
}
}

View File

@ -12,6 +12,7 @@ Texture2D texgen_build_anim(asset_id id, int64_t counter) {
Texture2D texgen_build_sprite(asset_id id) {
switch (id) {
case ASSET_PLAYER: return LoadTexEco("player");
default: return texgen_build_sprite_fallback(id); break;
}
}