Implement Memory Collection Point animations and units

This commit is contained in:
sigonasr2 2023-08-26 14:05:19 -05:00
parent 1d8f42283b
commit 74871ca4b5
15 changed files with 172 additions and 9 deletions

View File

@ -1,6 +1,8 @@
#pragma once
#include "olcPixelGameEngine.h"
#define PI 3.14159
class CONSTANT{
public:
static vf2d BAR_SQUARE_SIZE;

View File

@ -7,6 +7,8 @@ enum Image{
OUTLINE,
MINIMAP_OUTLINE,
VIRUS_IMG1,
SELECTION_CIRCLE
SELECTION_CIRCLE,
MATRIX,
MEMORY_COLLECTION_POINT,
};

View File

@ -4,8 +4,7 @@ Bit Repair
Memory Swapper
Corrupter (Randomly destroys bits)
Pipes
Attach collectors to them
"Memory Collection Point" Attach collectors to them (Unit attaches to pipe)
Memory Structure (Allocators)
RAM Bank (Creates new Memory Structures) has its own rate of creation based on Procedure amount.

View File

@ -3,6 +3,7 @@
#define OLC_PGEX_TRANSFORMEDVIEW
#include "olcUTIL_Geometry2D.h"
#include "TileManager.h"
#include "util.h"
#include "VirusAttack.h"
@ -22,10 +23,12 @@ void VirusAttack::InitializeImages(){
LoadImage(OUTLINE,"assets/outline.png");
LoadImage(VIRUS_IMG1,"assets/unit.png");
LoadImage(SELECTION_CIRCLE,"assets/selection_circle.png");
LoadImage(MEMORY_COLLECTION_POINT,"assets/memory_collection_point.png");
}
bool VirusAttack::OnUserCreate(){
SetPixelMode(Pixel::MASK);
game.Initialise(GetScreenSize());
InitializeImages();
@ -33,6 +36,10 @@ bool VirusAttack::OnUserCreate(){
IMAGES[MINIMAP_OUTLINE]=std::make_unique<Renderable>();
IMAGES[MINIMAP_OUTLINE]->Create(64,64);
IMAGES[MATRIX]=std::make_unique<Renderable>();
IMAGES[MATRIX]->Create(64,64,false,false);
IMAGES[MATRIX]->Sprite()->SetSampleMode(Sprite::PERIODIC);
units.push_back(std::make_unique<BasicUnit>(vf2d{32,32},*IMAGES[VIRUS_IMG1],true));
for(int i=0;i<10;i++){
if(rand()%2==0){
@ -41,6 +48,12 @@ bool VirusAttack::OnUserCreate(){
units.push_back(std::make_unique<BasicUnit2>(vf2d{float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},*IMAGES[VIRUS_IMG1],false));
}
}
for(int i=0;i<5;i++){
collectionPoints.push_back(std::make_unique<CollectionPoint>(this,vf2d{32.f+48*i,32.f},0,*IMAGES[MEMORY_COLLECTION_POINT],MemoryType(i)));
collectionPoints.push_back(std::make_unique<CollectionPoint>(this,vf2d{32.f,32.f+48*i},-PI/2,*IMAGES[MEMORY_COLLECTION_POINT],MemoryType(i)));
}
return true;
}
@ -136,11 +149,7 @@ void VirusAttack::DrawMinimap(){
vi2d worldPixelSize=CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE;
vf2d viewingTilesPct=vf2d{float(ScreenWidth()),float(ScreenHeight())}/CONSTANT::TILE_SIZE/CONSTANT::WORLD_SIZE;
SetDrawTarget(IMAGES[MINIMAP_OUTLINE]->Sprite());
for(int y=0;y<64;y++){
for(int x=0;x<64;x++){
Draw(x,y,BLANK);
}
}
Clear(BLANK);
DrawRect((game.GetWorldOffset()/worldPixelSize*64),viewingTilesPct*64/game.GetWorldScale());
for(auto&u:units){
FillRect(u->GetGhostPos()/worldPixelSize*64,vf2d{2,2}*u->GetUnitSize()/24,u->IsFriendly()?GREEN:RED);
@ -184,7 +193,59 @@ void VirusAttack::HandleMinimapClick(){
}
}
void VirusAttack::UpdateMatrixTexture(float fElapsedTime){
if(matrixTimer==0){
activeLetters.emplace_back(vf2d{float(rand()%64),float(64)},util::random(-40)-20,matrixLetters[rand()%matrixLetters.size()]);
matrixTimer=util::random(0.125);
}
if(updatePixelsTimer==0){
SetDrawTarget(IMAGES[MATRIX]->Sprite());
Sprite*img=IMAGES[MATRIX]->Sprite();
for(int y=63;y>=0;y--){
for(int x=63;x>=0;x--){
Pixel col=img->GetPixel(x,y);
if(col.r>0){
if(x>0){
Pixel leftCol=img->GetPixel(x-1,y);
if(leftCol.r<col.r){
leftCol=PixelLerp(col,leftCol,0.125);
}
Draw(x-1,y,leftCol);
}
if(x<img->width-1){
Pixel rightCol=img->GetPixel(x+1,y);
if(rightCol.r<col.r){
rightCol=PixelLerp(col,rightCol,0.125);
}
Draw(x+1,y,rightCol);
}
col/=8;
Draw(x,y,col);
}
}
}
for(int y=0;y<64;y++){
Draw({0,y},img->GetPixel(1,y));
}
SetDrawTarget(nullptr);
updatePixelsTimer=0.1;
}
if(activeLetters.size()>0){
SetDrawTarget(IMAGES[MATRIX]->Sprite());
for(Letter&letter:activeLetters){
letter.pos.y+=letter.spd*fElapsedTime;
DrawString(letter.pos,std::string(1,letter.c));
}
SetDrawTarget(nullptr);
IMAGES[MATRIX]->Decal()->Update();
}
matrixTimer=std::max(0.f,matrixTimer-fElapsedTime);
updatePixelsTimer=std::max(0.f,updatePixelsTimer-fElapsedTime);
std::erase_if(activeLetters,[](Letter&letter){return letter.pos.y<-32;});
}
bool VirusAttack::OnUserUpdate(float fElapsedTime){
UpdateMatrixTexture(fElapsedTime);
HandleDraggingSelection();
HandleRightClickMove();
HandlePanAndZoom(fElapsedTime);
@ -223,6 +284,34 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){
for(auto&u:units){
u->Draw(game,IMAGES);
}
for(auto&collectionPoint:collectionPoints){
collectionPoint->Update(this,*IMAGES[MATRIX]);
geom2d::rect<float>cpRect=geom2d::rect<float>({collectionPoint->pos-collectionPoint->img.Sprite()->Size()/2,collectionPoint->img.Sprite()->Size()});
geom2d::rect<float>viewRegion=geom2d::rect<float>({game.GetWorldTL(),game.GetWorldVisibleArea()});
if(geom2d::overlaps(cpRect,viewRegion)){
Pixel col;
switch(collectionPoint->type){
case HEALTH:{
col=CONSTANT::HEALTH_COLOR;
}break;
case RANGE:{
col=CONSTANT::RANGE_COLOR;
}break;
case ATKSPD:{
col=CONSTANT::ATKSPD_COLOR;
}break;
case MOVESPD:{
col=CONSTANT::MOVESPD_COLOR;
}break;
case PROCEDURE:{
col=CONSTANT::PROCEDURE_COLOR;
}break;
}
game.DrawRotatedDecal(collectionPoint->pos,collectionPoint->img.Decal(),collectionPoint->rot,collectionPoint->img.Sprite()->Size()/2,{1,1},col);
}
}
for(auto&u:units){
u->DrawHud(game,IMAGES);
}
@ -240,9 +329,35 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){
DrawMinimap();
DrawDecal({0,0},IMAGES[MATRIX]->Decal(),{1,1},{128,0,128});
return true;
}
VirusAttack::CollectionPoint::CollectionPoint(PixelGameEngine*pge,vf2d pos,float rot,Renderable&collectionPointImg,MemoryType type)
:pos(pos),rot(rot),type(type),originalCollectionPointImg(collectionPointImg.Sprite()),randomOffset({util::random(128),util::random(128)}){
img.Create(collectionPointImg.Sprite()->width,collectionPointImg.Sprite()->height);
pge->SetDrawTarget(img.Sprite());
pge->Clear(BLANK);
pge->DrawSprite({0,0},collectionPointImg.Sprite());
pge->SetDrawTarget(nullptr);
img.Decal()->Update();
}
void VirusAttack::CollectionPoint::Update(PixelGameEngine*pge,Renderable&matrixImg){
pge->SetDrawTarget(img.Sprite());
for(int y=0;y<img.Sprite()->height;y++){
for(int x=0;x<img.Sprite()->width;x++){
Pixel col=originalCollectionPointImg->GetPixel(x,y);
if(col==WHITE){
pge->Draw(x,y,matrixImg.Sprite()->GetPixel(int(x+randomOffset.x),int(y+randomOffset.y)));
}
}
}
img.Decal()->Update();
pge->SetDrawTarget(nullptr);
}
int main()
{
VirusAttack app;

View File

@ -5,15 +5,38 @@
#include "Constant.h"
#include "Image.h"
struct Letter{
vf2d pos;
float spd;
char c;
};
class VirusAttack : public olc::PixelGameEngine
{
class CollectionPoint{
public:
vf2d pos;
Renderable img;
Sprite*originalCollectionPointImg;
MemoryType type;
vf2d randomOffset;
float rot;
CollectionPoint(PixelGameEngine*pge,vf2d pos,float rot,Renderable&collectionPointImg,MemoryType type);
void Update(PixelGameEngine*pge,Renderable&matrixImg);
};
private:
std::vector<std::shared_ptr<Unit>>units;
std::vector<std::unique_ptr<CollectionPoint>>collectionPoints;
std::map<Image,std::unique_ptr<Renderable>>IMAGES;
TileTransformedView game;
float matrixTimer=0;
float updatePixelsTimer=0;
const std::array<char,16>matrixLetters={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F',};
std::vector<Letter>activeLetters;
vf2d startingDragPos=CONSTANT::UNSELECTED;
void HandleDraggingSelection();
void DrawSelectionRectangle();
@ -25,6 +48,7 @@ private:
void DrawMinimap();
void HandleMinimapClick();
void InitializeImages();
void UpdateMatrixTexture(float fElapsedTime);
public:
VirusAttack();

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 946 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

View File

@ -145,12 +145,14 @@
<ClInclude Include="resource1.h" />
<ClInclude Include="TileManager.h" />
<ClInclude Include="Unit.h" />
<ClInclude Include="util.h" />
<ClInclude Include="VirusAttack.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Constant.cpp" />
<ClCompile Include="TileManager.cpp" />
<ClCompile Include="Unit.cpp" />
<ClCompile Include="util.cpp" />
<ClCompile Include="VirusAttack.cpp" />
</ItemGroup>
<ItemGroup>

View File

@ -63,6 +63,9 @@
<ClInclude Include="Image.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="util.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="VirusAttack.cpp">
@ -77,6 +80,9 @@
<ClCompile Include="TileManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="util.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="olcCodeJam2023Entry.rc">

View File

@ -0,0 +1,8 @@
#include "util.h"
#include "olcPixelGameEngine.h"
namespace util{
float random(float range){
return float(rand())/RAND_MAX*range;
}
}

View File

@ -0,0 +1,5 @@
#pragma once
namespace util{
float random(float range);
}