@ -36,6 +36,18 @@ class Object{
std : : string name ;
} ;
class Animation {
public :
olc : : Decal * spr ;
std : : vector < olc : : vi2d > frames ;
int frame = 0 ;
bool flipped = false ;
olc : : vi2d getCurrentFrame ( ) {
return frames [ frame ] ;
}
} ;
class SeasonsOfLoneliness : public olc : : PixelGameEngine
{
public :
@ -78,8 +90,22 @@ public:
int oxygenQualityLevel = 34 ;
int plantState = 0b10010110101000101010100110101010 ;
olc : : SplashScreen splash ;
olc : : Decal * DOME_DECAL , * FOOD_METER_DECAL , * OXYGEN_METER_DECAL , * PLANT_DECAL ;
Animation current_playerAnim ;
Animation playerAnim ;
Animation playerAnimRight ;
Animation playerAnimLeft ;
Animation playerAnimDown ;
Animation playerAnimWalkUp ;
Animation playerAnimWalkDown ;
Animation playerAnimWalkRight ;
Animation playerAnimWalkLeft ;
Animation playerAnimPlantUp ;
Animation playerAnimPlantDown ;
Animation playerAnimPlantRight ;
Animation playerAnimPlantLeft ;
olc : : Decal * DOME_DECAL , * FOOD_METER_DECAL , * OXYGEN_METER_DECAL , * PLANT_DECAL ,
* PLAYER_DECAL ;
std : : map < std : : string , olc : : Decal * > BASE_OBJECTS ;
bool OnUserCreate ( ) override
@ -93,6 +119,46 @@ public:
FOOD_METER_DECAL = new olc : : Decal ( new olc : : Sprite ( " assets/corn.png " ) ) ;
OXYGEN_METER_DECAL = new olc : : Decal ( new olc : : Sprite ( " assets/co2.png " ) ) ;
PLANT_DECAL = new olc : : Decal ( new olc : : Sprite ( " assets/plant.png " ) ) ;
PLAYER_DECAL = new olc : : Decal ( new olc : : Sprite ( " assets/player.png " ) ) ;
current_playerAnim . spr = PLAYER_DECAL ;
playerAnim . spr = PLAYER_DECAL ;
playerAnimRight . spr = PLAYER_DECAL ;
playerAnimLeft . spr = PLAYER_DECAL ;
playerAnimLeft . flipped = true ;
playerAnimDown . spr = PLAYER_DECAL ;
playerAnimWalkUp . spr = PLAYER_DECAL ;
playerAnimWalkDown . spr = PLAYER_DECAL ;
playerAnimWalkRight . spr = PLAYER_DECAL ;
playerAnimWalkLeft . spr = PLAYER_DECAL ;
playerAnimWalkLeft . flipped = true ;
playerAnimPlantUp . spr = PLAYER_DECAL ;
playerAnimPlantDown . spr = PLAYER_DECAL ;
playerAnimPlantRight . spr = PLAYER_DECAL ;
playerAnimPlantLeft . spr = PLAYER_DECAL ;
playerAnimPlantLeft . flipped = true ;
playerAnim . frames . push_back ( { 64 , 0 } ) ;
playerAnimRight . frames . push_back ( { 32 , 0 } ) ;
playerAnimLeft . frames . push_back ( { 32 , 0 } ) ;
playerAnimDown . frames . push_back ( { 0 , 0 } ) ;
playerAnimWalkUp . frames . push_back ( { 0 , 96 } ) ;
playerAnimWalkUp . frames . push_back ( { 32 , 96 } ) ;
playerAnimWalkUp . frames . push_back ( { 64 , 96 } ) ;
playerAnimWalkRight . frames . push_back ( { 0 , 64 } ) ;
playerAnimWalkLeft . frames . push_back ( { 0 , 64 } ) ;
playerAnimWalkRight . frames . push_back ( { 32 , 64 } ) ;
playerAnimWalkLeft . frames . push_back ( { 32 , 64 } ) ;
playerAnimWalkRight . frames . push_back ( { 64 , 64 } ) ;
playerAnimWalkLeft . frames . push_back ( { 64 , 64 } ) ;
playerAnimWalkDown . frames . push_back ( { 0 , 32 } ) ;
playerAnimWalkDown . frames . push_back ( { 32 , 32 } ) ;
playerAnimWalkDown . frames . push_back ( { 64 , 32 } ) ;
playerAnimPlantUp . frames . push_back ( { 64 , 128 } ) ;
playerAnimPlantRight . frames . push_back ( { 32 , 128 } ) ;
playerAnimPlantLeft . frames . push_back ( { 32 , 128 } ) ;
playerAnimPlantDown . frames . push_back ( { 0 , 128 } ) ;
current_playerAnim = playerAnimWalkDown ;
BASE_OBJECTS [ " DOME " ] = DOME_DECAL ;
BASE_OBJECTS [ " PLANT " ] = PLANT_DECAL ;
BASE_OBJECTS [ " EXIT " ] = NULL ;
@ -101,6 +167,10 @@ public:
}
void GetAnyKeyPress ( ) override {
if ( ! GetKey ( olc : : W ) . bPressed & & ! GetKey ( olc : : A ) . bPressed & & ! GetKey ( olc : : S ) . bPressed & & ! GetKey ( olc : : D ) . bPressed & &
! GetKey ( olc : : UP ) . bPressed & & ! GetKey ( olc : : RIGHT ) . bPressed & & ! GetKey ( olc : : DOWN ) . bPressed & & ! GetKey ( olc : : LEFT ) . bPressed ) {
ActionButtonPress ( ) ;
}
if ( messageBoxVisible ) {
if ( messageBoxCursor ! = messageBoxRefText . length ( ) ) {
while ( messageBoxCursor < messageBoxRefText . length ( ) ) {
@ -119,6 +189,17 @@ public:
}
}
void ActionButtonPress ( ) {
switch ( GAME_STATE ) {
case GAMEWORLD : {
if ( PLAYER_COORDS [ 0 ] > = 8 & & PLAYER_COORDS [ 0 ] < 12 & &
PLAYER_COORDS [ 1 ] > = 2 & & PLAYER_COORDS [ 1 ] < 6 ) {
cout < < " You are standing over plant " < < getPlantId ( ( int ) PLAYER_COORDS [ 0 ] , ( int ) PLAYER_COORDS [ 1 ] ) < < " in state " < < getPlantStatus ( ( int ) PLAYER_COORDS [ 0 ] , ( int ) PLAYER_COORDS [ 1 ] ) ;
}
} break ;
}
}
bool OnUserUpdate ( float fElapsedTime ) override
{
elapsedTime + = fElapsedTime ;
@ -129,6 +210,35 @@ public:
if ( GetKey ( olc : : F1 ) . bPressed ) {
ConsoleShow ( olc : : F1 , false ) ;
}
if ( GetKey ( olc : : D ) . bPressed | | GetKey ( olc : : RIGHT ) . bPressed ) {
changeAnimation ( playerAnimWalkRight ) ;
}
if ( GetKey ( olc : : A ) . bPressed | | GetKey ( olc : : LEFT ) . bPressed ) {
changeAnimation ( playerAnimWalkLeft ) ;
}
if ( GetKey ( olc : : W ) . bPressed | | GetKey ( olc : : UP ) . bPressed ) {
changeAnimation ( playerAnimWalkUp ) ;
}
if ( GetKey ( olc : : S ) . bPressed | | GetKey ( olc : : DOWN ) . bPressed ) {
changeAnimation ( playerAnimWalkDown ) ;
}
if ( ! GetKey ( olc : : D ) . bHeld & & ! GetKey ( olc : : RIGHT ) . bHeld & &
! GetKey ( olc : : A ) . bHeld & & ! GetKey ( olc : : LEFT ) . bHeld & &
! GetKey ( olc : : S ) . bHeld & & ! GetKey ( olc : : UP ) . bHeld & &
! GetKey ( olc : : W ) . bHeld & & ! GetKey ( olc : : DOWN ) . bHeld ) {
if ( GetKey ( olc : : D ) . bReleased | | GetKey ( olc : : RIGHT ) . bReleased ) {
changeAnimation ( playerAnimRight ) ;
}
if ( GetKey ( olc : : A ) . bReleased | | GetKey ( olc : : LEFT ) . bReleased ) {
changeAnimation ( playerAnimLeft ) ;
}
if ( GetKey ( olc : : W ) . bReleased | | GetKey ( olc : : UP ) . bReleased ) {
changeAnimation ( playerAnim ) ;
}
if ( GetKey ( olc : : S ) . bReleased | | GetKey ( olc : : DOWN ) . bReleased ) {
changeAnimation ( playerAnimDown ) ;
}
}
drawGame ( ) ;
// called once per frame
@ -201,27 +311,45 @@ public:
}
}
}
current_playerAnim . frame = ( current_playerAnim . frame + 1 > = current_playerAnim . frames . size ( ) ) ? 0 : current_playerAnim . frame + 1 ;
if ( CURRENT_CUTSCENE = = NONE & & ! messageBoxVisible ) {
bool changed = false ;
if ( GetKey ( olc : : D ) . bHeld | | GetKey ( olc : : RIGHT ) . bHeld ) {
PLAYER_COORDS [ 0 ] = clamp ( PLAYER_COORDS [ 0 ] + MOVE_SPD , 0.1 , ( double ) MAP_WIDTH ) ;
ConsoleClear ( ) ;
cout < < " ( " < < PLAYER_COORDS [ 0 ] < < " , " < < PLAYER_COORDS [ 1 ] < < + " ) " ;
if ( ! changed & & & current_playerAnim ! = & playerAnimWalkRight ) {
changeAnimation ( playerAnimWalkRight ) ;
changed = true ;
}
}
if ( GetKey ( olc : : A ) . bHeld | | GetKey ( olc : : LEFT ) . bHeld ) {
PLAYER_COORDS [ 0 ] = clamp ( PLAYER_COORDS [ 0 ] - MOVE_SPD , 0.1 , ( double ) MAP_WIDTH ) ;
ConsoleClear ( ) ;
cout < < " ( " < < PLAYER_COORDS [ 0 ] < < " , " < < PLAYER_COORDS [ 1 ] < < + " ) " ;
if ( ! changed & & & current_playerAnim ! = & playerAnimWalkLeft ) {
changeAnimation ( playerAnimWalkLeft ) ;
changed = true ;
}
}
if ( GetKey ( olc : : W ) . bHeld | | GetKey ( olc : : UP ) . bHeld ) {
PLAYER_COORDS [ 1 ] = clamp ( PLAYER_COORDS [ 1 ] - MOVE_SPD , 0.1 , ( double ) MAP_HEIGHT ) ;
ConsoleClear ( ) ;
cout < < " ( " < < PLAYER_COORDS [ 0 ] < < " , " < < PLAYER_COORDS [ 1 ] < < + " ) " ;
if ( ! changed & & & current_playerAnim ! = & playerAnimWalkUp ) {
changeAnimation ( playerAnimWalkUp ) ;
changed = true ;
}
}
if ( GetKey ( olc : : S ) . bHeld | | GetKey ( olc : : DOWN ) . bHeld ) {
PLAYER_COORDS [ 1 ] = clamp ( PLAYER_COORDS [ 1 ] + MOVE_SPD , 0.1 , ( double ) MAP_HEIGHT ) ;
ConsoleClear ( ) ;
cout < < " ( " < < PLAYER_COORDS [ 0 ] < < " , " < < PLAYER_COORDS [ 1 ] < < + " ) " ;
if ( ! changed & & & current_playerAnim ! = & playerAnimWalkDown ) {
changeAnimation ( playerAnimWalkDown ) ;
changed = true ;
}
}
}
@ -394,7 +522,8 @@ public:
}
}
FillRectDecal ( { 0 , 0 } , { WIDTH , HEIGHT } , olc : : Pixel ( 0 , 0 , 0 , transparency ) ) ;
FillRectDecal ( { WIDTH / 2 - 2 , HEIGHT / 2 - 2 } , { 4 , 4 } , olc : : WHITE ) ;
//FillRectDecal({WIDTH/2-2,HEIGHT/2-2},{4,4},olc::WHITE);
DrawPartialDecal ( { WIDTH / 2 - 16 + ( current_playerAnim . flipped ? 32 : 0 ) , HEIGHT / 2 - 16 } , current_playerAnim . spr , current_playerAnim . getCurrentFrame ( ) , { 32 , 32 } , { current_playerAnim . flipped ? - 1 : 1 , 1 } ) ;
}
void DrawGameWorld ( ) {
@ -419,7 +548,7 @@ public:
for ( auto & obj : OBJECTS ) {
if ( obj . spr ! = NULL ) {
if ( obj . name . compare ( " PLANT " ) = = 0 ) {
DrawPartialDecal ( { ( obj . x - PLAYER_COORDS [ 0 ] ) * 32 + WIDTH / 2 , ( obj . y - PLAYER_COORDS [ 1 ] ) * 32 + HEIGHT / 2 } , { 32 , 32 } , obj . spr , { getPlantStatus ( obj ) * 32 , 0 } , { 32 , 32 } ) ;
DrawPartialDecal ( { ( obj . x - PLAYER_COORDS [ 0 ] ) * 32 + WIDTH / 2 , ( obj . y - PLAYER_COORDS [ 1 ] ) * 32 + HEIGHT / 2 } , { 32 , 32 } , obj . spr , { getPlantStatus ( obj . x , obj . y ) * 32 , 0 } , { 32 , 32 } ) ;
} else {
DrawDecal ( { ( obj . x - PLAYER_COORDS [ 0 ] ) * 32 + WIDTH / 2 , ( obj . y - PLAYER_COORDS [ 1 ] ) * 32 + HEIGHT / 2 } , obj . spr ) ;
}
@ -430,12 +559,12 @@ public:
}
}
int getPlantId ( Object obj ) {
return ( ( int ) obj . x - 8 ) % 4 + ( ( int ) obj . y - 2 ) * 4 ;
int getPlantId ( int x , int y ) {
return ( ( int ) x - 8 ) % 4 + ( ( int ) y - 2 ) * 4 ;
}
int getPlantStatus ( Object obj ) {
return plantState > > getPlantId ( obj ) * 2 & 0b11 ;
int getPlantStatus ( int x , int y ) {
return plantState > > getPlantId ( x , y ) * 2 & 0b11 ;
}
void DrawDialogBox ( const olc : : vi2d & pos , const olc : : vi2d & size , olc : : Pixel p = olc : : WHITE , olc : : Pixel p2 = olc : : DARK_GREY , olc : : Pixel p3 = olc : : VERY_DARK_GREY ) {
@ -531,6 +660,11 @@ public:
printf ( " \n " ) ;
}
}
void changeAnimation ( Animation anim ) {
current_playerAnim = anim ;
current_playerAnim . frame = 0 ;
}
} ;