Skip to content

Commit

Permalink
added PCManager which creates and manages a player character. Viewpor…
Browse files Browse the repository at this point in the history
…t now fiollows the character hurray
  • Loading branch information
britown88 committed Nov 8, 2015
1 parent ea938bb commit d59fbf2
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 105 deletions.
1 change: 1 addition & 0 deletions BTGame/BT.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void _initEntitySystem(BTGame *self){
RegisterManager(self->managers.gridManager, createGridManager(&self->view));
RegisterManager(self->managers.interpolationManager, createInterpolationManager(&self->view));
RegisterManager(self->managers.gridMovementManager, createGridMovementManager(&self->view));
RegisterManager(self->managers.pcManager, createPCManager(&self->view));
}

void _destroyEntitySystem(BTGame *self){
Expand Down
1 change: 1 addition & 0 deletions BTGame/BTGame.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<ClCompile Include="GridMovementManager.c" />
<ClCompile Include="InterpolationManager.c" />
<ClCompile Include="LightGrid.c" />
<ClCompile Include="PCManager.c" />
<ClCompile Include="WorldState.c" />
<ClCompile Include="BT.c" />
<ClCompile Include="CoreComponents.c" />
Expand Down
3 changes: 3 additions & 0 deletions BTGame/BTGame.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,8 @@
<ClCompile Include="GridMovementManager.c">
<Filter>Source Files\Managers</Filter>
</ClCompile>
<ClCompile Include="PCManager.c">
<Filter>Source Files\Managers</Filter>
</ClCompile>
</ItemGroup>
</Project>
9 changes: 7 additions & 2 deletions BTGame/GridManager.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ int gridManagerQueryOcclusion(GridManager *self, Recti *area, OcclusionCell *gri
return count;
}

short gridManagerWidth(GridManager *self) {
return self->width;
}
short gridManagerHeight(GridManager *self) {
return self->height;
}

GridManager *createGridManager(WorldView *view) {
GridManager *out = checkedCalloc(1, sizeof(GridManager));
out->view = view;
Expand Down Expand Up @@ -388,8 +395,6 @@ void gridManagerRender(GridManager *self, Frame *frame) {
}
}
}


}

void gridManagerRenderLighting(GridManager *self, Frame *frame) {
Expand Down
3 changes: 3 additions & 0 deletions BTGame/GridManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ void gridManagerRender(GridManager *self, Frame *frame);
void gridManagerRenderLighting(GridManager *self, Frame *frame);
void gridManagerSetAmbientLight(GridManager *self, byte level);

short gridManagerWidth(GridManager *self);
short gridManagerHeight(GridManager *self);

//changes the schema of a given tile (world-tile coords)
void gridManagerSetTileSchema(GridManager *self, int x, int y, byte schema);

Expand Down
15 changes: 15 additions & 0 deletions BTGame/GridMovementManager.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,20 @@ void gridMovementManagerMoveEntity(GridMovementManager *self, Entity *e, short x
COMPONENT_ADD(e, TGridMovingComponent, x, y, 0, 0);
_stepMovement(e);

}
}

void gridMovementManagerMoveEntityRelative(GridMovementManager *self, Entity *e, short x, short y) {
TGridMovingComponent *tgc = entityGet(TGridMovingComponent)(e);
GridComponent *gc = entityGet(GridComponent)(e);

if (tgc) {
tgc->destX = tgc->nextX + x;
tgc->destY = tgc->nextY + y;
}
else {
COMPONENT_ADD(e, TGridMovingComponent, gc->x + x, gc->y + y, 0, 0);
_stepMovement(e);

}
}
9 changes: 9 additions & 0 deletions BTGame/Managers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ typedef struct CursorManager_t CursorManager;
typedef struct GridManager_t GridManager;
typedef struct InterpolationManager_t InterpolationManager;
typedef struct GridMovementManager_t GridMovementManager;
typedef struct PCManager_t PCManager;

typedef struct Frame_t Frame;
typedef struct WorldView_t WorldView;
Expand All @@ -18,6 +19,7 @@ typedef struct BTManagers_t {
GridManager *gridManager;
InterpolationManager *interpolationManager;
GridMovementManager *gridMovementManager;
PCManager *pcManager;
}BTManagers;

