Add in Transformed View.

CorrectiveAction
sigonasr2 1 year ago
parent 2a538d0aee
commit 2ec0daf59e
  1. 5
      olcCodeJam2023Entry/Constant.cpp
  2. 3
      olcCodeJam2023Entry/Constant.h
  3. 17
      olcCodeJam2023Entry/Unit.cpp
  4. 4
      olcCodeJam2023Entry/Unit.h
  5. 31
      olcCodeJam2023Entry/VirusAttack.cpp
  6. 8
      olcCodeJam2023Entry/VirusAttack.h
  7. BIN
      olcCodeJam2023Entry/assets/tile.png
  8. 2
      olcCodeJam2023Entry/olcPGEX_TransformedView.h

@ -10,4 +10,7 @@ Pixel CONSTANT::PROCEDURE_COLOR={212, 11, 162};
vf2d CONSTANT::UNSELECTED={-99,-99}; vf2d CONSTANT::UNSELECTED={-99,-99};
Renderable CONSTANT::VIRUS_IMG1; Renderable CONSTANT::VIRUS_IMG1;
Renderable CONSTANT::SELECTION_CIRCLE; Renderable CONSTANT::SELECTION_CIRCLE;
vi2d CONSTANT::TILE_SIZE={24,24};
vi2d CONSTANT::WORLD_SIZE={64,64};

@ -13,4 +13,7 @@ public:
static vf2d UNSELECTED; static vf2d UNSELECTED;
static Renderable VIRUS_IMG1,SELECTION_CIRCLE; static Renderable VIRUS_IMG1,SELECTION_CIRCLE;
static vi2d TILE_SIZE;
static vi2d WORLD_SIZE;
}; };

