1
1
Fork 0
Browse Source

mouse logic, longer levels, close ends of level

thread-physics
alistair 4 years ago
parent
commit
447bb081fc
  1. 2
      draw.h
  2. 24
      environment.c
  3. 25
      game.c
  4. 228
      types.h

2
draw.h

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
#include "game.h"
#include "environment.h"
#define SHOWCOLLISION 0
#define SHOWCOLLISION 1
#define SHOWFORCES 0
typedef enum {

24
environment.c

@ -170,6 +170,30 @@ struct environment get_scene_at(Vect coordinate, int seed) { @@ -170,6 +170,30 @@ struct environment get_scene_at(Vect coordinate, int seed) {
arlst_add(&e.ceil, z);
}
Vect *v = arlst_get(&e.floor, 0);
Vect v2 = *(Vect *)arlst_get(&e.ceil, 0);
Vect v3 = vect_add(v2, vect_scalar(vect_add(*v, vect_scalar(v2, -1)) , 0.5));
int d[] = {0, E_ROOM_TILES - 1};
int a;
for (int i = 0; i <= 1; i++) {
a = d[i];
v = arlst_get(&e.floor, a);
v->y = v3.x;
v = arlst_get(&e.ceil, a);
v->y = v3.x;
v = arlst_get(&e.bg1, a);
v->y = v3.x;
v = arlst_get(&e.bg2, a);
v->y = v3.x;
}
// join the end and start together
init = true;
return e;

25
game.c

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
#include "game.h"
#define FLOOR_THICKNESS 200
#define MAX_ROPE_GRAB_LEN 800
#define MAX_ROPE_GRAB_LEN 80000
#define BREAKPOINT *(int *)0 = 1;
@ -1404,15 +1404,17 @@ void startgame(SDL_Renderer * ren) { @@ -1404,15 +1404,17 @@ void startgame(SDL_Renderer * ren) {
debug_ren = ren;
level = 2;
level = 0;
world = create_world();
SDL_GetRendererOutputSize(ren, &width, &height);
// player = get_player(200,1300);
player = get_player(50,-100);
get_floor_ceiling();
Vect stpos = *world.uniques_index[ROOM_W]->room->ceil.items[2]->collision_poly;
player = get_player(stpos.x + 20, stpos.y - 60);
world_thing player_world;
player_world.kind = PLAYER_W;
@ -1425,7 +1427,6 @@ void startgame(SDL_Renderer * ren) { @@ -1425,7 +1427,6 @@ void startgame(SDL_Renderer * ren) {
viewport_pos.x = 700;
viewport_pos.y = 0;
get_floor_ceiling();
//get_room();
}
@ -1447,6 +1448,7 @@ void walk_player(int x, int y) { @@ -1447,6 +1448,7 @@ void walk_player(int x, int y) {
void handle_input_event(SDL_Event event) {
SDL_Scancode sc = event.key.keysym.scancode;
static bool mouse_down = false;
switch (event.type) {
case SDL_KEYDOWN:
if (sc == input_map.player_up) {
@ -1474,18 +1476,23 @@ void handle_input_event(SDL_Event event) { @@ -1474,18 +1476,23 @@ void handle_input_event(SDL_Event event) {
}
break;
case SDL_MOUSEBUTTONDOWN:
add_rope(event.button.x, event.button.y);
if (event.button.button == SDL_BUTTON_LEFT)
add_rope(event.button.x, event.button.y);
mouse_down = true;
if (event.button.button == SDL_BUTTON_RIGHT)
pull_rope(900);
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT)
if (event.button.button == SDL_BUTTON_LEFT) {
mouse_down = false;
delete_rope();
if (event.button.button == SDL_BUTTON_RIGHT)
}
if (event.button.button == SDL_BUTTON_RIGHT) {
stop_pull_rope();
break;
if (!mouse_down) {
delete_rope();
}
}
}
}

228
types.h

@ -0,0 +1,228 @@ @@ -0,0 +1,228 @@
#ifndef PTYPES_H
#define PTYPES_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include <time.h>
#include "c-colours/colours.h"
#include "datastructures/datatypes.h"
#include "vect.h"
enum motors {
M_GRAVITY = 0,
M_FRICTION = 1,
M_PLAYER_WALK = 2,
M_WINCH = 3
};
enum world_thing_kind {
PLAYER_W = 0,
FLOOR = 1,
CEILING = 2,
ROOM_W,
STATIC_WALL_W,
PROJECTILE
};
// used to exert a force on an object
typedef struct motorstruct {
double x; // positive is right
double y; // positive is down
double torque; // positive is anticlockwise
double max_velocity; // max motor output velocity
// does not apply force if the velocity in the
// direction of the motor vector is greater than
// max_velocity
struct timespec timeout;// absolute time (in ns, from when the program starts)
// for how long the motor runs before stopping
// automatically
// set to -1 for infinity
bool stop; // turn the motor off or on
void (*update_motor)(struct motorstruct *motor); // function pointer for generating
// the motor's output curve
struct BodyStruct *end_object;
} Motor;
typedef struct {
int x;
int y;
} Point;
/* String structure, used as a sub-element of a Body, compressible, but not
* stretchable. */
struct String {
bool attached;
double max_length;
Vect end_point;
int (*update_end_point)(struct String*); // method to update the end pt
// say if string is attached
// to another object
int (*set_end_point)(struct String*, Vect *); // manually set the end point of
// string
};
typedef struct String String;
typedef struct BodyStruct{
// turn on dynamic physics
// For moving objects.
// eg. on for payer, off for walls
bool dynamics;
// unique identifier
int uid;
bool colliding;
// position in viewport (pixels)
SDL_Point screen_pos;
// SI Unit kinematics
/*------------------*/
Vect position;
Vect next_position; // used for casting collision
Vect vel;
Vect acc;
// properties
double obj_mass; // kgs
double obj_elasticity; // rho
double obj_friction; // between 0 and 1 (fraction of lateral velocity
// that is removed)
//float x_vel;
//float y_vel;
//float x_acc;
//float y_acc;
/*------------------*/
// collisions
Vect *collision_poly;
Vect *collision_shape;
int collision_poly_size;
int collision_shape_size;
void (*updateCollisionPoly)(struct BodyStruct *);
// fields
double glob_friction;
bool glob_gravity; // t/f
// uint32_t last_advance_time;
struct timespec last_advance_time;
// applying forces
int num_motors;
int max_motors;
Motor *motors;
int num_strings;
int max_strings;
String *strings;
} Body;
typedef struct {
bool has_physics;
Body *physics;
int max_walking_speed;
int colliding;
} player_st;
typedef struct {
int numNodes;
SDL_Point *nodes;
Body *physics;
} Wall;
typedef struct {
Vect left;
Vect right;
Body *physics;
} FloorPoly;
typedef struct {
FloorPoly *polys;
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 colour_pallete {
struct colour bg;
struct colour fg1;
struct colour fg2;
struct colour fg3;
};
struct environment {
int level;
Vect position;
ArrayList ceil; // doubles
ArrayList floor; // doubles
ArrayList bg1; // doubles
ArrayList bg2; // doubles
struct colour_pallete colours;
struct physics_collection physics;
};
struct room {
struct environment env;
struct physics_collection ceil;
struct physics_collection floor;
};
struct world_thing {
enum world_thing_kind kind;
int nid;
bool collisions;
bool physics;
// free function for the below type
void (*free)(void *);
union {
player_st *player;
Wall *wall;
Floor *floor;
Projectile *projectile;
struct room *room;
};
};
typedef struct world_thing world_thing;
struct world {
world_thing (*get)(int i);
ArrayList items;
int modified;
world_thing** uniques_index;
};
typedef struct world GlobWorld;
// environment
enum {
E_ROOM_WIDTH = 100000,
E_ROOM_RES = 500,
E_ROOM_TILES = E_ROOM_WIDTH / E_ROOM_RES,
};
#endif
Loading…
Cancel
Save