Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
jzeiber committed Nov 8, 2023
1 parent cb4eeef commit 62b226c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 29 deletions.
2 changes: 1 addition & 1 deletion bundle/index.html

Large diffs are not rendered by default.

Binary file modified cart/cart.wasm
Binary file not shown.
13 changes: 13 additions & 0 deletions src/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,19 @@ bool Game::DisbandUnit(const int8_t playerindex, const int32_t unitindex, const
return false;
}

int32_t Game::UnitCountAtLocation(const uint32_t x, const int32_t y) const
{
int32_t count=0;
for(size_t i=0; i<countof(m_gamedata.m_unit); i++)
{
if((m_gamedata.m_unit[i].flags & UNIT_ALIVE) == UNIT_ALIVE && m_gamedata.m_unit[i].x==x && m_gamedata.m_unit[i].y==y)
{
count++;
}
}
return count;
}

SpriteSheetPos Game::GetCitySpriteSheetPos(const int32_t cityidx) const
{
if(cityidx>=0 && cityidx<countof(m_gamedata.m_city) && m_gamedata.m_city[cityidx].population>0)
Expand Down
1 change: 1 addition & 0 deletions src/src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Game:public IUpdatable,public IDrawable,public IInputHandler
int32_t UnitEmbarkedShipIndex(const int32_t unitindex) const; // returns unit index of ship the unit is embarked on, or -1 if not embarked on a ship
bool MoveUnit(const uint8_t playerindex, const int32_t unitindex, const int32_t dx, const int32_t dy);
bool DisbandUnit(const int8_t playerindex, const int32_t unitindex, const bool killed);
int32_t UnitCountAtLocation(const uint32_t x, const int32_t y) const;

SpriteSheetPos GetCitySpriteSheetPos(const int32_t cityidx) const;

Expand Down
85 changes: 57 additions & 28 deletions src/src/stategame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ void StateGame::StateChanged(const uint8_t playerindex, const uint8_t prevstate,
}
// game has now started since at least 1 player is in the game state
m_game->GetGameData().m_gamestarted=true;
// set map position to a unit or city if no units alive
for(size_t i=0; i<countof(m_game->GetGameData().m_unit) && m_mapx==0 && m_mapy==0; i++)
{
if((m_game->GetGameData().m_unit[i].flags & UNIT_ALIVE)==UNIT_ALIVE && m_game->GetGameData().m_unit[i].owner==m_game->PlayerCivIndex(playerindex))
{
m_mapx=m_game->GetGameData().m_unit[i].x;
m_mapy=m_game->GetGameData().m_unit[i].y;
}
}
for(size_t i=0; i<countof(m_game->GetGameData().m_city) && m_mapx==0 && m_mapy==0; i++)
{
if(m_game->GetGameData().m_city[i].population>0 && m_game->GetGameData().m_city[i].owner==m_game->PlayerCivIndex(playerindex))
{
m_mapx=m_game->GetGameData().m_city[i].x;
m_mapy=m_game->GetGameData().m_city[i].y;
}
}
}
}

Expand Down Expand Up @@ -583,26 +600,6 @@ void StateGame::DrawMainView(const uint8_t playerindex)

if(dx>=-4 && dx<=4 && dy>=-4 && dy<=4)
{
/*
int8_t cidx=m_game->PlayerCivIndex(playerindex);
// sprite idx start position
int32_t sxidx=0+((cidx%2)*4);
int32_t syidx=13+(cidx/2);
if(c->population>4)
{
sxidx++;
}
if(c->population>8)
{
sxidx++;
}
if(c->population>12)
{
sxidx++;
}
*/

// sprite idx
SpriteSheetPos spos=m_game->GetCitySpriteSheetPos(i);

Expand Down Expand Up @@ -637,15 +634,22 @@ void StateGame::DrawMainView(const uint8_t playerindex)
}
}

// selected unit
Unit *su=nullptr;
if(m_selecttype==SELECT_UNIT && m_selectidx>=0 && m_selectidx<countof(m_game->GetGameData().m_unit))
{
su=&(m_game->GetGameData().m_unit[m_selectidx]);
}