@ -66,7 +66,14 @@ Unit::Unit(std::vector<Memory>memory,vf2d pos,Renderable&img,bool friendly)
void Unit::Draw(PixelGameEngine*pge){ void Unit::Draw(TileTransformedView&game){
game.DrawRotatedDecal(pos,img.Decal(),0,img.Sprite()->Size()/2,{1,1},friendly?Pixel{192,192,255}:Pixel{255,192,192});
if(IsSelected()){
game.DrawRotatedDecal(pos,CONSTANT::SELECTION_CIRCLE.Decal(),0,CONSTANT::SELECTION_CIRCLE.Sprite()->Size()/2,vf2d(img.Sprite()->Size())/CONSTANT::SELECTION_CIRCLE.Sprite()->Size(),WHITE);
}
}
void Unit::DrawHud(TileTransformedView&game){
int initialBarX=pos.x-GetMemorySize()/2*CONSTANT::BAR_SQUARE_SIZE.x; int initialBarX=pos.x-GetMemorySize()/2*CONSTANT::BAR_SQUARE_SIZE.x;
int initialBarY=pos.y-CONSTANT::BAR_SQUARE_SIZE.y-img.Sprite()->height/2-2; int initialBarY=pos.y-CONSTANT::BAR_SQUARE_SIZE.y-img.Sprite()->height/2-2;
Pixel col=0; Pixel col=0;
@ -93,15 +100,11 @@ void Unit::Draw(PixelGameEngine*pge){
for(int i=0;i<GetMemorySize();i++){ for(int i=0;i<GetMemorySize();i++){
CheckColor(i,col); CheckColor(i,col);
pge->FillRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x, game.FillRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x,
float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,memory[i]?col:col/4); float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,memory[i]?col:col/4);
pge->DrawRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x, game.DrawRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x,
float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,BLACK); float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,BLACK);
} }
pge->DrawRotatedDecal(pos,img.Decal(),0,img.Sprite()->Size()/2,{1,1},friendly?Pixel{192,192,255}:Pixel{255,192,192});
if(IsSelected()){
pge->DrawRotatedDecal(pos,CONSTANT::SELECTION_CIRCLE.Decal(),0,CONSTANT::SELECTION_CIRCLE.Sprite()->Size()/2,vf2d(img.Sprite()->Size())/CONSTANT::SELECTION_CIRCLE.Sprite()->Size(),WHITE);
}
} }
int Unit::GetBits(Marker&m){ int Unit::GetBits(Marker&m){

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "olcPixelGameEngine.h" #include "olcPixelGameEngine.h"
#include "olcPGEX_TransformedView.h"
#include "Constant.h" #include "Constant.h"
struct Marker{ struct Marker{
@ -33,7 +34,8 @@ public:
std::vector<bool>memory; std::vector<bool>memory;
void Update(float fElapsedTime); void Update(float fElapsedTime);
virtual void Attack(Unit&victim)=0; virtual void Attack(Unit&victim)=0;
virtual void Draw(PixelGameEngine*pge); virtual void Draw(TileTransformedView&game);
virtual void DrawHud(TileTransformedView&game);
bool IsFriendly(); bool IsFriendly();
bool IsSelected(); bool IsSelected();
void Select(); void Select();

@ -2,8 +2,12 @@
#include "olcPixelGameEngine.h" #include "olcPixelGameEngine.h"
#define OLC_SOUNDWAVE #define OLC_SOUNDWAVE
#include "olcSoundWaveEngine.h" #include "olcSoundWaveEngine.h"
#include "VirusAttack.h" #define OLC_PGEX_TRANSFORMEDVIEW
#include "olcPGEX_TransformedView.h"
#include "olcUTIL_Geometry2D.h" #include "olcUTIL_Geometry2D.h"
#include "olcUTIL_Camera2D.h"
#include "VirusAttack.h"
VirusAttack::VirusAttack() VirusAttack::VirusAttack()
{ {
@ -12,10 +16,13 @@ VirusAttack::VirusAttack()
} }
bool VirusAttack::OnUserCreate(){ bool VirusAttack::OnUserCreate(){
game.Initialise(GetScreenSize());
CONSTANT::VIRUS_IMG1.Load("assets/unit.png"); CONSTANT::VIRUS_IMG1.Load("assets/unit.png");
CONSTANT::SELECTION_CIRCLE.Load("assets/selection_circle.png"); CONSTANT::SELECTION_CIRCLE.Load("assets/selection_circle.png");
TILE.Load("assets/tile.png",nullptr,false,false);
units.push_back(std::make_unique<BasicUnit>(vf2d{32,32},CONSTANT::VIRUS_IMG1,true)); units.push_back(std::make_unique<BasicUnit>(vf2d{32,32},CONSTANT::VIRUS_IMG1,true));
for(int i=0;i<10;i++){ for(int i=0;i<10;i++){
if(rand()%2==0){ if(rand()%2==0){
@ -33,11 +40,11 @@ void VirusAttack::HandleDraggingSelection(){
u->Deselect(); u->Deselect();
} }
if(startingDragPos==CONSTANT::UNSELECTED){ if(startingDragPos==CONSTANT::UNSELECTED){
startingDragPos=GetMousePos(); startingDragPos=GetWorldMousePos();
} }
} }
if(GetMouse(0).bReleased){ if(GetMouse(0).bReleased){
vf2d endDragPos=GetMousePos(); vf2d endDragPos=GetWorldMousePos();
if(endDragPos.x<startingDragPos.x){std::swap(startingDragPos.x,endDragPos.x);} if(endDragPos.x<startingDragPos.x){std::swap(startingDragPos.x,endDragPos.x);}
if(endDragPos.y<startingDragPos.y){std::swap(startingDragPos.y,endDragPos.y);} if(endDragPos.y<startingDragPos.y){std::swap(startingDragPos.y,endDragPos.y);}
geom2d::rect<float> selectionRegion(startingDragPos,endDragPos-startingDragPos); geom2d::rect<float> selectionRegion(startingDragPos,endDragPos-startingDragPos);
@ -52,9 +59,13 @@ void VirusAttack::HandleDraggingSelection(){
} }
} }
vf2d VirusAttack::GetWorldMousePos(){
return game.ScreenToWorld(GetMousePos());
}
void VirusAttack::DrawSelectionRectangle(){ void VirusAttack::DrawSelectionRectangle(){
if(startingDragPos!=CONSTANT::UNSELECTED){ if(startingDragPos!=CONSTANT::UNSELECTED){
FillRectDecal(startingDragPos,GetMousePos()-startingDragPos,{255,255,0,128}); game.FillRectDecal(startingDragPos,GetWorldMousePos()-startingDragPos,{255,255,0,128});
} }
} }
@ -64,7 +75,7 @@ void VirusAttack::HandleRightClickMove(){
for(auto&u:units){ for(auto&u:units){
if(!u->IsFriendly()){ if(!u->IsFriendly()){
geom2d::rect<float> unitRegion(u->GetPos()-u->GetUnitSize()/2,u->GetUnitSize()); geom2d::rect<float> unitRegion(u->GetPos()-u->GetUnitSize()/2,u->GetUnitSize());
if(geom2d::overlaps(unitRegion,GetMousePos())){ if(geom2d::overlaps(unitRegion,GetWorldMousePos())){
for(auto&u2:units){ for(auto&u2:units){
if(u2->IsFriendly()&&u2->IsSelected()){ if(u2->IsFriendly()&&u2->IsSelected()){
u2->SetTargetUnit(u); u2->SetTargetUnit(u);
@ -78,7 +89,7 @@ void VirusAttack::HandleRightClickMove(){
if(!selectedTarget){ if(!selectedTarget){
for(auto&u:units){ for(auto&u:units){
if(u->IsFriendly()&&u->IsSelected()){ if(u->IsFriendly()&&u->IsSelected()){
u->SetTargetLocation(GetMousePos()); u->SetTargetLocation(GetWorldMousePos());
} }
} }
} }
@ -110,6 +121,8 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){
HandleDraggingSelection(); HandleDraggingSelection();
HandleRightClickMove(); HandleRightClickMove();
game.HandlePanAndZoom();
for(auto&u:units){ for(auto&u:units){
Unit*closestUnit=nullptr; Unit*closestUnit=nullptr;
float closestDist=999999; float closestDist=999999;
@ -120,9 +133,13 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){
u->AttemptAttack(closestUnit); u->AttemptAttack(closestUnit);
u->Update(fElapsedTime); u->Update(fElapsedTime);
} }
game.DrawPartialDecal({0,0},CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE,TILE.Decal(),{0,0},CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE,DARK_GREEN);
for(auto&u:units){ for(auto&u:units){
u->Draw(this); u->Draw(game);
}
for(auto&u:units){
u->DrawHud(game);
} }
DrawSelectionRectangle(); DrawSelectionRectangle();

@ -1,5 +1,6 @@
#include "olcPixelGameEngine.h" #include "olcPixelGameEngine.h"
#include "olcSoundWaveEngine.h" #include "olcSoundWaveEngine.h"
#include "olcPGEX_TransformedView.h"
#include "Unit.h" #include "Unit.h"
#include "Constant.h" #include "Constant.h"
class VirusAttack : public olc::PixelGameEngine class VirusAttack : public olc::PixelGameEngine
@ -7,12 +8,19 @@ class VirusAttack : public olc::PixelGameEngine
private: private:
std::vector<std::shared_ptr<Unit>>units; std::vector<std::shared_ptr<Unit>>units;
Renderable TILE;
vf2d camera={213,160};
TileTransformedView game;
vf2d startingDragPos=CONSTANT::UNSELECTED; vf2d startingDragPos=CONSTANT::UNSELECTED;
void HandleDraggingSelection(); void HandleDraggingSelection();
void DrawSelectionRectangle(); void DrawSelectionRectangle();
void HandleRightClickMove(); void HandleRightClickMove();
void CollisionChecking(std::shared_ptr<Unit>u,std::shared_ptr<Unit>u2); void CollisionChecking(std::shared_ptr<Unit>u,std::shared_ptr<Unit>u2);
void IdentifyClosestTarget(Unit*&closestUnit,float&closestDist,std::shared_ptr<Unit>u,std::shared_ptr<Unit>u2); void IdentifyClosestTarget(Unit*&closestUnit,float&closestDist,std::shared_ptr<Unit>u,std::shared_ptr<Unit>u2);
vf2d GetWorldMousePos();
public: public:
VirusAttack(); VirusAttack();

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

@ -82,8 +82,6 @@
#include "olcPixelGameEngine.h" #include "olcPixelGameEngine.h"
namespace olc namespace olc
{ {
class TransformedView : public olc::PGEX class TransformedView : public olc::PGEX

Loading…
Cancel
Save