there is now a player character and the world renders using a cammera

This commit is contained in:
2026-04-09 13:11:55 -07:00
parent 09c310cb4a
commit af639e62c3
47 changed files with 1737 additions and 83 deletions

View File

@@ -0,0 +1,11 @@
target_sources(${PROJECT_NAME} PRIVATE
cammera.c
entity.c
entity_handler.c
cells.c
world.c
player/player_packet.c
)
add_subdirectory(entities)

View File

@@ -0,0 +1,82 @@
#include "cammera.h"
#include "entity.h"
#include <tart.h>
#include <stdlib.h>
#include <stdio.h>
#define jump tart_move_cursor
int CammeraInit(cammera* c, int sx, int sy, int px, int py, world* w, tart_window* tw) {
c->w = w;
c->tw = tw;
c->pos_x = px;
c->pos_y = py;
c->size_x = sx;
c->size_y = sy;
c->tileCount = c->size_x * c->size_y;
//c->frame = (tile*)malloc(c->tileCount * sizeof(tile));
return 0;
}
int CammeraResize(cammera* c, int sx, int sy) {
tart_window_resize(c->tw, (tart_vec2){sx,sy});
c->size_x = sx ;
c->size_y = sy;
//free(c->frame);
c->tileCount = c->size_x * c->size_y;
//c->frame = (tile*)malloc(c->tileCount * sizeof(tile));
return 0;
}
int CammeraRender(cammera*c) {
jump(c->tw, (tart_vec2){0,0});
for(int i = 0; i < c->size_y; i++) {
for(int j = 0; j < c->size_x - 1; j++) {
if(c->pos_y+i < c->w->size_y-1 && c->pos_y+i >= 0 && c->pos_x+j < c->w->size_x-1 && c->pos_x+j >= 0){
c->tw->palette[2].background.r = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].cell->tile.background.r;
c->tw->palette[2].background.g = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].cell->tile.background.g;
c->tw->palette[2].background.b = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].cell->tile.background.b;
c->tw->palette[2].forground.r = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].cell->tile.forground.r;
c->tw->palette[2].forground.g = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].cell->tile.forground.g;
c->tw->palette[2].forground.b = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].cell->tile.forground.b;
tart_jump(c->tw, (tart_vec2){j,i});
for(int k = 0; k < 1; k++) {
if(c->w->wells[at((c->pos_x+j) ,(c->pos_y+i),c->w)].entityIds[0] != 0000) {
if(c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].entityIds[k]->tile.background.r != 0 &&
c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].entityIds[k]->tile.background.r != 0 &&
c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].entityIds[k]->tile.background.r != 0) {
c->tw->palette[2].background.r = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].entityIds[k]->tile.background.r;
c->tw->palette[2].background.g = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].entityIds[k]->tile.background.g;
c->tw->palette[2].background.b = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].entityIds[k]->tile.background.b;
}
c->tw->palette[2].forground.r = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].entityIds[k]->tile.forground.r;
c->tw->palette[2].forground.g = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].entityIds[k]->tile.forground.g;
c->tw->palette[2].forground.b = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].entityIds[k]->tile.forground.b;
tart_insert_cell(c->tw, 2, c->w->wells[at((c->pos_x+j) ,(c->pos_y+i),c->w)]
.entityIds[k]->tile.simble);
printf("found entity %c\n", c->w->wells[at((c->pos_x+j) ,(c->pos_y+i),c->w)]
.entityIds[k]->tile.simble);
}
}
tart_insert_cell(c->tw, 2, c->w->wells[at((c->pos_x+j) ,(c->pos_y+i),c->w)]
.cell->tile.simble);
}
}
}
return 0;
}
void CammeraFree(cammera *c) {
//free(c->frame);
}

View File

@@ -0,0 +1,27 @@
#ifndef CAMMERA_H
#define CAMMERA_H
#include "tile.h"
#include "world.h"
#include <tart.h>
typedef struct {
int pos_x;
int pos_y;
int size_x;
int size_y;
int tileCount;
char x;
tile* frame;
world* w;
tart_window* tw;
} cammera;
int CammeraInit(cammera* c, int sx, int sy, int px, int py, world* w, tart_window* tw);
int CammeraRender(cammera*c);
void CammeraFree(cammera* c);
#endif // !CAMMERA_H

131
source/game_source/cells.c Normal file
View File

