diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bullets.c | 6 | ||||
| -rw-r--r-- | src/game.c | 8 | ||||
| -rw-r--r-- | src/map.c | 56 | ||||
| -rw-r--r-- | src/objects.c | 65 | ||||
| -rw-r--r-- | src/pathfinding.c | 2 | ||||
| -rw-r--r-- | src/players.c | 53 | ||||
| -rw-r--r-- | src/protocol.c | 2 |
7 files changed, 114 insertions, 78 deletions
diff --git a/src/bullets.c b/src/bullets.c index ff96db9..dcdc02a 100644 --- a/src/bullets.c +++ b/src/bullets.c @@ -40,7 +40,6 @@ void shoot(platform_window* window, u32 id, float dirx, float diry) { for (int i = 0; i < max_bullets; i++) { bullet b = bullets[i]; if (b.active) continue; - bullets[i] = (bullet){p->id, true, bulletx, bullety, hh + 0.5,.endx = bullet_end_point_x,.endy = bullet_end_point_y, .damage = g.damage}; break; } @@ -76,7 +75,7 @@ bool check_if_bullet_collided_with_object(bullet* b, platform_window* window) { float dist_of_closest_intersect = __FLT_MAX__; for (int i = 0; i < MAX_OBJECTS; i++) { - object o = objects[i]; + object o = loaded_map.objects[i]; if (!o.active) continue; if (b->position.z <= o.h + o.size.z && b->position.z >= o.h) { box obj_box = get_box_of_square((vec3f){o.position.x, o.position.y, o.h}, o.size); @@ -259,8 +258,7 @@ void draw_bullets(platform_window* window) { b.position.z = p->gun_height; } - if ((int)bullets[i].position.y < (int)bullets[i].endy) { BULLET_RENDER_DEPTH((int)bullets[i].position.y); } - else { BULLET_RENDER_DEPTH((int)bullets[i].endy); } + BULLET_RENDER_DEPTH((int)bullets[i].position.y); float bullet_render_x = b.position.x*info.tile_width + (b.position.y*info.px_incline); float bullet_render_y = b.position.y*info.tile_height - (b.position.z*info.px_raised_per_h); @@ -62,7 +62,8 @@ void load_map() { thread_detach(&send_thread); } - load_map_from_file(); + create_empty_map(); + //load_map_from_file(); create_objects(); pathfinding_init(); @@ -294,7 +295,12 @@ void update_game(platform_window* window) { draw_zombie_chunks(window); draw_drops(window); draw_bullets(window); + + #ifdef MODE_DEBUG + if (!is_editing_map) + #endif draw_zombies(window); + draw_players(window); draw_spawners(window); draw_overlay(window); @@ -93,7 +93,8 @@ static int get_height_of_tile_tr(int current_height, int x, int y) { return highest_point; } -void export_map_from_map_data() { +void load_mapdata_into_world() { + // Load heightmap for (int y = 0; y < MAP_SIZE_Y; y++) { for (int x = MAP_SIZE_X-1; x >= 0; x--) { int h = map_to_load.heightmap[y][x]; @@ -101,11 +102,29 @@ void export_map_from_map_data() { int highest_point_topright = get_height_of_tile_tr(h, x, y); int highest_point_bottomright = get_height_of_tile_br(h, x, y); int highest_point_bottomleft = get_height_of_tile_bl(h, x, y); - loaded_map.heightmap[y][x] = (tile){h, highest_point_topleft, highest_point_topright, highest_point_bottomleft, highest_point_bottomright}; + loaded_map.heightmap[y][x] = (tile){h, TILE_COBBLESTONE1, highest_point_topleft, highest_point_topright, highest_point_bottomleft, highest_point_bottomright}; } } + + // Load objects + memcpy(loaded_map.objects, map_to_load.objects, sizeof(loaded_map.objects)); } +void create_empty_map() { + loaded_map.width = MAP_SIZE_X; + loaded_map.height = MAP_SIZE_Y; + + memset(map_to_load.tiles, TILE_COBBLESTONE1, sizeof(map_to_load.tiles)); + memset(map_to_load.heightmap, 0, sizeof(loaded_map.heightmap)); + + map_to_load.objects[0] = (object){.active = true, .h = 0, .position = (vec2f){16, 8}, .size = (vec3f){1,1,2}, .type = OBJECT_PLANTBOX1}; + + map_to_load.objects[1] = (object){.active = true, .h = 0, .position = (vec2f){0, 0}, .size = (vec3f){1,1,2}, .type = OBJECT_COBBLESTONEWALL1}; + map_to_load.objects[2] = (object){.active = true, .h = 0, .position = (vec2f){0, 1}, .size = (vec3f){1,1,2}, .type = OBJECT_COBBLESTONEWALL1}; + map_to_load.objects[3] = (object){.active = true, .h = 0, .position = (vec2f){0, 2}, .size = (vec3f){1,1,2}, .type = OBJECT_COBBLESTONEWALL1}; + + load_mapdata_into_world(); +} void load_map_from_file() { @@ -120,7 +139,7 @@ void load_map_from_file() { loaded_map.width = map_to_load.width; loaded_map.height = map_to_load.height; - export_map_from_map_data(); + load_mapdata_into_world(); } tile get_tile_under_coords(float x, float y) { @@ -172,6 +191,16 @@ bool is_in_bounds(float x, float y) { return (x >= 0 && x <= MAP_SIZE_X && y >= 0 && y < MAP_SIZE_Y); } +image* get_image_from_tiletype(tile_type tile) { + switch (tile) + { + case TILE_COBBLESTONE1: + return img_tile_cobblestone; + default: + return 0; + } +} + static float offset = 0.0f; static bool dir = true; void draw_grid(platform_window* window) { @@ -216,19 +245,14 @@ void draw_grid(platform_window* window) { if (highest_point_topleft < highest_point_bottomleft || highest_point_topright < highest_point_bottomright || highest_point_topleft > highest_point_topright) c = rgb(255,255,255); - renderer->render_image_quad_tint(img_tile_cobblestone, - topleft.x, topleft.y, - bottomleft.x, bottomleft.y, - bottomright.x, bottomright.y, - topright.x, topright.y, c); - - /* - renderer->render_quad( - topleft.x, topleft.y, - bottomleft.x, bottomleft.y, - bottomright.x, bottomright.y, - topright.x, topright.y, - c);*/ + image* img = get_image_from_tiletype(tile.type); + if (img) { + renderer->render_image_quad_tint(img, + topleft.x, topleft.y, + bottomleft.x, bottomleft.y, + bottomright.x, bottomright.y, + topright.x, topright.y, c); + } if (highest_point_topleft != highest_point_bottomright && highest_point_bottomleft == highest_point_topright) { if (highest_point_bottomleft < highest_point_topleft || highest_point_bottomleft < highest_point_bottomright) { diff --git a/src/objects.c b/src/objects.c index c8a150c..3969000 100644 --- a/src/objects.c +++ b/src/objects.c @@ -31,13 +31,25 @@ void render_quad_with_outline(vec2f tl, vec2f tr, vec2f bl, vec2f br, color c) { object get_object_at_tile(float x, float y) { for (int i = 0; i < MAX_OBJECTS; i++) { - object o = objects[i]; + object o = loaded_map.objects[i]; if (!o.active) continue; if (x >= o.position.x && x < o.position.x + o.size.x && y >= o.position.y && y < o.position.y + o.size.y) return o; } return (object){0}; } +image* get_image_from_objecttype(object_type tile) { + switch (tile) + { + case OBJECT_COBBLESTONEWALL1: + return img_obj_wall1; + case OBJECT_PLANTBOX1: + return img_obj_plants; + default: + return 0; + } +} + void draw_objects_at_row(platform_window* window, int row) { map_info info = get_map_info(window); @@ -48,8 +60,12 @@ void draw_objects_at_row(platform_window* window, int row) { if (!o.active) continue; box box = get_box_of_object(window, o); - renderer->render_image(o.image, box.tl_u.x, box.tl_u.y, - box.br_d.x - box.tl_d.x, box.br_d.y - box.tr_u.y); + + image* img = get_image_from_objecttype(o.type); + if (img) { + renderer->render_image(img, box.tl_u.x, box.tl_u.y, + box.br_d.x - box.tl_d.x, box.br_d.y - box.tr_u.y); + } /* render_quad_with_outline(box.tl_d, box.tr_d, box.bl_d, box.br_d, rgb(200,200,0)); render_quad_with_outline(box.tl_u, box.tr_u, box.bl_u, box.br_u, rgb(200,200,0)); @@ -58,47 +74,32 @@ void draw_objects_at_row(platform_window* window, int row) { } } -void create_box(float x, float y, float h, image* img) { - for (int i = 0; i < MAX_OBJECTS; i++) { - object o = objects[i]; - if (o.active) continue; - - objects[i].active = true; - objects[i].position = (vec2f){x, y}; - objects[i].h = h; - objects[i].size = (vec3f){1,1,2}; - objects[i].image = img; - break; - } -} - void create_objects() { + /* // rechts naar links op map. - - for (int i = MAP_SIZE_X-1; i >= 0; i--) { - create_box(i, 0, 0, img_obj_wall1); - create_box(i, MAP_SIZE_Y-1, 0, img_obj_wall1); + create_box(i, 0, 0, OBJECT_COBBLESTONEWALL1); + create_box(i, MAP_SIZE_Y-1, 0, OBJECT_COBBLESTONEWALL1); } for (int i = MAP_SIZE_Y-1; i >= 0; i--) { - create_box(0, i, 0, img_obj_wall1); - create_box(MAP_SIZE_X-1, i, 0, img_obj_wall1); + create_box(0, i, 0, OBJECT_COBBLESTONEWALL1); + create_box(MAP_SIZE_X-1, i, 0, OBJECT_COBBLESTONEWALL1); } - create_box(16, 8, 0, img_obj_plants); - create_box(14, 8, 0, img_obj_plants); - create_box(11, 8, 0, img_obj_plants); - create_box(10, 8, 0, img_obj_plants); + create_box(16, 8, 0, OBJECT_PLANTBOX1); + create_box(14, 8, 0, OBJECT_PLANTBOX1); + create_box(11, 8, 0, OBJECT_PLANTBOX1); + create_box(10, 8, 0, OBJECT_PLANTBOX1); - create_box(15, 10, 0, img_obj_plants); - create_box(14, 10, 0, img_obj_plants); - create_box(13, 10, 0, img_obj_plants); - create_box(11, 10, 0, img_obj_plants); + create_box(15, 10, 0, OBJECT_PLANTBOX1); + create_box(14, 10, 0, OBJECT_PLANTBOX1); + create_box(13, 10, 0, OBJECT_PLANTBOX1); + create_box(11, 10, 0, OBJECT_PLANTBOX1); create_spawner((vec2){15, 5}); create_spawner((vec2){3, 8}); create_spawner((vec2){11, 18}); - + */ create_wallitem((vec3f){14, 1, 0}, WALLITEM_GUN, (wall_item_data){.gun = GUN_NOVA}); }
\ No newline at end of file diff --git a/src/pathfinding.c b/src/pathfinding.c index 63741e7..70b084b 100644 --- a/src/pathfinding.c +++ b/src/pathfinding.c @@ -8,7 +8,7 @@ static float distance_between(vec2f v1, vec2f v2) bool can_walk_at(float x, float y) { for (int i = 0; i < MAX_OBJECTS; i++) { - object o = objects[i]; + object o = loaded_map.objects[i]; if (!o.active) continue; if (x >= o.position.x && x < o.position.x + o.size.x && y >= o.position.y && y < o.position.y + o.size.y) return false; } diff --git a/src/players.c b/src/players.c index f3c7c6e..05da02c 100644 --- a/src/players.c +++ b/src/players.c @@ -121,7 +121,7 @@ object check_if_player_collided_with_object(platform_window* window, player p) { float player_size = get_player_size(window); for (int i = 0; i < MAX_OBJECTS; i++) { - object o = objects[i]; + object o = loaded_map.objects[i]; if (!o.active) continue; if (check_if_player_collided_with_box(p, get_box_of_square((vec3f){o.position.x, o.position.y, o.h}, o.size))) { @@ -140,7 +140,7 @@ int get_my_player_index() { return -1; } -static bool is_editing_map = false; +#ifdef MODE_DEBUG void draw_debug(platform_window* window) { if (is_editing_map) { map_info info = get_map_info(window); @@ -155,23 +155,30 @@ void draw_debug(platform_window* window) { if (is_left_clicked()) { map_to_load.heightmap[mouse_tile_y][mouse_tile_x]++; - export_map_from_map_data(); + load_mapdata_into_world(); + } + if (is_right_clicked()) { + map_to_load.heightmap[mouse_tile_y][mouse_tile_x]--; + load_mapdata_into_world(); } } } static void take_update_debug(platform_window* window) { + if (keyboard_is_key_pressed(KEY_F1)) { + is_editing_map = !is_editing_map; + log_infox("Editing map: %d", is_editing_map); + } + + if (!is_editing_map) return; + if (keyboard_is_key_down(KEY_LEFT_CONTROL) && keyboard_is_key_pressed(KEY_S)) { platform_write_file_content("../data/maps/map1.dat", "wb", (u8*)&map_to_load, sizeof(map_to_load)); platform_write_file_content("data/maps/map1.dat", "wb", (u8*)&map_to_load, sizeof(map_to_load)); log_info("Saved map"); } - - if (keyboard_is_key_pressed(KEY_F1)) { - is_editing_map = !is_editing_map; - log_infox("Editing map: %d", is_editing_map); - } } +#endif void take_player_input(platform_window* window) { player* p = get_player_by_id(player_id); @@ -183,11 +190,11 @@ void take_player_input(platform_window* window) { if (keyboard_is_key_down(KEY_W)) { network_message message = create_protocol_user_moved(MOVE_UP, player_id); - add_message_to_outgoing_queuex(message, *global_state.client); + add_message_to_outgoing_queuex(message, *global_state.client); } if (keyboard_is_key_down(KEY_S)) { network_message message = create_protocol_user_moved(MOVE_DOWN, player_id); - add_message_to_outgoing_queuex(message, *global_state.client); + add_message_to_outgoing_queuex(message, *global_state.client); } if (keyboard_is_key_down(KEY_A)) { network_message message = create_protocol_user_moved(MOVE_LEFT, player_id); @@ -198,6 +205,10 @@ void take_player_input(platform_window* window) { add_message_to_outgoing_queuex(message, *global_state.client); } +#ifdef MODE_DEBUG + if (is_editing_map) return; +#endif + // Send gun position { float dirx = (_global_mouse.x - (window->width/2)); @@ -212,20 +223,16 @@ void take_player_input(platform_window* window) { add_message_to_outgoing_queuex(create_protocol_user_look(player_id, gun_offset_x, gun_offset_y), *global_state.client); } - // shoot - if (is_editing_map) return; + // shooting + if (is_left_down()) { + float dirx = (_global_mouse.x - (window->width/2)); + float diry = (_global_mouse.y - (window->height/2)); + double length = sqrt(dirx * dirx + diry * diry); + dirx /= length; + diry /= length; - { - if (is_left_down()) { - float dirx = (_global_mouse.x - (window->width/2)); - float diry = (_global_mouse.y - (window->height/2)); - double length = sqrt(dirx * dirx + diry * diry); - dirx /= length; - diry /= length; - - network_message message = create_protocol_user_shoot(player_id, dirx, diry); - add_message_to_outgoing_queuex(message, *global_state.client); - } + network_message message = create_protocol_user_shoot(player_id, dirx, diry); + add_message_to_outgoing_queuex(message, *global_state.client); } } diff --git a/src/protocol.c b/src/protocol.c index 555295c..e0af132 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -1,7 +1,7 @@ #include "../include/protocol.h" #include "../include/players.h" -#define alloc_network_message(_type) mem_alloc(sizeof(_type) + 20); +#define alloc_network_message(_type) mem_alloc(sizeof(_type) + NETWORK_PACKET_OVERHEAD); network_message create_protocol_get_id_up(u32 id) { |