// draw units
for(size_t i=0; i<countof(m_game->GetGameData().m_unit); i++)
{
// TODO - if other units are in same space as selected unit - don't show them
// TODO - only show selected unit if in city or embarked

Unit *u=&(m_game->GetGameData().m_unit[i]);
// don't show unit if it's in a city or embarked (select unit in a city will show later)
if((u->flags & UNIT_ALIVE) == UNIT_ALIVE && m_game->CityIndexAtLocation(u->x,u->y)<0 && m_game->UnitEmbarkedShipIndex(i)<0)
// don't show unit if it's in a city or embarked (select unit in a city will show later) or its in the same space as a selected unit
if((u->flags & UNIT_ALIVE) == UNIT_ALIVE && m_game->CityIndexAtLocation(u->x,u->y)<0 && m_game->UnitEmbarkedShipIndex(i)<0 && (!su || (su->x!=u->x || su->y!=u->y)))
{
// delta x,y between current pos on map
const int32_t dx=u->x-m_mapx;
Expand All @@ -667,6 +671,19 @@ void StateGame::DrawMainView(const uint8_t playerindex)
// if this is the selected unit - blink every .5 seconds (30 frames) (not selected unit - show always)
if(m_selecttype!=SELECT_UNIT || (m_selecttype==SELECT_UNIT && i!=m_selectidx) || (m_selecttype==SELECT_UNIT && m_selectidx==i && ((m_blinkticks/30)%2)==0))
{
if(u->owner!=m_game->PlayerCivIndex(playerindex))
{
*DRAW_COLORS=(PALETTE_CYAN << 4) | PALETTE_BROWN;
}
else
{
*DRAW_COLORS=(PALETTE_CYAN << 4) | PALETTE_WHITE;
}
if(m_game->UnitCountAtLocation(u->x,u->y)>1)
{
rect(sx-1,sy-1,16,16);
}
rect(sx,sy,16,16);
blitMasked(sprite,spritealpha,sx,sy,16,16,(spos.m_xidx*16),(spos.m_yidx*16),spritewidth,BLIT_2BPP);
}
if(m_showinfo && unitinfoidx<countof(unitinfo))
Expand Down Expand Up @@ -694,30 +711,42 @@ void StateGame::DrawMainView(const uint8_t playerindex)
}

// if we have a unit select - draw it again so it will be on top of everything else
if(m_selecttype==SELECT_UNIT && m_selectidx>=0 && m_selectidx<countof(m_game->GetGameData().m_unit))
if(su)
{
Unit *u=&(m_game->GetGameData().m_unit[m_selectidx]);
// selected unit info last so it's on top of everything else
if(m_showinfo)
{
selectedunitinfo=u;
selectedunitinfo=su;
}

if(((m_blinkticks/30)%2)==0)
{
// delta x,y between current pos on map
const int32_t dx=u->x-m_mapx;
const int32_t dy=u->y-m_mapy;
const int32_t dx=su->x-m_mapx;
const int32_t dy=su->y-m_mapy;

// sprite idx
SpriteSheetPos spos(unitdata[u->type].xidx,unitdata[u->type].yidx);
SpriteSheetPos spos(unitdata[su->type].xidx,unitdata[su->type].yidx);

// screen x,y
sx=((dx+4)*16)+16;
sy=(dy+4)*16;

if(sx>=0 && sy>=0 && sx<SCREEN_SIZE && sy<SCREEN_SIZE)
{
if(su->owner!=m_game->PlayerCivIndex(playerindex))
{
*DRAW_COLORS=(PALETTE_CYAN << 4) | PALETTE_BROWN;
}
else
{
*DRAW_COLORS=(PALETTE_CYAN << 4) | PALETTE_WHITE;
}
if(m_game->UnitCountAtLocation(su->x,su->y)>1)
{
rect(sx-1,sy-1,16,16);
}
rect(sx,sy,16,16);
blitMasked(sprite,spritealpha,sx,sy,16,16,(spos.m_xidx*16),(spos.m_yidx*16),spritewidth,BLIT_2BPP);
}
}
Expand Down

0 comments on commit 62b226c

Please sign in to comment.