Shoehorned in the button and text interface

This is an attempt to show that I can complete a specified set of
features. The code itself is an absolute mess, and I've always wanted to
refactor this project. For now though, it'll do as far as the portfolio
goes.
This commit is contained in:
Kayne Ruse
2013-09-06 21:06:07 +10:00
parent a97438a81b
commit 98a70e249c
3 changed files with 82 additions and 30 deletions

View File

@@ -186,7 +186,7 @@ void BaseEngine::RedrawScreen() {
}
void BaseEngine::AdditionalInit() {
SDL_WM_SetCaption("KAGE: Kayne's All-purpose Game Engine, By Kayne Ruse",NULL);
SDL_WM_SetCaption("Hearts",NULL);
}
int BaseEngine::TranslateMods(SDL_keysym ks) {

View File

@@ -27,11 +27,18 @@
*/
#include <iostream>
#include <time.h>
#include <sstream>
#include "hearts_engine.h"
using namespace std;
#define CASE break; case
string itos(int i) {
ostringstream ostr;
ostr << i;
return ostr.str();
}
//-------------------------
//Public access members
//-------------------------
@@ -46,8 +53,8 @@ HeartsEngine::HeartsEngine() {
button.LoadFontSurface("rsc\\pk_white_8.bmp");
font.LoadSurface("rsc\\pk_white_8.bmp");
// button.SetX();
// button.SetY();
button.SetX(300);
button.SetY(300);
player[0] = new PlayerUser();
player[1] = new PlayerAI();
@@ -84,8 +91,14 @@ HeartsEngine::~HeartsEngine() {
//Engine members
//-------------------------
void HeartsEngine::MouseButtonDown(SDL_MouseButtonEvent& button) {
BaseEngine::MouseButtonDown(button);
void HeartsEngine::MouseMotion(SDL_MouseMotionEvent& motion) {
BaseEngine::MouseMotion(motion);
button.MouseMotion(motion);
}
void HeartsEngine::MouseButtonDown(SDL_MouseButtonEvent& butt) {
BaseEngine::MouseButtonDown(butt);
button.MouseButtonDown(butt);
switch(gamePhase) {
CASE SWAP:
@@ -96,6 +109,11 @@ void HeartsEngine::MouseButtonDown(SDL_MouseButtonEvent& button) {
}
}
void HeartsEngine::MouseButtonUp(SDL_MouseButtonEvent& butt) {
BaseEngine::MouseButtonUp(butt);
button.MouseButtonUp(butt);
}
void HeartsEngine::Process() {
switch(gamePhase) {
CASE SETUP: SetupPhase();
@@ -113,6 +131,20 @@ void HeartsEngine::Draw() {
switch(gamePhase) {
case SETUP:
case SWAP:
if (player[0]->CheckSwapCards()) {
switch(rotation) {
case Rotation::LEFT:
button.SetText("Pass Left");
break;
case Rotation::RIGHT:
button.SetText("Pass Right");
break;
case Rotation::ACROSS:
button.SetText("Pass Across");
break;
}
button.DrawTo(screen);
}
case TRICK:
for (int i = 0; i < 4; i++)
player[i]->DrawHand(screen);
@@ -122,6 +154,28 @@ void HeartsEngine::Draw() {
case SCORE:
for (int i = 0; i < 4; i++)
player[i]->DrawTricks(screen);
button.SetText("Continue");
button.DrawTo(screen);
//I know this sucks
for (int i = 0; i < 4; i++) {
if (winner == i) {
font.DrawStringTo(
string() + "Player " + itos(i+1) + ": " + itos(player[i]->Score()) + " - " + itos(player[i]->Wins()) + " wins <-- Winner!!",
screen,
300,
200 + font.GetCharH() * i
);
}
else {
font.DrawStringTo(
string() + "Player " + itos(i+1) + ": " + itos(player[i]->Score()) + " - " + itos(player[i]->Wins()) + " wins",
screen,
300,
200 + font.GetCharH() * i
);
}
}
break;
}
}
@@ -166,7 +220,7 @@ void HeartsEngine::SetupPhase() {
}
void HeartsEngine::SwapPhase() {
if (!keyboard[SDLK_F1])
if (button.GetState() != Button::State::PRESSED)
return;
if (!player[0]->CheckSwapCards())
@@ -210,47 +264,41 @@ void HeartsEngine::TrickPhase() {
ResetPositions();
CalcScores();
gamePhase = SCORE;
for (int i = 0; i < 4; i++) {
cout << "Player " << i << ": " << player[i]->Score() << endl;//tmp...
}
cout << endl;
}
}
void HeartsEngine::ScorePhase() {
//end of game; temporary functionality...
if (player[0]->Score() >= 100 ||
if ((player[0]->Score() >= 100 ||
player[1]->Score() >= 100 ||
player[2]->Score() >= 100 ||
player[3]->Score() >= 100)
player[3]->Score() >= 100) && winner == -1)
{
cout << "GAME OVER" << endl;
int winner = 0;
winner = 0;
//find the winner
for (int i = 0; i < 4; i++) {
if (player[i]->Score() < player[winner]->Score()) {
winner = i;
}
}
cout << "Winner is: " << winner << endl;
cout << endl;
//award the winner
player[winner]->Wins(1);
for (int i = 0; i < 4; i++)
cout << "Player " << i << " wins: " << player[i]->Wins() << endl;
}
cout << endl;
//wait for input
if (button.GetState() == Button::State::PRESSED) {
gamePhase = CLEANUP;
//reset all values
//reset the game
if (winner != -1) {
winner = -1;
for (int i = 0; i < 4; i++)
player[i]->Score( -player[i]->Score() );
rotation = NONE;
}
//10 second delay
if (SDL_GetTicks() - timeTick > 10000 || keyboard[SDLK_F1])//button...
gamePhase = CLEANUP;
}
}
void HeartsEngine::CleanupPhase() {

View File

@@ -45,7 +45,9 @@ public:
~HeartsEngine();
private:
/* Engine members */
void MouseMotion (SDL_MouseMotionEvent& motion);
void MouseButtonDown (SDL_MouseButtonEvent& button);
void MouseButtonUp (SDL_MouseButtonEvent& button);
void Process ();
void Draw ();
@@ -88,6 +90,8 @@ private:
bool heartsBroken;
int timeTick;
int winner = -1;
/* Utility members */
void ResetPositions();
void ResetFaces();