playScene

mail@pastecode.io avatar
unknown
plain_text
23 days ago
5.0 kB
3
Indexable
Never
void PlayScene::ConstructUI() {
	// Background
	UIGroup->AddNewObject(new Engine::Image("play/sand.png", 1280, 0, 320, 832));
	// Text
	UIGroup->AddNewObject(new Engine::Label(std::string("Stage ") + std::to_string(MapId), "pirulen.ttf", 32, 1294, 0));
	UIGroup->AddNewObject(UIMoney = new Engine::Label(std::string("$") + std::to_string(money), "pirulen.ttf", 24, 1294, 48));
	UIGroup->AddNewObject(UILives = new Engine::Label(std::string("Life ") + std::to_string(lives), "pirulen.ttf", 24, 1294, 88));
	TurretButton* btn;
	// Button 1
	btn = new TurretButton("play/floor.png", "play/dirt.png",
		Engine::Sprite("play/tower-base.png", 1294, 136, 0, 0, 0, 0),
		Engine::Sprite("play/turret-1.png", 1294, 136 - 8, 0, 0, 0, 0)
		, 1294, 136, MachineGunTurret::Price);
	// Reference: Class Member Function Pointer and std::bind.
	btn->setButtonID(0);
	btn->SetOnClickCallback(std::bind(&PlayScene::UIBtnClicked, this, 0));
	UIGroup->AddNewControlObject(btn);
	// Button 2
	btn = new TurretButton("play/floor.png", "play/dirt.png",
		Engine::Sprite("play/tower-base.png", 1370, 136, 0, 0, 0, 0),
		Engine::Sprite("play/turret-2.png", 1370, 136 - 8, 0, 0, 0, 0)
		, 1370, 136, LaserTurret::Price);
	btn->setButtonID(1);
	btn->SetOnClickCallback(std::bind(&PlayScene::UIBtnClicked, this, 1));
	UIGroup->AddNewControlObject(btn);
	// Button 3
	btn = new TurretButton("play/floor.png", "play/dirt.png",
		Engine::Sprite("play/tower-base.png", 1446, 136, 0, 0, 0, 0),
		Engine::Sprite("play/turret-3.png", 1446, 136, 0, 0, 0, 0)
		, 1446, 136, MissileTurret::Price);
	btn->setButtonID(2);
	btn->SetOnClickCallback(std::bind(&PlayScene::UIBtnClicked, this, 2));
	UIGroup->AddNewControlObject(btn);
	// TODO: [CUSTOM-TURRET]: Create a button to support constructing the turret.
	// Button 4
	btn = new TurretButton("play/floor.png", "play/dirt.png",
		Engine::Sprite("play/tower-base.png", 1294, 212, 0, 0, 0, 0),
		Engine::Sprite("play/turret_new.png", 1309, 212, 0, 0, 0, 0)
		, 1294, 212, MachineGunTurret1::Price);
	btn->setButtonID(3);
	btn->SetOnClickCallback(std::bind(&PlayScene::UIBtnClicked, this, 3));
	UIGroup->AddNewControlObject(btn);
	// Button 5
	/*btn = new TurretButton("play/floor.png", "play/dirt.png",
		Engine::Sprite("play/tower-base.png", 1370, 212, 0, 0, 0, 0),
		Engine::Sprite("play/shovel.png", 1370, 212, 0, 0, 0, 0)
		, 1370, 212, Shovel::Price);
	btn->SetOnClickCallback(std::bind(&PlayScene::UIBtnClicked, this, 4));
	UIGroup->AddNewControlObject(btn);*/
	//PeaShooter
	btn = new TurretButton("play/grass-base.png", "play/grass-dirt.png",
		Engine::Sprite("play/plant-floor.png", 1370, 212, 0, 0, 0, 0),
		Engine::Sprite("play/Peashooter.png", 1370, 212, 0, 0, 0, 0)
		, 1370, 212, PeaShooter::Price);
	btn->setButtonID(5);
	btn->SetOnClickCallback(std::bind(&PlayScene::UIBtnClicked, this, 5));
	UIGroup->AddNewControlObject(btn);
	//Sunflower
	btn = new TurretButton("play/grass-base.png", "play/grass-dirt.png",
	Engine::Sprite("play/plant-floor.png", 1446, 212, 0, 0, 0, 0),
	Engine::Sprite("play/Sunflower.png", 1446, 212, 0, 0, 0, 0)
	, 1446, 212, SunFlower::Price);
	btn->setButtonID(6);
	btn->SetOnClickCallback(std::bind(&PlayScene::UIBtnClicked, this, 6));
	UIGroup->AddNewControlObject(btn);
	

	int w = Engine::GameEngine::GetInstance().GetScreenSize().x;
	int h = Engine::GameEngine::GetInstance().GetScreenSize().y;
	int shift = 135 + 25;
	dangerIndicator = new Engine::Sprite("play/benjamin.png", w - shift, h - shift);
	dangerIndicator->Tint.a = 0;
	UIGroup->AddNewObject(dangerIndicator);
}

void PlayScene::UIBtnClicked(int id) {
	//for peashooter cool_down time
	double currentTime = al_get_time();

	if (preview)
		UIGroup->RemoveObject(preview->GetObjectIterator());
    // TODO: [CUSTOM-TURRET]: On callback, create the turret.
	if (id == 0 && money >= MachineGunTurret::Price)
		preview = new MachineGunTurret(0, 0);
	else if (id == 1 && money >= LaserTurret::Price)
		preview = new LaserTurret(0, 0);
	else if (id == 2 && money >= MissileTurret::Price)
		preview = new MissileTurret(0, 0);
	else if (id == 3 && money >= MachineGunTurret1::Price)
		preview = new MachineGunTurret1(0, 0);
	else if (id == 4 && money >= Shovel::Price)
		preview = new Shovel(0, 0);
	else if(id == 5 && money >= PeaShooter::Price && PeaShooter::canPlaced(currentTime)) {
		preview = new PeaShooter(0, 0);
		PeaShooter::lastPlacedtime = currentTime;
	}
	else if(id == 6 && money >= SunFlower::Price && SunFlower::canPlaced(currentTime)) {
		preview = new SunFlower(0, 0);
		SunFlower::lastPlacedtime = currentTime;
	}
	if (!preview)
		return;
	preview->Position = Engine::GameEngine::GetInstance().GetMousePosition();
	preview->Tint = al_map_rgba(255, 255, 255, 200);
	preview->Enabled = false;
	preview->Preview = true;
	UIGroup->AddNewObject(preview);
	OnMouseMove(Engine::GameEngine::GetInstance().GetMousePosition().x, Engine::GameEngine::GetInstance().GetMousePosition().y);
}
Leave a Comment