1
1
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

246 lines
5.1 KiB

#ifndef PTYPES_H
#define PTYPES_H
#include <SDL_mutex.h>
#include <SDL_audio.h>
#include <SDL_scancode.h>
#include <SDL_mixer.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
#include <math.h>
#include "colours.h"
#include "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;
int was_colliding;
// position in viewport (pixels)
SDL_Point screen_pos;
SDL_mutex * lock;
// 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_WIDTH = 10000,
E_ROOM_RES = 500,
E_ROOM_TILES = E_ROOM_WIDTH / E_ROOM_RES,
};
struct button {
char *name;
bool state;
bool held;
int x, y, w, h;
};
extern struct button mute_button;
#endif