⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mousecontroller.cpp

📁 《BATTLE OF SKY》
💻 CPP
字号:
#include <assert.h>
#include "mousecontroller.h"
#include "rect.h"
#include "direction.h"
#include "ray.h"
#include "bitmap.h"
#include <iostream>

using namespace std ;

GameController::GameController(const string &Title,
 const Position &WinPosition, const float WinLength,
 const float WinHeight) : Level(Slow),
 Status(SettingUp) , EnemyNumbers(3), Bomber(50) , Button(6) {

	// create a window and open it
	GameWindow = new SimpleWindow(Title,
	 WinLength, WinHeight, WinPosition);
	GetGameWindow()->Open();

    for(int j = 0 ; j < Button.size() ; ++j) {
		Button[j].SetWindow(*GetGameWindow()) ;
	}

	Display() ;
     
	// create the Enemys.
	// Note: this must be done AFTER the window is opened
	// because initialization of the Enemys depends on a window.
	KindOfEnemy.resize(NumberOfEnemyTypes);
	KindOfEnemy[Slow] = new SlowEnemy(*GetGameWindow());

	// create a fastEnemy
	KindOfEnemy[Fast] = new FastEnemy(*GetGameWindow());

	// create a puff
	Terminator = new Puff(*GetGameWindow()) ;
	Terminator->Draw() ;

    for(int i = 0 ; i < Bomber.size() ; ++i) {
	    Bomber[i] = new Bomb(*GetGameWindow() , Terminator->GetPosition() , Ready , Terminator->GetDirection());
	}
    
}

GameController::~GameController() {
	// get rid of the Enemys ,Puff and the window
	delete KindOfEnemy[Slow];
	delete KindOfEnemy[Fast];
	delete Terminator ;
	delete GameWindow;
	for(int i = 0 ; i < Bomber.size() ; ++i) {
		delete Bomber[i] ;
	}
}

// return the window that containing the game
SimpleWindow *GameController::GetGameWindow() {
	return GameWindow;
}


void GameController::Display() {
    RectangleShape BackGround(*GetGameWindow() , 10, 8 , Black , 20 , 16) ;
	BackGround.Draw() ;

	RectangleShape View(*GetGameWindow() , 8 , 8 , White , 16 , 16) ;
	View.Draw() ;
    
	RectangleShape Control(*GetGameWindow() , 18.25 , 5.0 , Blue , 4  , 9) ;
	Control.Draw() ;

    RectangleShape Shot(*GetGameWindow() ,18.25,13 , Blue , 4 , 4 ) ;
	Shot.Draw() ;
	
    GetGameWindow()->RenderText(Position(16.5 ,10.5 ) , Position(19 , 11) , "Battle Of Sky" , Black , Yellow ) ;
    
	Button[0].Load("bmp\\button_u.bmp") ;
	Button[0].SetPosition(Position(18.25-Button[0].GetWidth()/2, 1.0));
	Button[0].Draw() ;

    Button[1].Load("bmp\\button_r.bmp") ;
	Button[1].SetPosition(Position(18.25+Button[0].GetWidth()/2, 1.0+Button[0].GetHeight()));
	Button[1].Draw() ;

	Button[2].Load("bmp\\button_d.bmp") ;
	Button[2].SetPosition(Position(18.25-Button[0].GetWidth()/2, 1.0+2*Button[0].GetHeight()));
	Button[2].Draw() ;

    Button[3].Load("bmp\\button_l.bmp") ;
	Button[3].SetPosition(Position(18.25-3*Button[0].GetWidth()/2, 1.0+Button[0].GetHeight()));
	Button[3].Draw() ;

    Button[4].Load("bmp\\button_bomb.bmp") ;
	Button[4].SetPosition(Position(18.25-Button[0].GetWidth()/2,1.0+Button[0].GetHeight()));
	Button[4].Draw() ;

	Button[5].Load("bmp\\exit.bmp") ;
	Button[5].SetPosition(Position(18.25-Button[5].GetWidth()/2 , 12)) ;
	Button[5].Draw() ;

}

