1
1
Fork 0
Browse Source

multiple levels

thread-physics
alistair 4 years ago
parent
commit
7cbdf4b4e9
  1. 4
      draw.c
  2. 11
      environment.c
  3. 1
      environment.h
  4. 106
      game.c
  5. 13
      game.h

4
draw.c

@ -399,7 +399,7 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -399,7 +399,7 @@ void redraw_buffer(SDL_Renderer * ren) {
//SDL_GetMouseState(&newmousex, &newmousey);
update_viewport(player.physics->position.x, player.physics->position.y);
struct environment scene = get_scene_at(viewport_pos, 0);
struct environment scene = get_scene_at(viewport_pos, level);
draw_environment(ren, &scene);
for (int i=0; i<world.size; i++) {
@ -417,14 +417,12 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -417,14 +417,12 @@ void redraw_buffer(SDL_Renderer * ren) {
draw_collision_poly(ren, thing.wall->physics);
break;
case FLOOR:
break;
for (int i = 0; i < thing.floor->numPolys; i++) {
draw_floor(ren, &thing.floor->polys[i], true);
draw_collision_poly(ren, thing.floor->polys[i].physics);
}
break;
case CEILING:
break;
for (int i = 0; i < thing.floor->numPolys; i++) {
draw_floor(ren, &thing.floor->polys[i], false);
draw_collision_poly(ren, thing.floor->polys[i].physics);

11
environment.c

@ -77,14 +77,16 @@ struct environment get_scene_at(Vect coordinate, int seed) { @@ -77,14 +77,16 @@ struct environment get_scene_at(Vect coordinate, int seed) {
static bool init;
static Vect last_room;
static struct environment e;
static int oldseed;
Vect room_coord;
room_coord.x = (int)coordinate.x - (int)coordinate.x % E_ROOM_WIDTH;
room_coord.y = 0;
if (init && room_coord.x == last_room.x) {
if (init && room_coord.x == last_room.x && oldseed == seed) {
return e;
}
last_room = room_coord;
oldseed = seed;
e.floor = new_arlst_wfree(E_ROOM_TILES, free);
e.ceil = new_arlst_wfree(E_ROOM_TILES, free);
@ -101,25 +103,24 @@ struct environment get_scene_at(Vect coordinate, int seed) { @@ -101,25 +103,24 @@ struct environment get_scene_at(Vect coordinate, int seed) {
Vect node;
node.y = 0;
for (int i = 0; i < E_ROOM_WIDTH; i += E_ROOM_RES) {
bit.x = room_coord.x + i;
bit.y = seed;
node.x = bit.x;
int r1 = 1.5*(rand() % 500) - (rand() % 100);
int r1 = 1.5*(rand() % 500) - (rand() % 100);
int r2 = 1.5*(rand() % 200) - (rand() % 200);
int r3 = (rand() % 100) - (rand() % 500);
int r4 = 50;
int r[4] = {r1, r2, r3, r4};
qsort(r, 4, sizeof(int), comp);
// r[0] == ceiling
// r[3] == floor
node.y += r[0];
// node.y = fmod(perlin(bit), 3000);
Vect *z = malloc(sizeof(Vect));
*z = node;

1
environment.h

@ -17,6 +17,7 @@ struct environment { @@ -17,6 +17,7 @@ struct environment {
ArrayList bg1;
ArrayList bg2;
struct colour_pallete colours;
struct physics_collection physics;
};
struct environment get_scene_at(Vect coordinate, int seed);

106
game.c

@ -1,10 +1,13 @@ @@ -1,10 +1,13 @@
#include "game.h"
#include "environment.h"
#define FLOOR_THICKNESS 200
GlobWorld world;
player_st *glob_player;
int level;
/* array of all the things in the world and their kinds */
//world_thing *world;
@ -13,6 +16,7 @@ void startgame(SDL_Renderer * ren) ; @@ -13,6 +16,7 @@ void startgame(SDL_Renderer * ren) ;
void process_keydown(SDL_Keysym key);
void process_keyup(SDL_Keysym key);
void add_to_world(world_thing thing);
void step(int interval);
player_st player;
@ -71,6 +75,7 @@ void default_motor_curve(Motor *motor) { @@ -71,6 +75,7 @@ void default_motor_curve(Motor *motor) {
return;
}
FloorPoly* generate_floor_simple(int num_polys, bool extend_down, int st_height) {
FloorPoly *floor = calloc(num_polys, sizeof(FloorPoly));
Vect last, next;
@ -492,6 +497,93 @@ bool check_collision(Body *one, Body *two, Vect *translation) { @@ -492,6 +497,93 @@ bool check_collision(Body *one, Body *two, Vect *translation) {
}
}
void next_level() {
Vect v;
v.x = 0;
v.y = 0;
player.physics->vel = v;
player.physics->acc = v;
player.physics->position.x = 0;
player.physics->position.y = 0;
player.physics->position = v;
player.physics->next_position = v;
level++;
}
int get_floor_ceiling() {
struct environment e = get_scene_at(viewport_pos, level);
struct physics_collection floor;
struct physics_collection ceil;
floor.items = calloc(e.floor.size, sizeof(Body*));
ceil.items = calloc(e.floor.size, sizeof(Body*));
floor.numItems = e.floor.size - 1;
ceil.numItems = e.floor.size - 1;
for (int i = 0; i < e.floor.size-1; i++) {
get_new_physics(&floor.items[i]);
get_new_physics(&ceil.items[i]);
Body *fseg = floor.items[i];
Body *cseg = floor.items[i];
fseg->position = *(Vect *)arlst_get(&e.floor, i);
cseg->position = *(Vect *)arlst_get(&e.ceil, i);
fseg->collision_poly_size = 4;
cseg->collision_poly_size = 4;
fseg->collision_poly = calloc(4, sizeof(Vect));
fseg->collision_shape = calloc(4, sizeof(Vect));
cseg->collision_poly = calloc(4, sizeof(Vect));
cseg->collision_shape = calloc(4, sizeof(Vect));
Vect fthis = fseg->position;
Vect cthis = cseg->position;
Vect fnext = *(Vect *)arlst_get(&e.floor, i);
Vect cnext= *(Vect *)arlst_get(&e.ceil, i);
double frise = fnext.y - fthis.y;
double frun = fnext.x - fthis.y;
double crise = cnext.y - cthis.y;
double crun = cnext.x - cthis.y;
fseg->collision_shape[0].x = 0;
fseg->collision_shape[0].y = 0;
fseg->collision_shape[1].x = frun;
fseg->collision_shape[1].y = frise;
fseg->collision_shape[2].x = frun;
fseg->collision_shape[2].y = frise + FLOOR_THICKNESS;
fseg->collision_shape[3].x = 0;
fseg->collision_shape[3].y = frise + FLOOR_THICKNESS;
cseg->collision_shape[0].x = 0;
cseg->collision_shape[0].y = 0;
cseg->collision_shape[1].x = crun;
cseg->collision_shape[1].y = crise;
cseg->collision_shape[2].x = crun;
cseg->collision_shape[2].y = crise - FLOOR_THICKNESS;
cseg->collision_shape[3].x = 0;
cseg->collision_shape[3].y = crise - FLOOR_THICKNESS;
}
world_thing room_world;
room_world.kind = ROOM_W;
struct room* room = malloc(sizeof(struct room));
room->ceil = ceil;
room->floor = floor;
room_world.room = room;
add_to_world(room_world);
return 0;
}
Wall *get_long_wall(int numNodes, int *nodes) {
Wall wall;
memset(&wall, 0, sizeof(wall));
@ -1043,6 +1135,7 @@ bool unique_world_kind(enum world_thing_kind k) { @@ -1043,6 +1135,7 @@ bool unique_world_kind(enum world_thing_kind k) {
case PLAYER_W:
case FLOOR:
case CEILING:
case ROOM_W:
return true;
default:
return false;
@ -1054,6 +1147,7 @@ void add_to_world(world_thing thing) { @@ -1054,6 +1147,7 @@ void add_to_world(world_thing thing) {
if (world.size == world.capacity) {
logwrite(INFO, "Increased world size.");
world.things = realloc(world.things, sizeof(world_thing) * (world.size*=2));
// man dodgy and unfinished
if (unique_world_kind(thing.kind)) {
}
}
@ -1124,6 +1218,8 @@ void startgame(SDL_Renderer * ren) { @@ -1124,6 +1218,8 @@ void startgame(SDL_Renderer * ren) {
logwrite(INFO, "STARTGAME");
get_input_map();
level = 0;
world = create_world();
SDL_GetRendererOutputSize(ren, &width, &height);
@ -1143,7 +1239,8 @@ void startgame(SDL_Renderer * ren) { @@ -1143,7 +1239,8 @@ void startgame(SDL_Renderer * ren) {
viewport_pos.x = 700;
viewport_pos.y = 0;
get_room();
get_floor_ceiling();
//get_room();
}
double get_abs(double x,double y) {
@ -1206,5 +1303,12 @@ void handle_input_event(SDL_Event event) { @@ -1206,5 +1303,12 @@ void handle_input_event(SDL_Event event) {
}
void step(int interval) {
if (player.physics->position.x > world.uniques_index[ROOM_W]->room
->floor.items[world.uniques_index[ROOM_W]->room->floor.numItems - 1]->position.x) {
next_level();
}
printf("%f %f", player.physics->position.x, 0.9 * world.uniques_index[ROOM_W]->room->floor.items[world.uniques_index[ROOM_W]->room->floor.numItems - 1]->position.x);
advance_things();
}

13
game.h

@ -25,6 +25,7 @@ enum world_thing_kind { @@ -25,6 +25,7 @@ enum world_thing_kind {
PLAYER_W = 0,
FLOOR = 1,
CEILING = 2,
ROOM_W,
STATIC_WALL_W,
PROJECTILE
};
@ -154,12 +155,22 @@ typedef struct { @@ -154,12 +155,22 @@ typedef struct {
int numPolys;
} Floor;
struct physics_collection {
int numItems;
Body **items;
};
typedef struct Projectile {
Body *physics;
void*(*on_collision)(struct Projectile*);
void*(*on_step)(struct Projectile*);
} Projectile;
struct room {
struct physics_collection ceil;
struct physics_collection floor;
};
typedef struct {
enum world_thing_kind kind;
int nid;
@ -170,6 +181,7 @@ typedef struct { @@ -170,6 +181,7 @@ typedef struct {
Wall *wall;
Floor *floor;
Projectile *projectile;
struct room *room;
};
} world_thing;
@ -196,5 +208,6 @@ extern void process_keydown(SDL_Keysym key); @@ -196,5 +208,6 @@ extern void process_keydown(SDL_Keysym key);
extern void process_keyup(SDL_Keysym key);
extern void step(int interval);
extern player_st player;
extern int level;
#endif

Loading…
Cancel
Save