adding multiplayer
This commit is contained in:
169
source/game_source/entities/Player_c.c
Normal file
169
source/game_source/entities/Player_c.c
Normal file
@@ -0,0 +1,169 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "Player_c.h"
|
||||
#include "Gun.h"
|
||||
#include "../../client_source/client.h"
|
||||
#include "../../server_source/transactions.h"
|
||||
|
||||
typedef struct {
|
||||
player_data pd;
|
||||
client* cli;
|
||||
}player_c;
|
||||
|
||||
#define PlayerMove 1
|
||||
|
||||
|
||||
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 PlayerUpdateC(int action, void* data) {
|
||||
entity* e = (entity*)data;
|
||||
player_c* p= (player_c*)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;
|
||||
|
||||
char* ib = td->inputBuffer;
|
||||
int ibsize = strlen(ib);
|
||||
|
||||
while(ib < ib+ibsize) {
|
||||
|
||||
if(!strcmp(ib, "\e[A")) {mv.y -= 1; ib += 3; continue;}
|
||||
if(!strcmp(ib, "\e[B")) {mv.y += 1; ib += 3; continue;}
|
||||
if(!strcmp(ib, "\e[D")) {mv.x -= 1; ib += 3; continue;}
|
||||
if(!strcmp(ib, "\e[C")) {mv.x += 1; ib += 3; continue;}
|
||||
|
||||
user_transaction st_move;
|
||||
memset(&st_move, 0, sizeof(st_move));
|
||||
|
||||
st_move = (user_transaction){
|
||||
.userArg = &mv,
|
||||
.action = PlayerMove,
|
||||
.id = SERVER_T_USER_FLAG,
|
||||
.argsize = sizeof(mv),
|
||||
};
|
||||
|
||||
|
||||
if(p->cli->server.game_info.socket > 0) {
|
||||
printf("Has Connection\n");
|
||||
if(SendUserTransaction(&st_move, p->cli->server.game_info.socket)) {
|
||||
printf("NG\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!strcmp(ib, "w")) {Shoot(e,SHOOT_N); ib += 1; continue;}
|
||||
if(!strcmp(ib, "d")) {Shoot(e,SHOOT_E); ib += 1; continue;}
|
||||
if(!strcmp(ib, "s")) {Shoot(e,SHOOT_S); ib += 1; continue;}
|
||||
if(!strcmp(ib, "a")) {Shoot(e,SHOOT_W); ib += 1; continue;}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int entity_can_move = EntityMove(e, &mv);
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
memset(td->inputBuffer,0,32);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PlayerCFree(void* data) {
|
||||
return 0;
|
||||
}
|
||||
int PlayerCInit(void* data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
entity* CreatePlayerCharecter(player_data pd, client* cli ,world* w) {
|
||||
entity* ent = malloc(sizeof(entity));
|
||||
player_c* p= malloc(sizeof(player_c));
|
||||
|
||||
*p = (player_c){
|
||||
.pd = pd,
|
||||
.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 = PlayerCInit,
|
||||
.update = PlayerUpdateC,
|
||||
.free = PlayerCFree,
|
||||
},
|
||||
.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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user