Permalink
Browse files

improved movement, camera follows focus, and more

  • Loading branch information...
natearn committed Feb 23, 2012
1 parent 2d4772a commit 02a9fe2dd24ac7b5bbf15bc44dd1c4ce660bb8a4
Showing with 71 additions and 46 deletions.
  1. +1 −2 TODO
  2. +3 −1 gamestate.c
  3. +44 −23 newgame.c
  4. +9 −7 sprite.c
  5. +14 −13 sprite.h
View
3 TODO
@@ -1,7 +1,6 @@
in order of importance:
- - improve sprite action routines and animation table/attributes
- - decide how affects with durations will be handled
+ - decide how affects with durations will be handled
- decide if sprite updating and sprite rendering need to be separated
- create a keyboard input state machine
- create a field (sensor shape) which applies a force or velocity to the sprite body;
View
@@ -49,11 +49,13 @@ int AddSprite( struct GameState *game, Sprite *sprite, cpVect posn ) {
int RenderGameState( struct GameState *game, SDL_Surface *screen, Uint32 delta ) {
assert( game && screen );
struct SpriteList *list = NULL;
+ /* TODO: replace hard-coded offset */
+ cpVect screen_posn = cpvadd( cpBodyGetPos(game->focus->body), cpv(-320.0, -240.0) );
/* render the map */
SDL_FillRect( screen, NULL, 0x0 ); /* temporary: fill screen black */
/* render the sprites */
for(list = game->sprites; list; list = list->next) {
- if( DrawSprite( list->sprite, screen, delta )) {
+ if( DrawSprite( list->sprite, screen, screen_posn, delta )) {
return -1;
}
}
View
@@ -102,30 +102,51 @@ Uint32 CalcWaitTime( Uint32 target, Uint32 delay, Uint32 min ) {
void HandleInput( unsigned int keys[], struct GameState *game, SDL_Event *event ) {
assert( event->type == SDL_KEYDOWN || event->type == SDL_KEYUP );
int x, y;
- switch( event->key.keysym.sym ) {
- case SDLK_LEFT:
- keys[2] = (event->type == SDL_KEYDOWN);
- y = keys[1] - keys[0];
- x = keys[3] - keys[2];
- break;
- case SDLK_RIGHT:
- keys[3] = (event->type == SDL_KEYDOWN);
- y = keys[1] - keys[0];
- x = keys[3] - keys[2];
- break;
- case SDLK_UP:
- keys[0] = (event->type == SDL_KEYDOWN);
- y = keys[1] - keys[0];
- x = keys[3] - keys[2];
- break;
- case SDLK_DOWN:
- keys[1] = (event->type == SDL_KEYDOWN);
- y = keys[1] - keys[0];
- x = keys[3] - keys[2];
- break;
- default:
- break;
+ if( event->type == SDL_KEYDOWN ) {
+ switch( event->key.keysym.sym ) {
+ case SDLK_a:
+ return;
+ case SDLK_s:
+ return;
+ case SDLK_d:
+ return;
+ case SDLK_f:
+ return;
+ case SDLK_LEFT:
+ keys[2] = 1;
+ break;
+ case SDLK_RIGHT:
+ keys[3] = 1;
+ break;
+ case SDLK_UP:
+ keys[0] = 1;
+ break;
+ case SDLK_DOWN:
+ keys[1] = 1;
+ break;
+ default:
+ return;
+ }
+ } else {
+ switch( event->key.keysym.sym ) {
+ case SDLK_LEFT:
+ keys[2] = 0;
+ break;
+ case SDLK_RIGHT:
+ keys[3] = 0;
+ break;
+ case SDLK_UP:
+ keys[0] = 0;
+ break;
+ case SDLK_DOWN:
+ keys[1] = 0;
+ break;
+ default:
+ return;
+ }
}
+ y = keys[1] - keys[0];
+ x = keys[3] - keys[2];
switch(x) {
case -1:
switch(y) {
View
@@ -26,7 +26,7 @@ Sprite *InitSprite( Sprite *sprite, struct Resource *resource ) {
/* TODO: replace hard-coded constants with parameters */
sprite->control = cpBodyNew( INFINITY, INFINITY );
sprite->body = cpBodyNew( 10, INFINITY );
- sprite->shape = cpCircleShapeNew( sprite->body, 12, cpvzero );
+ sprite->shape = cpCircleShapeNew( sprite->body, 10, cpvzero );
//cpShapeSetElasticity(sprite->shape, 0.0f);
sprite->shape->e = 0.0f;
//cpShapeSetFriction(sprite->shape, 0.7f);
@@ -36,9 +36,12 @@ Sprite *InitSprite( Sprite *sprite, struct Resource *resource ) {
sprite->pivot->maxBias = 0;
//cpConstraintSetMaxForce(sprite->pivot, 10000.0f); // emulate linear friction
sprite->pivot->maxForce = 1000000.0;
+
for( size_t i=0; i < NUM_ATTR; i++ ) {
sprite->attributes[i] = 0;
}
+ sprite->facing = NONE;
+ sprite->moving = NONE;
return sprite;
}
@@ -83,11 +86,10 @@ Uint32 UpdateSprite( Sprite *sprite, Uint32 time ) {
}
/* this needs to be separated */
-int DrawSprite( Sprite *sprite, SDL_Surface *surface, Uint32 delta ) {
+int DrawSprite( Sprite *sprite, SDL_Surface *surface, cpVect screen_posn, Uint32 delta ) {
assert(sprite && surface);
SDL_Rect *frame;
SDL_Rect posn = {0,0,0,0};
- cpVect offset;
/* determine current animation */
if( sprite->animation != sprite->table[sprite->attributes[ATTR_FACE]][sprite->attributes[ATTR_MOVE]] ) {
@@ -100,10 +102,10 @@ int DrawSprite( Sprite *sprite, SDL_Surface *surface, Uint32 delta ) {
sprite->time += delta;
frame = GetUpdatedFrame( sprite->animation, &sprite->index, &sprite->time );
- /* get the sprite position */
- /* TODO: calculated offset to center of grav */
- offset = cpvzero;
- if( sprite->body ) posn = Vect2Rect( cpvadd( offset, cpBodyGetPos( sprite->body )));
+ /* get the sprite screen position */
+ posn = Vect2Rect( cpvsub( cpBodyGetPos(sprite->body), screen_posn ));
+ posn.y -= frame->h;
+ posn.x -= frame->w/2;
/* blit */
if( SDL_BlitSurface( sprite->resource->surface, frame, surface, &posn )) {
View
@@ -6,18 +6,17 @@
#include "animation.h"
#include "resource.h"
-typedef enum {
- NONE = 0,
- UP,
- DOWN,
- LEFT,
- RIGHT,
- UP_RIGHT,
- UP_LEFT,
- DOWN_RIGHT,
- DOWN_LEFT,
- NUM_DIRECTION = 8
-} Direction;
+#define NONE 0
+#define UP 1
+#define DOWN 2
+#define LEFT 3
+#define RIGHT 4
+#define UP_RIGHT 5
+#define UP_LEFT 6
+#define DOWN_RIGHT 7
+#define DOWN_LEFT 8
+#define NUM_DIRECTION 9
+typedef unsigned int Direction;
#define FACE_LEFT 0
#define FACE_RIGHT 1
@@ -43,6 +42,8 @@ typedef struct {
/* game data */
unsigned int attributes[NUM_ATTR]; /* collection of state enums used by the animation table */
+ Direction facing;
+ Direction moving;
/* animation data */
Animation *table[NUM_FACE][NUM_MOVE]; /* look-up table */
@@ -73,7 +74,7 @@ void FreeSprite( Sprite *sprite );
Uint32 UpdateSprite( Sprite *sprite, Uint32 time );
/* draw the sprite on the surface */
-int DrawSprite( Sprite *sprite, SDL_Surface *surface, Uint32 delta );
+int DrawSprite( Sprite *sprite, SDL_Surface *surface, cpVect screen_posn, Uint32 delta );
/* get methods (hiding the implementation) */
cpBody *GetSpriteBody( Sprite *sprite );

0 comments on commit 02a9fe2

Please sign in to comment.