RenderManager *createRenderManager(WorldView *view, double *fps);
Expand All @@ -34,6 +36,13 @@ void interpolationManagerUpdate(InterpolationManager *self);
GridMovementManager *createGridMovementManager(WorldView *view);
void gridMovementManagerUpdate(GridMovementManager *self);
void gridMovementManagerMoveEntity(GridMovementManager *self, Entity *e, short x, short y);
void gridMovementManagerMoveEntityRelative(GridMovementManager *self, Entity *e, short x, short y);

PCManager *createPCManager(WorldView *view);
void pcManagerUpdate(PCManager *self);
void pcManagerCreatePC(PCManager *self);
void pcManagerMove(PCManager *self, short x, short y);
void pcManagerMoveRelative(PCManager *self, short x, short y);



63 changes: 63 additions & 0 deletions BTGame/PCManager.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "Managers.h"
#include "Entities/Entities.h"
#include "CoreComponents.h"
#include "WorldView.h"
#include "LightGrid.h"
#include "segashared/CheckedMemory.h"


struct PCManager_t {
Manager m;
WorldView *view;

Entity *pc;
};

ImplManagerVTable(PCManager)

PCManager *createPCManager(WorldView *view) {
PCManager *out = checkedCalloc(1, sizeof(PCManager));
out->view = view;
out->m.vTable = CreateManagerVTable(PCManager);
return out;
}

void _destroy(PCManager *self) {
checkedFree(self);
}
void _onDestroy(PCManager *self, Entity *e) {}
void _onUpdate(PCManager *self, Entity *e) {}

void pcManagerUpdate(PCManager *self) {
Viewport *vp = &self->view->viewport;
PositionComponent *pc = entityGet(PositionComponent)(self->pc);
short gridWidth = gridManagerWidth(self->view->managers->gridManager);
short gridHeight = gridManagerHeight(self->view->managers->gridManager);
short xCenter = (GRID_WIDTH / 2) * GRID_CELL_SIZE;
short yCenter = (GRID_HEIGHT / 2) * GRID_CELL_SIZE;
int xOffset = MIN(gridWidth, MAX(0, pc->x - xCenter));
int yOffset = MIN(gridHeight, MAX(0, pc->y - yCenter));


vp->worldPos = (Int2) { xOffset, yOffset };
}

void pcManagerCreatePC(PCManager *self) {
self->pc = entityCreate(self->view->entitySystem);
COMPONENT_ADD(self->pc, PositionComponent, 0, 0);
COMPONENT_ADD(self->pc, ImageComponent, stringIntern("assets/img/cursor.ega"));
COMPONENT_ADD(self->pc, LayerComponent, LayerGrid);
COMPONENT_ADD(self->pc, InViewComponent, 0);
COMPONENT_ADD(self->pc, GridComponent, 11, 6);
COMPONENT_ADD(self->pc, LightComponent, 0, MAX_BRIGHTNESS);

entityUpdate(self->pc);
}

void pcManagerMove(PCManager *self, short x, short y) {
gridMovementManagerMoveEntity(self->view->managers->gridMovementManager, self->pc, x, y);
}

void pcManagerMoveRelative(PCManager *self, short x, short y) {
gridMovementManagerMoveEntityRelative(self->view->managers->gridMovementManager, self->pc, x, y);
}
126 changes: 24 additions & 102 deletions BTGame/WorldState.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

typedef struct {
WorldView *view;
Entity *mouseLight, *moveTest;
}WorldState;

