Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "MyGame.h"
- CMyGame::CMyGame(void) :
- startScreen(480, 360, "startScreen.bmp", CColor::Blue(), 0),
- underlay(68, 582, "underlay.bmp", CColor::Blue(), 0),
- keep_practising(240, 678, "keep_practising.bmp", CColor::Blue(), 0),
- reach_the_goal(240, 677, "reach_the_goal.bmp", CColor::Blue(), 0),
- cover(362, 680, "cover.bmp", CColor::Blue(), 0),
- GUI(69, 582, "GUI.bmp", CColor::Blue(), 0),
- GUIclose(69, 703, "GUIclose.bmp", CColor::Blue(), 0),
- GUIbutton(120, 703, "GUIbutton2.bmp", CColor::Blue(), 0),
- BGMbutton(20, 703, "GUIbutton2.bmp", CColor::Blue(), 0),
- Gplus(115, 595, "plus2.bmp", CColor::Blue(), 0),
- Gminus(25, 595, "minus2.bmp", CColor::Blue(), 0),
- Tplus(115, 550, "plus2.bmp", CColor::Blue(), 0),
- Tminus(25, 550, "minus2.bmp", CColor::Blue(), 0),
- box_large(-300, -300, "box_large.bmp", CColor::Blue(), 0),
- box_small(-300, -300, "box_small.bmp", CColor::Blue(), 0)
- {
- GUIbutton.LoadImage("GUIbutton2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
- GUIbutton.LoadImage("GUIbutton2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
- BGMbutton.LoadImage("GUIbutton2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
- BGMbutton.LoadImage("GUIbutton2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
- Gplus.LoadImage("plus2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
- Gplus.LoadImage("plus2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
- Gminus.LoadImage("minus2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
- Gminus.LoadImage("minus2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
- Tplus.LoadImage("plus2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
- Tplus.LoadImage("plus2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
- Tminus.LoadImage("minus2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
- Tminus.LoadImage("minus2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
- }
- CMyGame::~CMyGame(void)
- {
- }
- void CMyGame::StarsControl(Uint16 x, Uint16 y) // on-click function - star spawn, attributes, aiming, trajectory
- {
- color_coordinator = CColor(rand() % 155 + 100, rand() % 155 + 100, rand() % 155 + 100);
- CSprite *pStar = new CSpriteOval(CVector(x, y), 3, color_coordinator, GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100);
- pStar->SetStatus(stars.size());
- initial_aim = CVector(x, y);
- if (pStar->HitTest(x, y))
- {
- CSprite *pStarclick = new CSpriteRect(x, y, 7, 7, CColor::DarkRed(), GetTime()); // small sprite to show player where they initially clicked
- starclick.push_back(pStarclick);
- s_Aiming = true;
- }
- for (CSprite *pTrajectory : trajectory) delete pTrajectory; // deleted previous star's trajectory : memory efficient
- trajectory.clear();
- }
- void CMyGame::LevelControl(int a) // game rules to be called on specific levels
- {
- if (a == 2)
- {
- if (stars.size() == 15 && s_Aiming == false)
- {
- SetLevel(3);
- levelup.Play("levelup.wav");
- }
- }
- else if (a == 3)
- {
- for (CSprite* pStar : stars) if (pStar->GetStatus() == 17 && pStar->IsDeleted())
- {
- SetLevel(12);
- gameover.Play("gameover.wav");
- BGM.Stop();
- }
- }
- else if (a == 4)
- {
- for (CSprite* pStar : stars)
- {
- if (pStar->GetStatus() == 16 && pStar->IsDeleted())
- {
- SetLevel(12);
- gameover.Play("gameover.wav");
- BGM.Stop();
- }
- if (pStar->GetMass() > 99999 && pStar->GetX() < 100) pStar->SetVelocity(100, 0);
- if (pStar->GetMass() > 99999 && pStar->GetX() > 860) pStar->SetVelocity(-100, 0);
- }
- }
- else if (a == 5)
- {
- for (CSprite* pStar : stars)
- {
- if (pStar->GetStatus() == 16 && pStar->IsDeleted())
- {
- SetLevel(12);
- gameover.Play("gameover.wav");
- BGM.Stop();
- }
- if (pStar->GetMass() > 99999 && pStar->GetX() < 100) pStar->SetVelocity(100, 0);
- if (pStar->GetMass() > 99999 && pStar->GetX() > 860) pStar->SetVelocity(-100, 0);
- }
- for (CSprite *pGoal : goal)
- {
- if (pGoal->GetX() < 100) pGoal->SetVelocity(100, 0);
- if (pGoal->GetX() > 860) pGoal->SetVelocity(-100, 0);
- }
- }
- else if (a == 6)
- {
- for (CSprite* pStar : stars) if (pStar->GetStatus() == 15 && pStar->IsDeleted())
- {
- SetLevel(10);
- gameover.Play("gameover.wav");
- BGM.Stop();
- }
- if (box_small.GetX() > 860) box_small.SetVelocity(0, 100);
- if (box_small.GetY() > 620) box_small.SetVelocity(-100, 0);
- if (box_small.GetX() < 100) box_small.SetVelocity(0, -100);
- if (box_small.GetY() < 100) box_small.SetVelocity(100, 0);
- if (box_small.GetX() > 1060)
- {
- SetLevel(12);
- gameover.Play("gameover.wav");
- BGM.Stop();
- }
- }
- else if (a == 7)
- {
- for (CSprite* pStar : stars)
- {
- if (pStar->GetStatus() == 18 && pStar->IsDeleted())
- {
- SetLevel(12);
- gameover.Play("gameover.wav");
- BGM.Stop();
- }
- if (pStar->GetMass() > 99999 && pStar->GetY() < 100) pStar->SetVelocity(0, 100);
- if (pStar->GetMass() > 99999 && pStar->GetY() > 620) pStar->SetVelocity(0, -100);
- }
- }
- else if (a == 8)
- {
- for (CSprite* pStar : stars)
- {
- if (pStar->GetStatus() == 16 && pStar->IsDeleted())
- {
- SetLevel(12);
- gameover.Play("gameover.wav");
- BGM.Stop();
- }
- if (pStar->GetMass() > 99999 && pStar->GetX() > 620)
- {
- pStar->SetX(620);
- pStar->SetVelocity(0, -100);
- }
- else if (pStar->GetMass() > 99999 && pStar->GetY() > 520)
- {
- pStar->SetY(520);
- pStar->SetVelocity(100, 0);
- }
- else if (pStar->GetMass() > 99999 && pStar->GetX() < 300)
- {
- pStar->SetX(300);
- pStar->SetVelocity(0, 100);
- }
- else if (pStar->GetMass() > 99999 && pStar->GetY() < 200)
- {
- pStar->SetY(200);
- pStar->SetVelocity(-100, 0);
- }
- }
- for (CSprite *pGoal : goal)
- {
- if (pGoal->GetX() < 100)
- {
- pGoal->SetX(100);
- pGoal->SetVelocity(0, -100);
- }
- else if (pGoal->GetX() > 860)
- {
- pGoal->SetX(860);
- pGoal->SetVelocity(0, 100);
- }
- else if (pGoal->GetY() > 620)
- {
- pGoal->SetY(620);
- pGoal->SetVelocity(-100, 0);
- }
- else if (pGoal->GetY() < 100)
- {
- pGoal->SetY(100);
- pGoal->SetVelocity(100, 0);
- }
- }
- }
- else if (a == 9)
- {
- for (CSprite* pStar : stars) if (pStar->GetStatus() == 17 && pStar->IsDeleted())
- {
- SetLevel(12);
- gameover.Play("gameover.wav");
- BGM.Stop();
- }
- }
- else if (a == 10)
- {
- for (CSprite* pStar : stars) if (pStar->GetStatus() == 16 && pStar->IsDeleted())
- {
- SetLevel(12);
- gameover.Play("gameover.wav");
- BGM.Stop();
- }
- }
- }
- void CMyGame::GameRules() // general game rules, collision detection, GUI control applied every level
- {
- for (CSprite *pStar : stars)
- {
- if (pStar->GetX() > 960 || pStar->GetX() < 0)
- {
- pStar->SetMass(0);
- pStar->Delete();
- }
- if (pStar->GetY() > 720 || pStar->GetY() < 0)
- {
- pStar->SetMass(0);
- pStar->Delete();
- }
- if (s_Trajectory)
- {
- if (pStar->GetStatus() == stars.size() && pStar->GetMass() < 100000)
- {
- if (!pStar->IsDeleted())
- {
- CSprite* pTrajectory = new CSpriteOval(pStar->GetPosition(), 1, color_coordinator, GetTime());
- trajectory.push_back(pTrajectory);
- }
- }
- }
- else if (s_Trajectory == false)
- {
- for (CSprite *pTrajectory : trajectory) delete pTrajectory;
- trajectory.clear();
- }
- for (CSprite* pGoal : goal) if (pStar->HitTest(pGoal))
- {
- NewLevel();
- if (GetLevel() != 10) levelup.Play("levelup.wav");
- else
- {
- BGM.Stop();
- win.Play("win.wav");
- }
- }
- }
- for (CSprite *pExplode : explode)
- {
- pExplode->SetStatus(pExplode->GetStatus() - 1);
- if (pExplode->GetStatus() <= 0) pExplode->Delete();
- }
- if (starclick.size() == 2 && s_Aiming == false) starclick.pop_front();
- if (starclickup.size() == 2 && s_Aiming == false) starclickup.pop_front();
- if (stars.size() == 19) stars.clear();
- if (GetLevel() != 6) box_small.SetSpeed(0);
- if (keep_practising.GetX() < -150) keep_practising.SetPosition(240, 678);
- if (reach_the_goal.GetX() < -150) reach_the_goal.SetPosition(240, 677);
- reach_the_goal.SetXVelocity(-30);
- keep_practising.SetXVelocity(-30);
- }
- void CMyGame::CalculateDistance() // get the numerical value of distance from one star to all others, and store in array
- {
- for each (CSprite *pStar in stars) each_star_pos[pStar->GetStatus()] = pStar->GetPos();
- for (CSprite *pStar : stars)
- {
- if (pStar->GetStatus() != stars.size() || pStar->GetStatus() == stars.size() && s_Aiming == false)
- {
- for (int i = 0; i < stars.size() + 1; i++)
- if (i != pStar->GetStatus())
- each_star_dist[pStar->GetStatus()][i] = Distance(pStar->GetPos(), each_star_pos[i]);
- }
- }
- }
- void CMyGame::CalculateForce() // with known distance and mass calculate force applied to other stars. Newton's law of universal gravity: F = G (Mm / (r * r))
- {
- for each (CSprite *pStar in stars) each_star_mass[pStar->GetStatus()] = pStar->GetMass();
- for (CSprite *pStar : stars)
- {
- if (pStar->GetStatus() != stars.size() || pStar->GetStatus() == stars.size() && s_Aiming == false)
- {
- for (int i = 0; i < stars.size() + 1; i++)
- if (i != pStar->GetStatus())
- each_star_G[pStar->GetStatus()][i] = G * (pStar->GetMass() * each_star_mass[i]) / (each_star_dist[pStar->GetStatus()][i] * each_star_dist[pStar->GetStatus()][i]);
- }
- }
- }
- void CMyGame::ApplyForces() // accelerate stars by normal displacement of other stars for direction and apply Newton's second law: A = F / M
- {
- for (CSprite *pStar : stars)
- {
- if (pStar->GetStatus() != stars.size() || pStar->GetStatus() == stars.size() && s_Aiming == false)
- {
- for (int i = 0; i < stars.size() + 1; i++)
- {
- if (pStar->GetMass() < 99999) // UPDATE: large yellow stars are static, only stars with less mass will accelerate, for playability
- if (i != pStar->GetStatus() && each_star_dist[pStar->GetStatus()][i] >= 10)
- pStar->Accelerate((Normalize(each_star_pos[i] - pStar->GetPos())) * each_star_G[i][pStar->GetStatus()] / pStar->GetMass());
- if (i == pStar->GetStatus()) continue;
- if (each_star_dist[pStar->GetStatus()][i] <= 10 && pStar->GetMass() == 100)
- {
- CSprite *pExplode = new CSpriteOval(pStar->GetPosition(), 7, CColor::Red(), GetTime());
- explode.push_back(pExplode);
- pExplode->SetStatus(15);
- // --- IMPORTANT: resetting mass and pos of deleted star to keep increasing size of star list, which is necessary for physics calculations and game logic
- pStar->SetPosition(rand() % 100000 +1000, rand() % 100000 + 1000); // BUG FIX: less chance of stars' distance being <= 10 thus causing all stars to be deleted (previously: pStar->SetPosition(0, 0) when star collision)
- pStar->SetMass(0);
- pStar->Delete();
- explodes.Play("explode.wav");
- }
- }
- }
- }
- }
- inline void CMyGame::SmallBoxFunc(Uint16 x, Uint16 y)
- {
- if (s_Aiming && !box_small.HitTest(x, y))
- {
- s_Aiming = false;
- CSprite *pStarclickup = new CSpriteRect(x, y, 7, 7, CColor::White(), GetTime());
- starclickup.push_back(pStarclickup);
- }
- }
- inline void CMyGame::LargeBoxFunc(Uint16 x, Uint16 y)
- {
- if (s_Aiming && !box_large.HitTest(x, y))
- {
- s_Aiming = false;
- CSprite *pStarclickup = new CSpriteRect(x, y, 7, 7, CColor::White(), GetTime());
- starclickup.push_back(pStarclickup);
- }
- }
- void CMyGame::OnUpdate()
- {
- // physics
- CalculateDistance();
- CalculateForce();
- ApplyForces();
- // general
- GameRules();
- // level control
- if (GetLevel() == 2) LevelControl(2);
- else if (GetLevel() == 3) LevelControl(3);
- else if (GetLevel() == 4) LevelControl(4);
- else if (GetLevel() == 5) LevelControl(5);
- else if (GetLevel() == 6) LevelControl(6);
- else if (GetLevel() == 7) LevelControl(7);
- else if (GetLevel() == 8) LevelControl(8);
- else if (GetLevel() == 9) LevelControl(9);
- else if (GetLevel() == 10) LevelControl(10);
- // misc
- if (GetLevel() == 12 || GetLevel() == 11) GUIbool = false;
- long t = GetTime();
- for (CSprite *pStar : stars) pStar->Update(t);
- for (CSprite *pStarclick : starclick) pStarclick->Update(t);
- for (CSprite *pStarclickup : starclickup) pStarclickup->Update(t);
- for (CSprite *pExplode : explode) pExplode->Update(t);
- if (s_Trajectory) for (CSprite *pTrajectory : trajectory) pTrajectory->Update(t);
- for (CSprite *pGoal : goal) pGoal->Update(t);
- keep_practising.Update(t);
- reach_the_goal.Update(t);
- box_small.Update(t);
- trajectory.remove_if(deleted);
- explode.remove_if(deleted);
- }
- void CMyGame::OnDraw(CGraphics* g)
- {
- if (GetLevel() != 12 && GetLevel() != 11 && GetLevel() != 13)
- {
- if (GUIbool)
- {
- underlay.Draw(g);
- if (GetLevel() == 2) keep_practising.Draw(g);
- else if (GetLevel() != 2) reach_the_goal.Draw(g);
- cover.Draw(g);
- }
- if (GetLevel() == 3) box_large.Draw(g);
- if (GetLevel() == 4 || GetLevel() == 5 || GetLevel() == 6 || GetLevel() == 7 || GetLevel() == 8 || GetLevel() == 9 || GetLevel() == 10) box_small.Draw(g);
- if (s_Trajectory) for (CSprite *pTrajectory : trajectory) pTrajectory->Draw(g);
- for (CSprite *pStar : stars) pStar->Draw(g);
- for (CSprite *pStarclick : starclick) pStarclick->Draw(g);
- for (CSprite *pStarclickup : starclickup) pStarclickup->Draw(g);
- for (CSprite *pExplode : explode) pExplode->Draw(g);
- for (CSprite *pGoal : goal) pGoal->Draw(g);
- if (GUIbool)
- {
- GUI.Draw(g);
- Gplus.Draw(g);
- Gminus.Draw(g);
- Tplus.Draw(g);
- Tminus.Draw(g);
- }
- else GUIclose.Draw(g);
- GUIbutton.Draw(g);
- BGMbutton.Draw(g);
- }
- // text drawing
- if (GUIbool)
- {
- *g << font(16) << color(CColor::Black()) << xy(55, 587) << G;
- if (s_Trajectory) *g << font(16) << color(CColor::Green()) << xy(57, 542) << "ON";
- else *g << font(16) << color(CColor::Red()) << xy(57, 542) << "OFF";
- *g << font(16) << color(CColor::Black()) << xy(40, 500) << each_star_G[1][stars.size()];
- *g << font(16) << color(CColor::Black()) << xy(40, 458) << each_star_dist[1][stars.size()];
- }
- if (GetLevel() == 1) // BUG FIX: level 1 is 'menu mode', necessary because the choice of 'practise' or 'begin' on title screen would not allow SetLevel(). Fixed by not using MenuMode().
- {
- startScreen.Draw(g);
- return;
- }
- if (GetLevel() == 2)
- {
- if (GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 15 - stars.size();
- if (stars.size() == 0)
- *g << font(30) << color(CColor::White()) << top << center << endl << endl << endl << "right click to add a large star";
- if (stars.size() == 1)
- *g << font(30) << color(CColor::White()) << top << center << endl << endl << endl << "left click and drag to push a smaller star";
- if (stars.size() > 2 && stars.size() <= 5)
- *g << font(22) << color(CColor::White()) << top << center << endl << endl << endl << endl << "minimise and maximise GUI with the top right button" << endl << "on the GUI, pause and resume BGM on the top left";
- if (stars.size() > 6 && stars.size() <= 9)
- *g << font(22) << color(CColor::White()) << top << center << endl << endl << endl << endl << "stars are deleted if they go outside the game space"
- << endl << "or if they collide";
- if (stars.size() > 10 && stars.size() <= 12)
- *g << font(30) << color(CColor::White()) << top << center << endl << endl << endl << endl << "there are 8 levels to complete";
- if (stars.size() == 13 && s_Aiming == false)
- *g << font(30) << color(CColor::White()) << top << center << endl << endl << endl << "level 1 will begin shortly";
- }
- else if (GetLevel() == 3)
- {
- if (GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 17 - stars.size();
- if (stars.size() == 2)
- *g << font(22) << color(CColor::White()) << top << center << endl << endl << endl << "you can only push stars within the dotted box"
- << endl << "if you go outside of the box while aiming, the star will be dropped" << endl <<
- "the grey circle is your goal";
- }
- else if (GetLevel() == 4 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 16 - stars.size();
- else if (GetLevel() == 5 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 16 - stars.size();
- else if (GetLevel() == 6 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 15 - stars.size();
- else if (GetLevel() == 7 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 18 - stars.size();
- else if (GetLevel() == 8 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 16 - stars.size();
- else if (GetLevel() == 9 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 17 - stars.size();
- else if (GetLevel() == 10 && GUIbool) *g << font(18) << color(CColor::White()) << xy(58, 628) << 16 - stars.size();
- else if (GetLevel() == 11) *g << font(80) << color(CColor::White()) << vcenter << center << "YOU WIN!";
- else if (GetLevel() == 12)
- {
- *g << font(80) << color(CColor::White()) << vcenter << center << "GAME OVER";
- *g << font(30) << color(CColor::White()) << vcenter << center << endl << endl << "F2 to restart";
- }
- else if (GetLevel() == 13)
- {
- *g << font(80) << color(CColor::White()) << top << center << endl << endl << endl << "CAN YOU\nCOMPLETE THEM ALL?";
- GUIbool = false;
- }
- }
- void CMyGame::OnInitialize()
- {
- }
- void CMyGame::OnDisplayMenu()
- {
- StartGame();
- }
- void CMyGame::OnStartGame()
- {
- BGM.Play("BGM.wav", -1, 3000);
- BGMbool = true;
- GUIbool = true;
- s_Aiming = false;
- s_Trajectory = true;
- BGMbutton.SetImage("release");
- GUIbutton.SetImage("release");
- Gplus.SetImage("release");
- Gminus.SetImage("release");
- Tplus.SetImage("release");
- Tminus.SetImage("release");
- }
- void CMyGame::OnStartLevel(Sint16 nLevel)
- {
- for (CSprite *pTrajectory : trajectory) delete pTrajectory;
- trajectory.clear();
- for (CSprite *pStar : stars) delete pStar;
- stars.clear();
- for (CSprite *pStarclick : starclick) delete pStarclick;
- starclick.clear();
- for (CSprite *pStarclickup : starclickup) delete pStarclickup;
- starclickup.clear();
- for (CSprite *pExplode : explode) delete pExplode;
- explode.clear();
- for (CSprite *pGoal : goal) delete pGoal;
- goal.clear();
- switch (nLevel)
- {
- case 3:
- goal.push_back(new CSpriteOval(CVector(860, 620), 40, 40, CColor::LightGray(), GetTime()));
- box_large.SetPosition(100, 100);
- {
- CSprite *pStar = new CSpriteOval(CVector(740, 500), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetStatus(stars.size());
- }
- {
- CSprite *pStar = new CSpriteOval(CVector(400, 350), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetStatus(stars.size());
- }
- break;
- case 4:
- goal.push_back(new CSpriteOval(CVector(480, 50), 40, 40, CColor::LightGray(), GetTime()));
- box_small.SetPosition(480, 625);
- {
- CSprite *pStar = new CSpriteOval(CVector(480, 360), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetVelocity(100, 0);
- pStar->SetStatus(stars.size());
- }
- break;
- case 5:
- box_small.SetPosition(480, 625);
- {
- CSprite *pGoal = new CSpriteOval(CVector(480, 50), 40, 40, CColor::LightGray(), GetTime());
- pGoal->SetVelocity(-100, 0);
- goal.push_back(pGoal);
- }
- {
- CSprite *pStar = new CSpriteOval(CVector(480, 360), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetVelocity(100, 0);
- pStar->SetStatus(stars.size());
- }
- break;
- case 6:
- box_small.SetPosition(860, 100);
- box_small.SetSpeed(100);
- goal.push_back(new CSpriteOval(CVector(480, 360), 40, 40, CColor::LightGray(), GetTime()));
- break;
- case 7:
- box_small.SetPosition(860, 360);
- goal.push_back(new CSpriteOval(CVector(180, 360), 40, 40, CColor::LightGray(), GetTime()));
- {
- CSprite *pStar = new CSpriteOval(CVector(480, 360), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetVelocity(0, 100);
- pStar->SetStatus(stars.size());
- }
- {
- CSprite *pStar = new CSpriteOval(CVector(380, 360), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetVelocity(0, -100);
- pStar->SetStatus(stars.size());
- }
- {
- CSprite *pStar = new CSpriteOval(CVector(580, 360), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetVelocity(0, -100);
- pStar->SetStatus(stars.size());
- }
- break;
- case 8:
- box_small.SetPosition(460, 360);
- {
- CSprite *pGoal = new CSpriteOval(CVector(100, 100), 40, 40, CColor::LightGray(), GetTime());
- pGoal->SetVelocity(100, 0);
- goal.push_back(pGoal);
- }
- {
- CSprite *pStar = new CSpriteOval(CVector(620, 200), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetVelocity(-100, 0);
- pStar->SetStatus(stars.size());
- }
- break;
- case 9:
- box_small.SetPosition(860, 100);
- goal.push_back(new CSpriteOval(CVector(100, 100), 40, 40, CColor::LightGray(), GetTime()));
- {
- CSprite *pStar = new CSpriteOval(CVector(460, 200), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetStatus(stars.size());
- }
- {
- CSprite *pStar = new CSpriteOval(CVector(460, 520), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetStatus(stars.size());
- }
- break;
- case 10:
- box_small.SetPosition(460, 645);
- goal.push_back(new CSpriteOval(CVector(460, 50), 20, 20, CColor::LightGray(), GetTime()));
- {
- CSprite *pStar = new CSpriteOval(CVector(460, 360), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetStatus(stars.size());
- }
- break;
- }
- }
- void CMyGame::OnGameOver()
- {
- }
- void CMyGame::OnTerminate()
- {
- }
- // Keyboard Event Handlers
- void CMyGame::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode)
- {
- if (sym == SDLK_F4 && (mod & (KMOD_LALT | KMOD_RALT)))
- StopGame();
- if (sym == SDLK_SPACE)
- PauseGame();
- if (sym == SDLK_F2)
- NewGame();
- if (sym == SDLK_BACKSPACE) for (CSprite* pStar : stars) if (pStar->GetMass() == 100000)
- {
- pStar->Delete();
- pStar->SetMass(0);
- }
- }
- void CMyGame::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode)
- {
- }
- // Mouse Events Handlers
- void CMyGame::OnMouseMove(Uint16 x,Uint16 y,Sint16 relx,Sint16 rely,bool bLeft,bool bRight,bool bMiddle)
- {
- for (CSprite *pStar : stars)
- {
- if (pStar->GetStatus() == stars.size())
- if (s_Aiming)
- pStar->SetPosition(x, y);
- if (GetLevel() == 3)
- LargeBoxFunc(x, y);
- else if (GetLevel() != 2) SmallBoxFunc(x, y);
- }
- }
- void CMyGame::OnLButtonDown(Uint16 x,Uint16 y)
- {
- if (GetLevel() == 1 && x > 340 && x < 620 && y < 355 && y > 300)
- {
- SetLevel(2);
- menuchoice.Play("menuchoice.wav");
- }
- else if (GetLevel() == 1 && x > 340 && x < 620 && y < 270 && y > 215)
- {
- SetLevel(3);
- menuchoice.Play("menuchoice.wav");
- }
- if (GetLevel() == 2 && !GUI.HitTest(x, y)) StarsControl(x, y);
- else if (GetLevel() == 3 && box_large.HitTest(x, y) && stars.size() <= 16) StarsControl(x, y);
- else if (GetLevel() == 4 && box_small.HitTest(x, y) && stars.size() <= 15) StarsControl(x, y);
- else if (GetLevel() == 5 && box_small.HitTest(x, y) && stars.size() <= 15) StarsControl(x, y);
- else if (GetLevel() == 6 && box_small.HitTest(x, y) && stars.size() <= 14) StarsControl(x, y);
- else if (GetLevel() == 7 && box_small.HitTest(x, y) && stars.size() <= 17) StarsControl(x, y);
- else if (GetLevel() == 8 && box_small.HitTest(x, y) && stars.size() <= 15) StarsControl(x, y);
- else if (GetLevel() == 9 && box_small.HitTest(x, y) && stars.size() <= 16) StarsControl(x, y);
- else if (GetLevel() == 10 && box_small.HitTest(x, y) && stars.size() <= 15) StarsControl(x, y);
- if (Gplus.HitTest(x, y))
- {
- Gplus.SetImage("press");
- menuclick.Play("open.wav");
- G += 0.5f;
- if (G == 0)
- G = 0.5;
- }
- if (Gminus.HitTest(x, y))
- {
- Gminus.SetImage("press");
- menuclick.Play("open.wav");
- G -= 0.5f;
- if (G == 0)
- G = -0.5; // so that G constant never reaches 0 (making the game too easy)
- }
- if (Tplus.HitTest(x, y))
- {
- Tplus.SetImage("press");
- menuclick.Play("open.wav");
- s_Trajectory = true;
- }
- if (Tminus.HitTest(x, y))
- {
- Tminus.SetImage("press");
- menuclick.Play("open.wav");
- s_Trajectory = false;
- }
- if (GUIbutton.HitTest(x, y) && GUIbool)
- {
- GUIbutton.SetImage("press");
- GUImin.Play("GUImin.wav");
- GUIbool = false;
- }
- else if (GUIbutton.HitTest(x, y) && GUIbool == false)
- {
- GUIbutton.SetImage("press");
- GUImax.Play("GUImax.wav");
- GUIbool = true;
- }
- if (BGMbutton.HitTest(x, y) && BGMbool)
- {
- BGMbutton.SetImage("press");
- menuclick.Play("open.wav");
- BGM.Pause();
- BGMbool = false;
- }
- else if (BGMbutton.HitTest(x, y) && BGMbool == false)
- {
- BGMbutton.SetImage("press");
- menuclick.Play("open.wav");
- BGM.Resume();
- BGMbool = true;
- }
- }
- void CMyGame::OnLButtonUp(Uint16 x,Uint16 y)
- {
- if (IsGameMode())
- {
- for (CSprite *pStar : stars)
- {
- if (pStar->GetStatus() == stars.size())
- {
- if (s_Aiming)
- {
- pStar->SetVelocity(Normalize(initial_aim - pStar->GetPosition()) * (Distance(pStar->GetPosition(), initial_aim)));
- CSprite *pStarclickup = new CSpriteRect(x, y, 7, 7, CColor::White(), GetTime());
- starclickup.push_back(pStarclickup);
- s_Aiming = false;
- }
- }
- }
- BGMbutton.SetImage("release");
- GUIbutton.SetImage("release");
- Gplus.SetImage("release");
- Gminus.SetImage("release");
- Tplus.SetImage("release");
- Tminus.SetImage("release");
- }
- }
- void CMyGame::OnRButtonDown(Uint16 x,Uint16 y)
- {
- if (IsGameMode())
- {
- if (GetLevel() == 2)
- {
- CSprite *pStar = new CSpriteOval(CVector(x, y), 10, CColor::Yellow(), GetTime());
- stars.push_back(pStar);
- pStar->SetMass(100000);
- pStar->SetStatus(stars.size());
- for (CSprite *pTrajectory : trajectory) pTrajectory->Delete();
- trajectory.clear();
- }
- }
- }
- void CMyGame::OnRButtonUp(Uint16 x,Uint16 y)
- {
- }
- void CMyGame::OnMButtonDown(Uint16 x,Uint16 y)
- {
- }
- void CMyGame::OnMButtonUp(Uint16 x,Uint16 y)
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement