-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from jorgegv/rename_sprite_to_enemy
Rename sprite to enemy
- Loading branch information
Showing
17 changed files
with
279 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// RAGE1 - Retro Adventure Game Engine, release 1 | ||
// (c) Copyright 2020 Jorge Gonzalez Villalonga <jorgegv@daikon.es> | ||
// | ||
// This code is published under a GNU GPL license version 3 or later. See | ||
// LICENSE file in the distribution for details. | ||
// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#ifndef _ENEMY_H | ||
#define _ENEMY_H | ||
|
||
#include "rage1/sprite.h" | ||
#include "rage1/types.h" | ||
|
||
#define ENEMY_MOVE_LINEAR 0x00 | ||
struct enemy_movement_data_s { | ||
uint8_t type; // LINEAR, etc. | ||
uint8_t delay; // dx,dy are added every 'delay' calls | ||
uint8_t delay_counter; // current movement delay counter | ||
union { // this union must be the last struct component | ||
struct { | ||
uint8_t xmin,xmax; // enemy moves bouncing in a rectangle | ||
uint8_t ymin,ymax; // (xmin,ymin)-(xmax,ymax) | ||
int8_t dx,dy; // current position increments | ||
uint8_t initx,inity; // reset positions | ||
int8_t initdx,initdy; // reset increments | ||
} linear; | ||
} data; | ||
}; | ||
|
||
struct enemy_info_s { | ||
struct sp1_ss *sprite; // ptr to SP1 sprite struct | ||
uint8_t width,height; // dimensions in pixels ( rows,cols * 8 ) | ||
struct sprite_animation_data_s animation; // sprite animation data | ||
struct position_data_s position; // enemy position data | ||
struct enemy_movement_data_s movement; // enemy movement data | ||
uint16_t flags; // flags | ||
}; | ||
|
||
// enemy flags macros and definitions | ||
#define GET_ENEMY_FLAG(s,f) ( (s).flags & (f) ) | ||
#define SET_ENEMY_FLAG(s,f) ( (s).flags |= (f) ) | ||
#define RESET_ENEMY_FLAG(s,f) ( (s).flags &= ~(f) ) | ||
|
||
#define F_ENEMY_ACTIVE 0x0001 | ||
#define F_ENEMY_BOUNCE 0x0002 | ||
|
||
#define IS_ENEMY_ACTIVE(s) (GET_ENEMY_FLAG((s),F_ENEMY_ACTIVE)) | ||
#define ENEMY_MUST_BOUNCE(s) (GET_ENEMY_FLAG((s),F_ENEMY_BOUNCE)) | ||
|
||
// sets all enemies in a enemy set to initial positions and frames | ||
void enemy_reset_position_all( uint8_t num_enemies, struct enemy_info_s *enemies ); | ||
|
||
// animates and moves all enemies in a enemy set according to their rules | ||
void enemy_animate_and_move_all( uint8_t num_enemies, struct enemy_info_s *enemies ); | ||
|
||
void enemy_move_offscreen_all( uint8_t num_enemies, struct enemy_info_s *enemies ); | ||
|
||
#endif // _ENEMY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// RAGE1 - Retro Adventure Game Engine, release 1 | ||
// (c) Copyright 2020 Jorge Gonzalez Villalonga <jorgegv@daikon.es> | ||
// | ||
// This code is published under a GNU GPL license version 3 or later. See | ||
// LICENSE file in the distribution for details. | ||
// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include "rage1/game_state.h" | ||
#include "rage1/sprite.h" | ||
#include "rage1/enemy.h" | ||
#include "rage1/screen.h" | ||
#include "rage1/map.h" | ||
#include "rage1/debug.h" | ||
#include "rage1/util.h" | ||
|
||
#include "game_data.h" | ||
|
||
void enemy_reset_position_all( uint8_t num_enemies, struct enemy_info_s *enemies ) { | ||
static struct enemy_info_s *s; | ||
static uint8_t n; | ||
|
||
n = num_enemies; | ||
while( n-- ) { | ||
s = &enemies[n]; // eficiency matters ;-) | ||
if ( ! IS_ENEMY_ACTIVE(*s) ) // skip if not active | ||
continue; | ||
|
||
// reset enemy state to initial values - update xmax and ymax also | ||
s->animation.current_frame = s->animation.delay_counter = 0; | ||
s->position.x = s->movement.data.linear.initx; | ||
s->position.y = s->movement.data.linear.inity; | ||
s->movement.data.linear.dx = s->movement.data.linear.initdx; | ||
s->movement.data.linear.dy = s->movement.data.linear.initdy; | ||
|
||
// adjust xmax, ymax and move enemy to initial position | ||
s->position.xmax = s->position.x + s->width - 1; | ||
s->position.ymax = s->position.y + s->height - 1; | ||
sp1_MoveSprPix( s->sprite, &game_area, s->animation.frames[0], s->position.x, s->position.y ); | ||
} | ||
} | ||
|
||
void enemy_animate_and_move_all( uint8_t num_enemies, struct enemy_info_s *enemies ) { | ||
static struct enemy_info_s *s; | ||
static struct sprite_animation_data_s *anim; | ||
static struct position_data_s *pos; | ||
static struct enemy_movement_data_s *move; | ||
static uint8_t n; | ||
|
||
n = num_enemies; | ||
while( n-- ) { | ||
s = &enemies[n]; // efficiency matters ;-) | ||
if ( ! IS_ENEMY_ACTIVE(*s) ) // skip if not active | ||
continue; | ||
|
||
// animate sprite | ||
anim = &s->animation; | ||
if ( ++anim->delay_counter == anim->delay ) { | ||
anim->delay_counter = 0; | ||
if ( ++anim->current_frame == anim->num_frames ) { | ||
anim->current_frame = 0; | ||
} | ||
} | ||
|
||
// set new sprite position according to movement rules | ||
pos = &s->position; | ||
move = &s->movement; | ||
switch ( move->type ) { | ||
case ENEMY_MOVE_LINEAR: | ||
if ( ++move->delay_counter == move->delay ) { | ||
move->delay_counter = 0; | ||
pos->x += move->data.linear.dx; | ||
if ( | ||
( pos->x >= move->data.linear.xmax ) || | ||
( pos->x <= move->data.linear.xmin ) || | ||
( ENEMY_MUST_BOUNCE(*s) && ( | ||
( TILE_TYPE_AT( PIXEL_TO_CELL_COORD( pos->y ), PIXEL_TO_CELL_COORD( pos->x + s->width ) ) == TT_OBSTACLE ) || | ||
( TILE_TYPE_AT( PIXEL_TO_CELL_COORD( pos->y ), PIXEL_TO_CELL_COORD( pos->x - 1 ) ) == TT_OBSTACLE ) || | ||
( TILE_TYPE_AT( PIXEL_TO_CELL_COORD( pos->y + s->height - 1), PIXEL_TO_CELL_COORD( pos->x + s->width ) ) == TT_OBSTACLE ) || | ||
( TILE_TYPE_AT( PIXEL_TO_CELL_COORD( pos->y + s->height - 1), PIXEL_TO_CELL_COORD( pos->x - 1 ) ) == TT_OBSTACLE ) | ||
) ) | ||
) { // then | ||
move->data.linear.dx = -move->data.linear.dx; | ||
} | ||
pos->y += move->data.linear.dy; | ||
if ( | ||
( pos->y >= move->data.linear.ymax ) || | ||
( pos->y <= move->data.linear.ymin ) || | ||
( ENEMY_MUST_BOUNCE(*s) && ( | ||
( TILE_TYPE_AT( PIXEL_TO_CELL_COORD( pos->y + s->height ), PIXEL_TO_CELL_COORD( pos->x ) ) == TT_OBSTACLE ) || | ||
( TILE_TYPE_AT( PIXEL_TO_CELL_COORD( pos->y - 1 ), PIXEL_TO_CELL_COORD( pos->x ) ) == TT_OBSTACLE ) || | ||
( TILE_TYPE_AT( PIXEL_TO_CELL_COORD( pos->y + s->height ), PIXEL_TO_CELL_COORD( pos->x + s->width - 1) ) == TT_OBSTACLE ) || | ||
( TILE_TYPE_AT( PIXEL_TO_CELL_COORD( pos->y - 1), PIXEL_TO_CELL_COORD( pos->x + s->width - 1 ) ) == TT_OBSTACLE ) | ||
) ) | ||
) { // then | ||
move->data.linear.dy = -move->data.linear.dy; | ||
} | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
// adjust xmax, ymax and move sprite to new position | ||
pos->xmax = pos->x + s->width - 1; | ||
pos->ymax = pos->y + s->height - 1; | ||
sp1_MoveSprPix( s->sprite, &game_area, anim->frames[anim->current_frame], pos->x, pos->y ); | ||
} | ||
} | ||
|
||
void enemy_move_offscreen_all( uint8_t num_enemies, struct enemy_info_s *enemies ) { | ||
static uint8_t i; | ||
i = num_enemies; | ||
while ( i-- ) sprite_move_offscreen( enemies[i].sprite ); | ||
} | ||
|
Oops, something went wrong.