// reset the game at the beginning
void GameController::Reset() {
	Status = SettingUp;

    for(int j = 0 ; j < Bomber.size() ; ++j) {
		Bomber[j]->SetStatus(Ready) ;
	}

	EnemyNumbers = 3 ;
	Level  = Slow;
	CurrentEnemy()->SetPosition(Position(3.0 , 3.0)) ;
	CurrentEnemy()->Create();
	Terminator->Draw() ;
}

// start the game at the designed level
void GameController::Play(const GameLevel l) {
	Level = l;
	Status = Playing;
	GetGameWindow()->StartTimer(GameSpeed);
}

// check the oposition of the mouse click on the window
int GameController::MouseClick(const Position &MousePosition) {
	Position Temp = MousePosition ;

	//check if the mouse click on the current direction area of the pannel window
	if(Button[0].IsInside(Temp)) {
		Terminator->Erase() ;
		Terminator->SetDirection(Up) ;
	}

	else if(Button[1].IsInside(Temp)) {
        Terminator->Erase() ;
		Terminator->SetDirection(Right) ;
	}

    else if(Button[2].IsInside(Temp)) {
        Terminator->Erase() ;
		Terminator->SetDirection(Down) ;
	}

	else if(Button[3].IsInside(Temp)) {
        Terminator->Erase() ;
		Terminator->SetDirection(Left) ;
	}
	else if(Button[4].IsInside(Temp)) {
		for(int i = 0 ; i < Bomber.size() ;++i) {
			if(Bomber[i]->GetStatus() == Ready) {
				Direction Temp_d = Terminator->GetDirection() ;
				Position  Temp_p = Terminator->GetPosition() ;
				Bomber[i]->SetStatus(Flying) ;
				cout << '\007' ;

			
				switch(Temp_d) {
				case Up :
                    Bomber[i]->SetPosition(Temp_p+Position(Terminator->GetBmp(Temp_d).GetWidth()/2,0.0)) ;
					break ;
				case Right :
					Bomber[i]->SetPosition(Temp_p+Position(Terminator->GetBmp(Temp_d).GetWidth() , Terminator->GetBmp(Temp_d).GetHeight() / 2)) ;
					break ;
				case Down:
					Bomber[i]->SetPosition(Temp_p+Position(Terminator->GetBmp(Temp_d).GetWidth()/2 , Terminator->GetBmp(Temp_d).GetHeight() )) ;
				    break ;
				case Left:
					Bomber[i]->SetPosition(Temp_p+Position(0.0 , Terminator->GetBmp(Temp_d).GetHeight()/2)) ;
					break ;
				}

				Bomber[i]->SetDirection(Temp_d) ;
				break ;
			}
		}
	}

	else if(Button[5].IsInside(Temp)) {
		GetGameWindow()->Close() ;
		delete GetGameWindow() ;
	}
    return 1;
}

// check if the Game is over
int GameController::TimerTick() {
	// gamewon

    if (Status == GameWon) {
		GetGameWindow()->StopTimer();
		GetGameWindow()->Message("Mission Complete!");
		Reset();
		Play(Slow);
	}

	else if(Status == GameLost) {
		GetGameWindow()->StopTimer() ;
		GetGameWindow()->Message("Mission Failed!") ;
		Reset() ;
		Play(Slow) ;
	}
	// game is not over
	else{
        Terminator->Move() ;
	    CurrentEnemy()->Move();
		PuffHit() ;
	    for(int i = 0 ; i < Bomber.size() ; ++i) {
			if( Bomber[i]->GetStatus() == Flying ) {
				Bomber[i]->Move() ;
				cout << "\0x7h" << endl ;
				EnemyHit(Bomber[i]);
				if(Status == GameWon) {
			       break ;
				}
			}	
		}
	}
	
	return 1;
}

// Puff catches the Enemy
void GameController::EnemyHit( Bomb *b ) {

    /* check if the Enemy and the Puff touch each other
	 * If so, kill it and determine if the game is over
	 * if is not over advance to the next level of game
	 *************************/
	if (IsShot(b) == true) {
		b->SetStatus(Ready) ;
		b->Erase() ;
		CurrentEnemy()->Kill();
		-- EnemyNumbers ;

		if(EnemyNumbers <= 0) {
		   Level  = (GameLevel) (Level + 1);
		   if (Level == Done) {
		       Status = GameWon;
		   }
		   else {
		       EnemyNumbers = 2 ;
		   }
		}

		if(Status != GameWon) {
            if(Terminator->GetPosition().GetXDistance() <= 15.5/2) {
                CurrentEnemy()->SetPosition(Position(15.4-3 , 15.4-3)) ;
			}
			else {
				CurrentEnemy()->SetPosition(Position(2.0 , 2.0)) ;
			}

		    CurrentEnemy()->Create();
		}
	}
	
}