@@ -0,0 +1,131 @@
#include "cells.h"
#include <string.h>
static cell c_wall = {
.id = 1,
.flags = CELL_FLAG_BLOCKING,
.tile = {
.background = {0x00,0x00,0x00},
.forground = {0xee,0xee,0xee},
.simble = L'#'
},
.func = NULL,
};
static cell c_long_grass = {
.id = 7,
.flags = CELL_FLAG_FLAMABLE,
.tile = {
.background = {0x0f, 0x33, 0x0f},
.forground = {0x40, 0xff, 0x40},
.simble = '"',
},
.func = NULL,
};
static cell c_short_grass = {
.id = 8,
.flags = CELL_FLAG_FLAMABLE,
.tile = {
.background = {0x0f, 0x33, 0x0f},
.forground = {0x40, 0xff, 0x40},
.simble = '\'',
},
.func = NULL,
};
static cell c_grass = {
.id = 9,
.flags = CELL_FLAG_FLAMABLE,
.tile = {
.background = {0x0f, 0x33, 0x0f},
.forground = {0x40, 0xff, 0x40},
.simble = ' ',
},
.func = NULL,
};
static cell c_floar = {
.id = 2,
.flags = CELL_FLAG_FLORE,
.tile = {
.background = {0x11,0x11,0x11},
.forground = {0xee,0xee,0xee},
.simble = '.'
},
.func = NULL,
};
static cell c_hard_wall = {
.id = 3,
.flags = CELL_FLAG_DAMAGEABLE | CELL_FLAG_BLOCKING,
.tile = {
.background = {0xcc,0xcc,0xcc},
.forground = {0xee,0xee,0xee},
.simble = '#'
},
.func = NULL,
};
static cell c_soft_wall = {
.id = 4,
.flags = CELL_FLAG_BLOCKING | CELL_FLAG_DAMAGEABLE,
.tile = {
.background = {0xcc,0xcc,0xcc},
.forground = {0xee,0xee,0xee},
.simble = L'#'
},
.func = NULL,
};
static cell c_water = {
.id = 5,
.flags = CELL_FLAG_FLORE | CELL_FLAG_SLIPPERY | CELL_FLAG_UNEVEN,
.tile = {
.background = {0,0,0},
.forground = {0xff,0xff,0xff},
.simble = '~',
},
.func = NULL
};
void f_growth(int x, int y, void* data) {
// every tick spread to the next availbe flore / grass.
}
static cell c_growth = {
.id = 6,
.tile = {
.background = {0x34,0x0c,0x40},
.forground = {0xb0,0x25,0xda},
.simble = '*',
},
.func = f_growth
};
int UpdateCells(cell* cell, int x, int y, void* data) {
if(cell->func != NULL) {
cell->func(x,y,0);
}
return 0;
}
cell* SetCell(cell_id id) {
switch (id) {
case 1:
return &c_wall;
case 2:
return &c_floar;
case 3:
return &c_hard_wall;
case 4:
return &c_soft_wall;
case 5:
return &c_water;
case 6:
return &c_growth;
case 7:
return &c_long_grass;
case 8:
return &c_short_grass;
case 9:
return &c_grass;
}
return 0;
}

View File

@@ -0,0 +1,50 @@
#ifndef CELLS_H
#define CELLS_H
#define CELL_FLAG_BLOCKING 0b00000000000000000000000000000001
#define CELL_FLAG_PARTIAL_COVER 0b00000000000000000000000000000010
#define CELL_FLAG_DAMAGEABLE 0b00000000000000000000000000000100
#define CELL_FLAG_SLIPPERY 0b00000000000000000000000000001000
#define CELL_FLAG_UNEVEN 0b00000000000000000000000000010000
#define CELL_FLAG_FLORE 0b00000000000000000000000000100000
#define CELL_FLAG_STARE 0b00000000000000000000000001000000
#define CELL_FLAG_MOVEABLE 0b00000000000000000000000010000000
#define CELL_FLAG_EXPLODING 0b00000000000000000000000100000000
#define CELL_FLAG_FLAMABLE 0b00000000000000000000001000000000
#define CELL_FLAG_RADIO_ACTIVE 0b00000000000000000000010000000000
//#define CELL_FLAG_ 0b00000000000000000000100000000000
//#define CELL_FLAG_ 0b00000000000000000001000000000000
//#define CELL_FLAG_ 0b00000000000000000010000000000000
//#define CELL_FLAG_ 0b00000000000000000100000000000000
//#define CELL_FLAG_ 0b00000000000000001000000000000000
//#define CELL_FLAG_ 0b00000000000000010000000000000000
//#define CELL_FLAG_ 0b00000000000000100000000000000000
//#define CELL_FLAG_ 0b00000000000001000000000000000000
//#define CELL_FLAG_ 0b00000000000010000000000000000000
//#define CELL_FLAG_ 0b00000000000100000000000000000000
//#define CELL_FLAG_ 0b00000000001000000000000000000000
//#define CELL_FLAG_ 0b00000000010000000000000000000000
//#define CELL_FLAG_ 0b00000000100000000000000000000000
//#define CELL_FLAG_ 0b00000001000000000000000000000000
//#define CELL_FLAG_ 0b00000010000000000000000000000000
//#define CELL_FLAG_ 0b00000100000000000000000000000000
//#define CELL_FLAG_ 0b00001000000000000000000000000000
//#define CELL_FLAG_ 0b00010000000000000000000000000000
//#define CELL_FLAG_ 0b00100000000000000000000000000000
//#define CELL_FLAG_ 0b01000000000000000000000000000000
//#define CELL_FLAG_ 0b10000000000000000000000000000000
#include "tile.h"
typedef short cell_id;
typedef struct {
cell_id id;
unsigned int flags;
tile tile;
void(*func)(int x, int y, void* data);
} cell;
int UpdateCells(cell* cell, int x, int y, void* data);
cell* SetCell(cell_id id);
#endif // !CELLS_H

