there is now a player character and the world renders using a cammera
This commit is contained in:
186
source/game_source/world.c
Normal file
186
source/game_source/world.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user