static void _boardStateDestroy(WorldState *self){
Expand All @@ -45,10 +44,9 @@ void _boardUpdate(WorldState *state, GameStateUpdate *m){
cursorManagerUpdate(managers->cursorManager, mousePos.x, mousePos.y);
interpolationManagerUpdate(managers->interpolationManager);
gridMovementManagerUpdate(managers->gridMovementManager);
pcManagerUpdate(managers->pcManager);
}

static Int2 testlinepos = { 0, 0 };

static void _handleKeyboard(WorldState *state){
BTManagers *managers = state->view->managers;
Keyboard *k = appGetKeyboard(appGet());
Expand Down Expand Up @@ -84,34 +82,17 @@ static void _handleKeyboard(WorldState *state){
}

if (keyboardIsDown(k, SegaKey_W)) {
vp->worldPos.y -= speed;
pcManagerMoveRelative(state->view->managers->pcManager, 0, -1);
}
if (keyboardIsDown(k, SegaKey_S)) {
vp->worldPos.y += speed;
pcManagerMoveRelative(state->view->managers->pcManager, 0, 1);
}
if (keyboardIsDown(k, SegaKey_A)) {
vp->worldPos.x -= speed;
pcManagerMoveRelative(state->view->managers->pcManager, -1, 0);
}
if (keyboardIsDown(k, SegaKey_D)) {
vp->worldPos.x += speed;
}

if (keyboardIsDown(k, SegaKey_Up)) {
testlinepos.y -= speed;
}
if (keyboardIsDown(k, SegaKey_Down)) {
testlinepos.y += speed;
pcManagerMoveRelative(state->view->managers->pcManager, 1, 0);
}
if (keyboardIsDown(k, SegaKey_Left)) {
testlinepos.x -= speed;
}
if (keyboardIsDown(k, SegaKey_Right)) {
testlinepos.x += speed;
}

vp->worldPos.x = MAX(0, vp->worldPos.x);
vp->worldPos.y = MAX(0, vp->worldPos.y);

}

static void _handleMouse(WorldState *state){
Expand All @@ -121,76 +102,40 @@ static void _handleMouse(WorldState *state){
MouseEvent event = { 0 };
Int2 pos = mouseGetPosition(mouse);
Viewport *vp = &state->view->viewport;
while (mousePopEvent(mouse, &event)){
if (event.action == SegaMouse_Scrolled) {
LightComponent *lc = entityGet(LightComponent)(state->mouseLight);
lc->radius = MAX(0, lc->radius + event.pos.y);
}
else if (event.action == SegaMouse_Released && event.button == SegaMouseBtn_Left) {
LightComponent *lc = entityGet(LightComponent)(state->mouseLight);
PositionComponent *pc = entityGet(PositionComponent)(state->mouseLight);
int x = pc->x, y = pc->y;
byte rad = lc->radius, cl = lc->centerLevel;
Entity *e= entityCreate(state->view->entitySystem);
COMPONENT_ADD(e, PositionComponent, .x = x, .y = y);
COMPONENT_ADD(e, LightComponent, .radius = rad, .centerLevel = cl);
entityUpdate(e);
}

}

COMPONENT_LOCK(PositionComponent, cpos, state->mouseLight, {
cpos->x = pos.x - vp->region.origin_x + vp->worldPos.x;
cpos->y = pos.y - vp->region.origin_y + vp->worldPos.y;
});
//while (mousePopEvent(mouse, &event)){
// if (event.action == SegaMouse_Scrolled) {
// LightComponent *lc = entityGet(LightComponent)(state->mouseLight);
// lc->radius = MAX(0, lc->radius + event.pos.y);
// }
// else if (event.action == SegaMouse_Released && event.button == SegaMouseBtn_Left) {
// LightComponent *lc = entityGet(LightComponent)(state->mouseLight);
// PositionComponent *pc = entityGet(PositionComponent)(state->mouseLight);
// int x = pc->x, y = pc->y;
// byte rad = lc->radius, cl = lc->centerLevel;
// Entity *e= entityCreate(state->view->entitySystem);
// COMPONENT_ADD(e, PositionComponent, .x = x, .y = y);
// COMPONENT_ADD(e, LightComponent, .radius = rad, .centerLevel = cl);
// entityUpdate(e);
// }

//}

if (mouseIsDown(mouse, SegaMouseBtn_Right)) {
//PositionComponent *pc = entityGet(PositionComponent)(state->mouseLight);
//int x = pc->x / GRID_CELL_SIZE, y = pc->y / GRID_CELL_SIZE;
//gridManagerSetTileSchema(state->view->managers->gridManager, x, y, 7);

gridMovementManagerMoveEntity(state->view->managers->gridMovementManager, state->moveTest,
pcManagerMove(state->view->managers->pcManager,
(pos.x - vp->region.origin_x + vp->worldPos.x) / GRID_CELL_SIZE,
(pos.y - vp->region.origin_y + vp->worldPos.y) / GRID_CELL_SIZE);
}

//vp->worldPos.x = pos.x - vp->region.origin_x - (vp->region.width / 2);
//vp->worldPos.y = pos.y - vp->region.origin_y - (vp->region.height / 2);
}

void _boardHandleInput(WorldState *state, GameStateHandleInput *m){
_handleKeyboard(state);
_handleMouse(state);
}

static void _drawLineTest(WorldState *state, Frame *frame) {
Mouse *mouse = appGetMouse(appGet());
Int2 pos = mouseGetPosition(mouse);
FrameRegion *vp = &state->view->viewport.region;
Int2 v1 = testlinepos;
Int2 v2 = { pos.x - vp->origin_x, pos.y - vp->origin_y };
Recti r = { 70, 70, 126, 126 };

bool intersects = lineSegmentIntersectsAABBi(v1, v2, &r);

frameRenderRect(frame, vp, r.left, r.top, r.right, r.bottom, 2);

frameRenderLine(frame, vp, v1.x, v1.y, v2.x, v2.y, intersects ? 5 : 14);
}

void _boardRender(WorldState *state, GameStateRender *m){
renderManagerRender(state->view->managers->renderManager, m->frame);

//_drawLineTest(state, m->frame);


}

static void _testLisp() {
LispExpr ex = lispCreatef32(1.0f);

float *f = lispf32(&ex);
int *i = lispi32(&ex);
}

static void _addTestEntities(WorldState *state) {
Expand All @@ -200,37 +145,14 @@ static void _addTestEntities(WorldState *state) {
COMPONENT_ADD(e, LayerComponent, LayerBackground);
COMPONENT_ADD(e, RenderedUIComponent, 0);
entityUpdate(e);

state->mouseLight = entityCreate(state->view->entitySystem);
COMPONENT_ADD(state->mouseLight, PositionComponent, 0, 0);
COMPONENT_ADD(state->mouseLight, LightComponent, .radius = 10, .centerLevel = MAX_BRIGHTNESS);
entityUpdate(state->mouseLight);

e = entityCreate(state->view->entitySystem);
COMPONENT_ADD(e, PositionComponent, 0, 0);
COMPONENT_ADD(e, ImageComponent, stringIntern("assets/img/cursor.ega"));
COMPONENT_ADD(e, LayerComponent, LayerGrid);
COMPONENT_ADD(e, InViewComponent, 0);
COMPONENT_ADD(e, GridComponent, 10, 10);

entityUpdate(e);

state->moveTest = e;

//e = entityCreate(view->entitySystem);
//COMPONENT_ADD(e, PositionComponent, 0, 0);
//COMPONENT_ADD(e, ImageComponent, stringIntern("assets/img/dotagrid.ega"));
//COMPONENT_ADD(e, InViewComponent, 0);
//COMPONENT_ADD(e, LayerComponent, LayerGrid);
//entityUpdate(e);
}

static void _enterState(WorldState *state) {
appLoadPalette(appGet(), "assets/img/default2.pal");
cursorManagerCreateCursor(state->view->managers->cursorManager);
pcManagerCreatePC(state->view->managers->pcManager);

_addTestEntities(state);
_testLisp();
}

StateClosure gameStateCreateWorld(WorldView *view){
Expand Down
4 changes: 3 additions & 1 deletion Entities/Entities.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ MyTestManager *createMyTestManager(WorldView *view){
return out;
}
void _destroy(MyTestManager *self){}
void _destroy(MyTestManager *self){
checkedFree(self);
}
void _onDestroy(MyTestManager *self, Entity *e){}
void _onUpdate(MyTestManager *self, Entity *e){}
Expand Down

0 comments on commit d59fbf2

Please sign in to comment.