View File

@@ -0,0 +1,5 @@
target_sources(${PROJECT_NAME} PRIVATE
Player.c
Centery.c
Gun.c
)

View File

@@ -1,15 +1,129 @@
#include "Centery.h"
#include "entity_utils.h"
#include <stdlib.h>
#include <stdio.h>
int CreateCentery(int tear, int classification) {
int CenteryInit(void* self) {
entity* ent = self;
centery* cen = ent->data;
cen->direction = 0;
return 0;
}
int ServerUpdate(void * ent) {
entity* e = (entity*)ent;
centery* cen = (centery*)e->userdata;
int CenteryUpdate(int arg, void* self) {
entity* e = self;
world* w = e->world;
centery* cen = e->data;
vec2 moveTo = {0,0};
int goodMove;
//do{
// goodMove = EntityMove(e,(vec2){e->position.x+ 0, e-> position.y + 1});
// if(goodMove == 1) {
// switch (cen->direction) {
// case 0:
// moveTo = (vec2){e->position.x+ 0, e-> position.y + 1};
// printf("moveing S\n");
// break;
// case 1:
// moveTo = (vec2){e->position.x+ 1, e-> position.y + 1};
// printf("moveing SE\n");
// break;
// case 2:
// moveTo = (vec2){e->position.x+ 1, e-> position.y + 0};
// printf("moveing E\n");
// break;
// case 3:
// moveTo = (vec2){e->position.x+ 1, e-> position.y + -1};
// printf("moveing NE\n");
// break;
// case 4:
// moveTo = (vec2){e->position.x+ 0, e-> position.y + -1};
// printf("moveing N\n");
// break;
// case 6:
// moveTo = (vec2){e->position.x+ -1, e->position.y+ -1};
// printf("moveing NW\n");
// break;
// case 7:
// moveTo = (vec2){e->position.x+ -1, e->position.y + 0};
// printf("moveing W\n");
// break;
// case 8:
// moveTo = (vec2){e->position.x+ -1, e->position.y + 1};
// printf("moveing SW\n");
// break;
// }
// goodMove = EntityMove(e,moveTo);
// if(cen->direction > 8) {
// cen->direction = 0;
// }else {
// cen->direction ++;
// }
//
// }
//}while(goodMove == 1);
printf("name of entity %s\n",e->name);
return 0;
}
int CenteryFree(void* self) {
entity* ent = self;
centery* cen = ent->data;
free(cen);
free(ent);
return 0;
}
entity* CreateCentery(int tear, int classification, world* w) {
entity* ent = (entity*)malloc(sizeof(entity));
centery* cen = (centery*)malloc(sizeof(centery));
*cen = (centery){
.weapon = 1,
.classification = 1,
.tear = 3,
.alert = 0,
.mode = 0,
.health = 30,
.ac = 3,
.str = 3,
.wis = 3,
.dex = 3,
};
*ent = (entity){
.id = CENTERY_ID,
.name = "Centery",
.description = "Your Basic Bad giy",
.tile = {
.simble = 'C',
.background = {0,0,0},
.forground = {0x3f,0x5f,0x10},
},
.position = (vec2){5,5},
.data = cen,
.world = w,
.callback = {
.init = CenteryInit,
.update = CenteryUpdate,
.free = CenteryFree,
},
.cleanup = 0,
};
return ent;
}
int ServerUpdate(void * ent) {
//entity* e = (entity*)ent;
//centery* cen = (centery*)e->userdata;
if(
return 0;
}
@@ -17,6 +131,7 @@ int ClientUpdate(int action, void * centery) {
return 0;
}
int Init(void * ent){
return 0;
}

View File

@@ -1,14 +1,16 @@
#ifndef CENTERY_H
#define CENTERY_H
#define CENTERY_ID 1
#include "../entity.h"
#include "../world.h"
typedef struct {
entity* target;
int weapon;
int classification;
int tear;
int alert;
int mode;
int direction;
int health;
int ac;
@@ -17,7 +19,7 @@ typedef struct {
int dex;
} centery;
int CreateCentery(int tear, int classification);
entity* CreateCentery(int tear, int classification,world* w);
int ServerUpdate(void*);
int ClientUpdate(int, void*);

View File

@@ -0,0 +1,99 @@
#include <stdio.h>
#include <stdlib.h>
#include "Gun.h"
int GunUpdate(int action, void* data) {
printf("Running Gun\n");
entity* e = (entity*)data;
vec2 bullet_pos = e->position;
Gun* g = (Gun*)e->data;
world* w = (world*)e->world;
if(g->inpact == 0) {
switch (g->direction) {
case SHOOT_N: bullet_pos.y -= 1; break;
case SHOOT_E: bullet_pos.x += 1; break;
case SHOOT_S: bullet_pos.y += 1; break;
case SHOOT_W: bullet_pos.x -= 1; break;
}
}
int entity_colied = EntityMove(e, bullet_pos);
if((entity_colied) && g->inpact == 0) {
if(w->wells[at(bullet_pos.x, bullet_pos.y, w)].cell->id == 1) {
w->wells[at(bullet_pos.x, bullet_pos.y, w)].cell = SetCell(2);
}
g->inpact ++;
}
if(g->inpact > 0) {
e->tile.simble = '*';
e->tile.background = (color){0xff,0x0a,0x0a};
e->tile.forground = (color){0xff,0xff,0xff};
g->inpact ++;
}
if(g->inpact > 25) {
e->tile.simble = 'o';
e->tile.background = (color){0xff,0x20,0x20};
e->tile.forground = (color){0xff,0x3f,0x3f};
}
if(g->inpact > 30) {
e->tile.simble = 'O';
e->tile.background = (color){0x01,0x01,0x01};
e->tile.forground = (color){0xff,0x0a,0x0a};
}
if(g->inpact > 40) {
e->tile.simble = '#';
e->tile.background = (color){0x01,0x01,0x01};
e->tile.forground = (color){0xff,0x0a,0x0a};
}
if(g->inpact > 75) {
printf("tagged for cleanup\n");
e->tile.simble = 'R';
e->cleanup = 1;
}
return 0;
}
entity* CreateGun(int direction, int bullet, world* w) {
entity* ent = malloc(sizeof(entity));
Gun* g= malloc(sizeof(Gun));
*g = (Gun){
.direction = direction,
.projectileId = bullet,
.fuse = 10,
.currentFuse = 0,
.inpact = 0,
};
*ent = (entity){
.id = 50,
.name = "Gun",
.description = "description",
.tile = {
.simble = '-',
.background = {0,0,0},
.forground = {0xff,0,0},
},
.position = (vec2){0,0},
.data = g,
.world = w,
.callback = {
.init = GunInit,
.update = GunUpdate,
.free = GunFree,
},
.cleanup = 0,
};
return ent;
}
int GunInit(void* ent) {return 0;}
int GunFree(void* ent) {return 0;}

View File

@@ -0,0 +1,26 @@
#ifndef ENTITY_Gun_H
#define ENTITY_Gun_H
#include "../entity.h"
#include "../world.h"
#define SHOOT_N 1
#define SHOOT_E 2
#define SHOOT_S 3
#define SHOOT_W 4
typedef struct {
int projectileId;
int direction;
int fuse;
int currentFuse;
int inpact;
} Gun;
entity* CreateGun(int direction,int bullet, world* w);
int GunUpdate(int, void*);
int GunInit(void*);
int GunFree(void*);
#endif // ENTITY_Gun_H

View File

@@ -0,0 +1,146 @@
#include <stdlib.h>
#include "Player.h"
#include "Gun.h"
#include "Centery.h"
#include "../../client_source/client.h"
void Shoot(entity* e,int direction) {
if(direction == SHOOT_N) {
entity* g = CreateGun(SHOOT_N, 0, e->world);
g->position = (vec2){e->position.x, e->position.y-1};
world* w = e->world;
AddEntity(w, g);
printf("Shoot N\n");
}
if(direction == SHOOT_E) {
entity* g = CreateGun(SHOOT_E, 0, e->world);
world* w = e->world;
g->position = (vec2){e->position.x + 1, e->position.y};
AddEntity(w, g);
printf("Shoot E\n");
}
if(direction == SHOOT_S) {
entity* g = CreateGun(SHOOT_S, 0, e->world);
world* w = e->world;
g->position = (vec2){e->position.x, e->position.y+1};
AddEntity(w, g);
printf("Shoot S\n");
}
if(direction == SHOOT_W) {
entity* g = CreateGun(SHOOT_W, 0, e->world);
world* w = e->world;
g->position = (vec2){e->position.x - 1, e->position.y};
AddEntity(w, g);
printf("Shoot W\n");
}
}
int PlayerUpdate(int action, void* data) {
entity* e = (entity*)data;
player* p= (player*)e->data;
world* w = e->world;
ssh_terminal_data* td = &p->cli->term;
vec2 mv = {e->position.x, e->position.y};
vec2 cammera_pos = {
p->cli->cam.pos_x + (p->cli->cam.size_x/2),
p->cli->cam.pos_y + (p->cli->cam.size_y/2),
};
int cammera_fallow_readious = 14;
printf("input buffer for player %s\n", td->inputBuffer);
if(!strcmp(td->inputBuffer, "k")) {mv.y -= 1;}
if(!strcmp(td->inputBuffer, "j")) {mv.y += 1;}
if(!strcmp(td->inputBuffer, "h")) {mv.x -= 1;}
if(!strcmp(td->inputBuffer, "l")) {mv.x += 1;}
int entity_can_move = EntityMove(e, mv);
if(!strcmp(td->inputBuffer, "w")) {Shoot(e,SHOOT_N);}
if(!strcmp(td->inputBuffer, "d")) {Shoot(e,SHOOT_E);}
if(!strcmp(td->inputBuffer, "s")) {Shoot(e,SHOOT_S);}
if(!strcmp(td->inputBuffer, "a")) {Shoot(e,SHOOT_W);}
printf("Cammera Position %d,%d\n", cammera_pos.x, cammera_pos.y);
if((e->position.x < cammera_pos.x - cammera_fallow_readious )){
p->cli->cam.pos_x += - 1;
}
if ((e->position.x > cammera_pos.x + cammera_fallow_readious)) {
p->cli->cam.pos_x += 1;
}
if((e->position.y < cammera_pos.y - (cammera_fallow_readious / 2) )){
p->cli->cam.pos_y += - 1;
}
if ((e->position.y > cammera_pos.y + (cammera_fallow_readious / 2))) {
p->cli->cam.pos_y += 1;
}
if(!entity_can_move) {
w->wells[at(mv.x, mv.y, w)].cell = SetCell(6);
}
printf("is this a good move? %d\n", EntityMove(e, mv));
memset(td->inputBuffer,0,32);
return 0;
}
entity* CreatePlayer(const char* username, client* cli,world* w) {
entity* ent = malloc(sizeof(entity));
player* p= malloc(sizeof(player));
*p = (player){
.holding = 0,
.class = 0,
.health = 0,
.ac = 0,
.str = 0,
.wis = 0,
.dex = 0,
.cli = cli,
};
*ent = (entity){
.id = 30,
.name = "Testing",
.description = "This is a player",
.tile = {
.simble = '@',
.background = {0,0,0},
.forground = {0x5f,0x3f,0x10},
},
.position = (vec2){5,5},
.data = p,
.world = w,
.callback = {
.init = PlayerInit,
.update = PlayerUpdate,
.free = PlayerFree,
},
.cleanup = 0,
};
p->cli->cam.pos_x = ent->position.x - (p->cli->cam.size_x/2);
p->cli->cam.pos_y = ent->position.y - (p->cli->cam.size_y/2);
return ent;
}
int PlayerServerUpdate(void*);
int PlayerClientUpdate(int, void*);
int PlayerInit(void* ent) {return 0;}
int PlayerFree(void* ent) {return 0;}

View File

@@ -0,0 +1,30 @@
#ifndef PLAYER_H
#define PLAYER_H
#include "../entity.h"
#include "../../client_source/client.h"
#include "../world.h"
typedef struct {
int holding;
int class;
int health;
int ac;
int str;
int wis;
int dex;
client* cli;
int invintory[30];
} player;
entity* CreatePlayer(const char* username, client* cli ,world* w);
int PlayerServerUpdate(void*);
int PlayerClientUpdate(int, void*);
int PlayerInit(void*);
int PlayerFree(void*);
#endif

View File

@@ -0,0 +1,42 @@
#include <stdlib.h>
#include "Template.h"
#include "../../client_source/client.h"
int templateUpdate(int action, void* data) {
return 0;
}
entity* Createtemplate() {
entity* ent = malloc(sizeof(entity));
template* p= malloc(sizeof(template));
*t = (template){
};
*ent = (entity){
.id = ID,
.name = "template",
.description = "description",
.tile = {
.simble = 'T',
.background = {0,0,0},
.forground = {0,0,0},
},
.position = (vec2){0,0},
.data = t,
.world = w,
.callback = {
.init = PlayerInit,
.update = PlayerUpdate,
.free = PlayerFree,
},
};
return ent;
}
int templateInit(void* ent) {return 0;}
int templateFree(void* ent) {return 0;}

View File

@@ -0,0 +1,16 @@
#ifndef ENTITY_template_H
#define ENTITY_template_H
#include "../entity.h"
#include "../world.h"
typedef struct {
} template;
entity* Createtemplate(initvar);
int templateUpdate(int, void*);
int templateInit(void*);
int templateFree(void*);
#endif // ENTITY_template_H

View File

@@ -0,0 +1,21 @@
#include "entity.h"
#include "cells.h"
#include "vector.h"
#include "world.h"
int EntityMove(entity* e, vec2 pos) {
world* world = e->world;
if(world->wells[at(pos.x,pos.y, world)].cell->flags & (CELL_FLAG_BLOCKING)){
return 1;
}
if(world->wells[at(pos.x,pos.y, world)].entityIds[0] != NULL){
return 2;
}
world->wells[at(e->position.x,e->position.y, world)].entityIds[0] = 0;
world->wells[at(pos.x,pos.y, world)].entityIds[0] = e;
e->position = pos;
return 0;
}

View File

@@ -3,12 +3,13 @@
#include "vector.h"
#include "tile.h"
typedef short entity_id;
typedef int(*entity_func)(void*);
typedef int(*entity_update_func)(int, void*);
// Entity network transaction.
typedef struct {
int id;
entity_id id;
int action;
int userdataSize;
void* userdata;
@@ -23,19 +24,22 @@ typedef struct {
// Entity Struct
typedef struct {
int id;
entity_id id;
const char* name;
const char* description;
tile tile;
vec2 position;
void* userdata;
void* data;
void* world;
entity_callbacks callback;
entity_transaction ta;
unsigned char cleanup;
} entity;
entity* CreateEntity(const char* name, const char* description);
int EntityAddCallbacks(entity* e, entity_callbacks cb);
int EntitySetUserdat(entity* e, void* userdata);
int EntityMove(entity* e, vec2 pos);
// Gets
const char* GetEntityDescription(entity* e);

View File

@@ -0,0 +1,30 @@
#include "entity_handler.h"
#include "world.h"
#include <bits/time.h>
#include <time.h>
#include <stdio.h>
void EntityListInit(entity_list* list) {
list->count = 0;
for(int i = 0; i < 256; i++) {
list->entities[i] = NULL;
}
clock_gettime(CLOCK_MONOTONIC, &list->start);
}
int EntityListUpdate(entity_list* list) {
clock_gettime(CLOCK_MONOTONIC, &list->current);
double elapsed = (list->current.tv_sec - list->start.tv_sec);
elapsed += (list->current.tv_nsec - list->start.tv_nsec) / 1000000000.0;
if(elapsed > 0.125/4) {
for(int i = 0; i < 256; i++) {
if(list->entities[i] != NULL) {
printf("running index %d\n", i);
list->entities[i]->callback.update(0,list->entities[i]);
}
}
clock_gettime(CLOCK_MONOTONIC, &list->start);
}
return 0;
}

View File

@@ -4,10 +4,13 @@
#define ENTITY_LIST_ACTION_ADD
#define ENTITY_LIST_ACTION_REMOVE
#include "entity.h"
#include <time.h>
typedef struct {
int count;
entity* entities[256];
struct timespec start;
struct timespec current;
} entity_list;
typedef struct {
@@ -17,13 +20,13 @@ typedef struct {
}entity_list_transaction;
void EntityListInit(entity_list* list);
int EntityListAddEntity(entity* ent);
int EntityListRemoveEntity(entity* ent);
//int EntityListAddEntity(entity* ent);
//int EntityListRemoveEntity(entity* ent);
//
//int EntityListTransactionSet(entity_list_transaction* ta, int action, void* data);
//int EntityListTransactionSend(entity_list* list, entity_list_transaction* ta);
int EntityListTransactionSet(entity_list_transaction* ta, int action, void* data);
int EntityListTransactionSend(entity_list* list, entity_list_transaction* ta);
int EntityListUpdate();
int EntityListUpdate(entity_list* list);
#endif

View File

@@ -0,0 +1,13 @@
#include "player/player_packet.h"
#ifndef GAME_SERVER_H
typedef struct {
const char* ip;
int port;
unsigned int sessionId;
}game_server_config;
int ConnectToGameServer(game_server_config* config);
#define GAME_SERVER_H
#endif

View File

@@ -0,0 +1,8 @@
#include "player_packet.h"
int SendPlayerPacket(player_packet *pakcet, game_server *config) {
return 0;
}

View File

@@ -0,0 +1,25 @@
#ifndef PLAYER_PACKET_H
#define PLAYER_PACKET_H
#include "../../server_source/game_client.h"
#define MOVE_UP 1
#define MOVE_DOWN 2
#define MOVE_LEFT 3
#define MOVE_RIGHT 4
#define ATTACK_MELEE 11
#define ATTACK_WEAPON_1 12
#define ATTACK_WEAPON_2 13
#define ATTACK_WEAPON_3 14
typedef struct {
}player_packet;
int SendPlayerPacket(player_packet* pakcet, game_server* config);
#endif

View File

@@ -1,8 +1,9 @@
#ifndef TILE_H
#define TILE_H
#include "color.h"
#include "tart.h"
typedef struct {
char simble;
t_char simble;
color forground;
color background;
} tile;

186
source/game_source/world.c Normal file
View File

@@ -0,0 +1,186 @@
#include "world.h"
#include "cells.h"
#include <stdlib.h>
#include <string.h>
#include "./entities/Centery.h"
#include "./entities/Player.h"
#include "entity_handler.h"
#include "vector.h"
#include <stdio.h>
#define iToX(i) i%w->size_x
#define iToY(i) i/w->size_x
int UpdateWorld(world* w) {
for(int i = 0; i < 255; i++ ) {
if(w->ent_list->entities[i] != NULL) {
if(w->ent_list->entities[i]->cleanup) {
printf("CleanningUp Entity %d\n", i);
RemoveEntity(w, w->ent_list->entities[i]);
w->ent_list->entities[i] = NULL;
}
}
}
EntityListUpdate(w->ent_list);
return 0;
}
int InitWorld(world* w) {
EntityListInit(w->ent_list);
return 0;
}
int CreateWorld(world* w, long seed) {
int grid_size = w->size_x * w->size_y * sizeof(well);
w->world_size = grid_size;
w->seed = seed;
w->wells = (well*)malloc(grid_size * sizeof(well));
w->prevWells = (well*)malloc(grid_size * sizeof(well));
if(!w->wells || !w->prevWells) {
return -1;
}
entity_list* el = malloc(sizeof(entity_list));
w->ent_list = el;
memset(w->wells, 0, grid_size * sizeof(well));
memset(w->prevWells, 0, grid_size * sizeof(well));
return grid_size;
}
int AddEntity(world* w, entity* ent) {
for(int i = 0; i < 255; i++) {
if(w->ent_list->entities[i] == NULL) {
w->ent_list->entities[i] = ent;
break;
}
}
w->wells[at(ent->position.x, ent->position.y,w)].entityIds[0] = ent;
w->ent_list->count++;
return 0;
}
int RemoveEntity(world *w, entity *ent) {
printf("removeing entity\n");
w->wells[at(ent->position.x, ent->position.y,w)].entityIds[0] = NULL;
ent->callback.free(ent);
free(ent->data);
free(ent);
w->ent_list->count--;
return 0;
}
int GenerateWorld(world *w) {
for(int i = 0; i <= w->size_y; i++) {
for(int j = 0; j <= w->size_x; j++) {
if(i == 0 || i == w->size_y - 2 || j == 0 || j == w->size_x - 2) {
w->wells[at(j, i, w)].cell = SetCell(1);
}else {
int grass_type = (rand()%3) + 7;
w->wells[at(j, i, w)].cell = SetCell(grass_type);
w->wells[at(j, i, w)].entityIds[0] = 0;
w->wells[at(j, i, w)].entityIds[1] = 0;
w->wells[at(j, i, w)].entityIds[2] = 0;
w->wells[at(j, i, w)].entityIds[3] = 0;
w->wells[at(j, i, w)].entityIds[4] = 0;
w->wells[at(j, i, w)].entityIds[5] = 0;
w->wells[at(j, i, w)].entityIds[6] = 0;
w->wells[at(j, i, w)].entityIds[7] = 0;
}
}
}
for(int i = 0; i < w->ent_list->count; i++) {
entity* ent = w->ent_list->entities[i];
w->wells[at(ent->position.x + 1, ent->position.y + 1, w)].entityIds[0] = ent;
}
vec2 building_position = {50,50};
vec2 building_size = {100,100};
for(int i = 0; i <= building_size.y; i++) {
for(int j = 0; j <= building_size.x; j++) {
w->wells[at(building_position.x + j, building_position.y + i,w)].cell = SetCell(1);
}
}
typedef struct {
vec2 size,pos;
int type;
} block;
block roomList[100];
block room;
int sizeIteration = 0;
int continueIdx = 0;
for(int i = 0; i < 100; i++) {
//room.size = (vec2){(rand()%9)+1, (rand()%9)+1};
room.size = (vec2){15, 15};
room.pos = (vec2){
(rand()%(building_size.x - room.size.x) -1),
(rand()%(building_size.y - room.size.y) -1)
};
if(i > 0) {
for(int j = 0; j < i; j++) {
printf("Room[%d] %d,%d, Chalange Room[%d] %d,%d ",i, room.pos.x, room.pos.y, j, roomList[j].pos.x, roomList[j].pos.y);
if(!((room.pos.x + room.size.x < -1 + roomList[j].pos.x || room.pos.x > 1 + roomList[j].pos.x + roomList[j].size.x)|| (room.pos.y + room.size.y < -1 + roomList[j].pos.y
|| room.pos.y > 1 + roomList[j].pos.y + roomList[j].size.y))) {
room.pos = (vec2){
(rand()%(building_size.x - room.size.x)),
(rand()%(building_size.y - room.size.y))
};
if(sizeIteration >= 5) {
printf("iterate Size ");
if(room.size.x > 5 && (rand()%2 - 1)) {
room.size.x--;
}else if(room.size.y > 5) {
room.size.y--;
}
sizeIteration = 0;
continueIdx++;
}
if(continueIdx > 100) {
return -1;
}
sizeIteration++;
j = -1;
printf("BAD!\n");
continue;
}
printf("Good!\n");
}
}
continueIdx = 0;
roomList[i] = room;
for(int y = 0; y <= room.size.y; y++) {
for(int x = 0; x <= room.size.x; x++) {
if(room.pos.x+x > 0 && room.pos.x+x < w->size_x && room.pos.y+y > 0 && room.pos.y+y < w->size_y){
if(y == 0 || y == room.size.y || x == 0 || x == room.size.x ) {
w->wells[at(
building_position.x + room.pos.x + x,
building_position.y + room.pos.y + y,
w)].cell = SetCell(2);
}else {
w->wells[at(
building_position.x + room.pos.x + x,
building_position.y + room.pos.y + y,
w)].cell = SetCell(2);
}
}
}
}
}
return 0;
}

View File

@@ -0,0 +1,32 @@
#ifndef WORLD_H
#define WORLD_H
#define at(X,Y,w) ((Y) * w->size_x ) + (X)
#include "cells.h"
#include "entity.h"
#include "entity_handler.h"
typedef struct {
cell* cell;
entity* entityIds[1];
} well;
typedef struct {
int size_x;
int size_y;
int player_spawn_x;
int player_spawn_y;
long seed;
int world_size;
well *wells;
well *prevWells;
entity_list* ent_list;
} world;
int UpdateWorld(world*);
int InitWorld(world*);
int CreateWorld(world*, long seed);
int AddEntity(world* w, entity* ent);
int RemoveEntity(world* w, entity* ent);
int GenerateWorld(world* w);
#endif