void GameController::PuffHit() {
	if(IsTouch()==true) {
		Terminator->Erase() ;
        if(CurrentEnemy()->GetPosition().GetXDistance() <= 15.5/2) {
                Terminator->SetPosition(Position(15.4-3 , 15.4-3)) ;
		}
		else {
				Terminator->SetPosition(Position(2.0 , 2.0)) ;
		}
		Status = GameLost ;
		CurrentEnemy()->Kill() ;
	}
}

// return the current level
GameLevel GameController::CurrentLevel() const {
	return Level;
}

// return a pointer to the current Enemy
Enemy *GameController::CurrentEnemy() const {
	return KindOfEnemy[CurrentLevel()];
}

// check if puff catch the Enemy
bool GameController::IsTouch() {

    // position of the Enemy and size of the bitmap of the Enemy
	float X1 = CurrentEnemy()->GetPosition().GetXDistance() ; 
	float Y1 = CurrentEnemy()->GetPosition().GetYDistance() ;
	float W1 = CurrentEnemy()->GetBmp(CurrentEnemy()->GetDirection()).GetWidth() ;
	float H1 = CurrentEnemy()->GetBmp(CurrentEnemy()->GetDirection()).GetHeight() ;

	// position of the Terminator and size of the bitmap of Terminator
	float X2 = Terminator->GetPosition().GetXDistance() ;
	float Y2 = Terminator->GetPosition().GetYDistance() ;
	float W2 = Terminator->GetBmp(Terminator->GetDirection()).GetWidth() ;
	float H2 = Terminator->GetBmp(Terminator->GetDirection()).GetHeight() ;
    
	// check if the Enemy and puff get touch
    if ((X1 >= X2) && (Y1 >= Y2)) {
		return ((X1-X2 <= W2) && (Y1-Y2 <= H2)) ;
	}
	else if ((X1 >= X2) && (Y1 <= Y2)) {
		return ((X1-X2 <= W2) && (Y2-Y1 <= H1)) ;
	}
	else if ((X1 <= X2) && (Y1 >= Y2)) {
		return ((X2-X1 <= W1) && (Y1-Y2 <= H2)) ;
	}
	else {
		return ((X2-X1 <= W1) && (Y2-Y1 <= H1)) ;
	}
}


// check if the puff catch the enemy
bool GameController::IsShot(const Bomb *b) {

	// position of the Enemy and size of the bitmap of the Enemy
	float X1 = CurrentEnemy()->GetPosition().GetXDistance() ; 
	float Y1 = CurrentEnemy()->GetPosition().GetYDistance() ;
	float W1 = CurrentEnemy()->GetBmp(CurrentEnemy()->GetDirection()).GetWidth() ;
	float H1 = CurrentEnemy()->GetBmp(CurrentEnemy()->GetDirection()).GetHeight() ;

    //the position of the Terminator and the size of the bitmap of Terminator
	float X2 = b->GetPosition().GetXDistance() ;
	float Y2 = b->GetPosition().GetYDistance() ;
	float W2 = b->GetBmp().GetWidth() ;
	float H2 = b->GetBmp().GetHeight() ;

    //check if the Enemy and puff get touch
    if ((X1 >= X2) && (Y1 >= Y2)) {
		return ((X1-X2 <= W2) && (Y1-Y2 <= H2)) ;
	}
	else if ((X1 >= X2) && (Y1 <= Y2)) {
		return ((X1-X2 <= W2) && (Y2-Y1 <= H1)) ;
	}
	else if ((X1 <= X2) && (Y1 >= Y2)) {
		return ((X2-X1 <= W1) && (Y1-Y2 <= H2)) ;
	}
	else {
		return ((X2-X1 <= W1) && (Y2-Y1 <= H1)) ;
	}
}


    


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -