void Shop::buyItem(cint mousePos, vector<Pickup*>* shopInventory, Player* plr) { Pickup* item = 0; if ((mousePos.X() > tl_xres() - 7) && (mousePos.Y() > tl_yres() - 6)) { int inventoryPos = ((abs((tl_yres() - mousePos.Y() - 5)) * 6) + abs((tl_xres() - mousePos.X() - 6))); if (shopInventory->size() > inventoryPos) { item = shopInventory->at(inventoryPos); if (plr->inventory.size() >= 48) { Console::log("Your Inventory is full!", 0xFFFFFFFF); return; } if (plr->purchase(item->Price(BUY))) { plr->inventory.push_back(item); shopInventory->erase(shopInventory->begin() + item->inventoryPosition); ostringstream message; message << "You bought the " << item->Name() << " for " << item->Price(BUY) << "GP"; Console::log(message.str().c_str(),0xFFFF00FF); Sound::play("gold.sfs"); } else Console::log("You can't afford that!",0xFFFF00FF); } } }
void Shop::buy(Point loc) { int idx = idxFromPoint(loc); Pickup itemToBuy = storeItems[idx]; int price = itemToBuy.getPickupDef().getPrice(); if (invLog->getGold() >= price && !invLog->isInventoryFull()) { //Make sure hero has enough cash and inv isn't full invLog->addItem(itemToBuy); storeItems[idx] = Pickup(); invLog->changeGold(-price); log->registerEvent("Bought a " + itemToBuy.getPickupDef().getName() + " for " + to_string(price)); lib->play("Buy"); } else { log->registerEvent("Not enough gold!"); lib->play("CantBuy"); } }
void Shop::renderShop() { if (shopOpen) { //Render the background lib->loop_portion(start, end, [&](Point p) { lib->rendertile(0x125, p); }); //Render the close button if (lib->mouseLoc() == start) //Highlight if mouse is over close button tl_color(0x000000FF); lib->rendertile(static_cast<int>('x'), start); tl_color(0xFFFFFFFF); //If close button is hit, close the shop if (tl_buttonwentdown(1) && lib->mouseLoc() == start) shopOpen = false; tl_scale(2); lib->renderText(Point(start.x() + 6, start.y() + 3), "Well Hello There, Stranger!"); //Compensate start point with scale lib->renderText(Point(start.x() + 10, end.y() + 12), "Gold Amount: " + to_string(shopGold)); tl_scale(1); //Render icon slots unsigned int idx = 0; Point iconStart = start + Point(5, 5); Point iconEnd = end + Point(-5, -5); lib->loop_portion(iconStart, end + Point(-5, -5), [&](Point p) { lib->rendertile(0x13D, p); if (lib->mouseLoc() == p && storeItems[idx].getLoc() != Point(-1, -1)) { tl_color(0xFF000040); lib->rendertile(0x13D, p); } tl_color(0xFFFFFFFF); if (storeItems[idx].getLoc() != Point(-1, -1)) lib->rendertile(storeItems[idx].getPickupDef().getTile(), p); idx++; }); if (lib->mouseLoc() >= iconStart && lib->mouseLoc() <= iconEnd - Point(1, 1)) { //Render item mouse over Pickup p = storeItems[idxFromPoint(lib->mouseLoc() - iconStart)]; Point loc = lib->mouseLoc() - iconStart; if (p.getLoc() != Point(-1, -1)) { if (p.getPickupDef().getType() != 0) log->renderMouseOver(p.getPickupDef().getName(), p.getPickupDef().getDESC(), "Uses left: " + to_string(p.getUsesLeft()), "Cost: " + to_string(p.getPickupDef().getPrice())); else log->renderMouseOver(p.getPickupDef().getName(), p.getPickupDef().getDESC(), "Cost: " + to_string(p.getPickupDef().getPrice()), ""); } //Buy item if (tl_buttonwentdown(1)) buy(loc); } else if (lib->mouseLoc() >= Point(lib->res().x() - 3, 3) && lib->mouseLoc() <= Point(lib->res().x() - 2, 9)) { if (tl_buttonwentdown(1)) { Pickup p = invLog->sellItem(lib->mouseLoc()); sell(p); } } } }
void Shop::sell(Pickup p) { if (p.getLoc() != Point(-1, -1)) { //Make sure we're actually selling an item //Add it to the store if there's room for (unsigned int i = 0; i < storeItems.size(); i++) { if (storeItems[i].getLoc() == Point(-1, -1)) { storeItems[i] = p; break; } } //Decrement gold, add it to the player int price = p.getPickupDef().getPrice(); invLog->changeGold(price); shopGold -= price; lib->play("Sell"); log->registerEvent("Sold a " + p.getPickupDef().getName() + " for " + to_string(price) + " gold"); } }
void Test() { robotDrive.SetSafetyEnabled(false); uint8_t toSend[10];//array of bytes to send over I2C uint8_t toReceive[10];//array of bytes to receive over I2C uint8_t numToSend = 1;//number of bytes to send uint8_t numToReceive = 0;//number of bytes to receive toSend[0] = 7; //send 0 to arduino i2c.Transaction(toSend, numToSend, toReceive, numToReceive); bool isSettingUp = true; pickup.setGrabber(-1); pickup.setLifter(1); while (isSettingUp) { isSettingUp = false; if (grabOuterLimit.Get() == false) { pickup.setGrabber(0); } else { isSettingUp = true; } if (liftLowerLimit.Get()) { pickup.setLifter(0); } else { isSettingUp = true; } } gyro.Reset(); liftEncoder.Reset(); grabEncoder.Reset(); toSend[0] = 8; i2c.Transaction(toSend, numToSend, toReceive, numToReceive); while(IsTest() && IsEnabled()); toSend[0] = 0; i2c.Transaction(toSend, numToSend, toReceive, numToReceive); }
unsigned int NetClient::handlePickupState(Uint8 *data, unsigned int size) { DEBUGSTR("net", "handlePickupState()"); Uint16 eid; CRC32 type; float qx, qy, qz, qw, bx, by, bz; unpack(data, "hl ffff fff", &eid, &type, &qx, &qy, &qz, &qw, &bx, &by, &bz ); Entity* e = st->getEntity(eid); Pickup* p = (Pickup*) e; // If don't exist, create if (p == NULL) { PickupType *pt = GEng()->mm->getPickupType(type); if (pt == NULL) { this->error("Invalid pickup type " + type); return 34; } p = new Pickup(pt, st, bx, bz, by); st->addPickup(p); p->eid = eid; } // Update the transform btTransform xform = btTransform( btQuaternion(qx, qy, qz, qw), btVector3(bx, by, bz) ); p->setTransform(xform); return 34; }
void BattleStage::Update() { while( GameObjectsToAdd.size() > 0 ) { Projectile* p = GameObjectsToAdd.front(); GameObjectsToAdd.pop_front(); GameObjects.push_back( p ); } while( GameObjectsToRemove.size() > 0 ) { Projectile* p = GameObjectsToRemove.front(); GameObjectsToRemove.pop_front(); GameObjects.remove( p ); delete p; } for( std::list<Projectile*>::const_iterator i = GameObjects.begin(); i != GameObjects.end(); i++ ) { Projectile* p = (*i); p->Update(); } LeftPlayer->Update(); RightPlayer->Update(); if( ((BattlePlayer*)LeftPlayer)->TargetHealth == 0 || ((BattlePlayer*)RightPlayer)->TargetHealth == 0 ) { FRAMEWORK->ProgramStages->Push( new TransitionStrips( new BattleOver( this ), FRAMES_PER_SECOND, 80 ) ); } for( std::list<Pickup*>::const_iterator i = GamePickups.begin(); i != GamePickups.end(); i++ ) { Pickup* p = (*i); p->Update(); } }
unsigned int NetClient::handlePickupState(Uint8 *data, unsigned int size) { cout << " handlePickupState()\n"; Uint16 eid; CRC32 type; float qx, qy, qz, qw, bx, by, bz; unpack(data, "hl ffff fff", &eid, &type, &qx, &qy, &qz, &qw, &bx, &by, &bz ); Entity* e = st->getEntity(eid); Pickup* p = (Pickup*) e; // If don't exist, create if (p == NULL) { PickupType *pt = st->mm->getPickupType(type); if (pt == NULL) return 34; // TODO: Should we err instead? p = new Pickup(pt, st, bx, bz, by); st->addPickup(p); p->eid = eid; } // Update the transform btTransform xform = btTransform( btQuaternion(qx, qy, qz, qw), btVector3(bx, by, bz) ); p->setTransform(xform); return 34; }
/** * Runs the motors with Mecanum drive. */ void OperatorControl()//teleop code { robotDrive.SetSafetyEnabled(false); gyro.Reset(); grabEncoder.Reset(); timer.Start(); timer.Reset(); double liftHeight = 0; //variable for lifting thread int liftHeightBoxes = 0; //another variable for lifting thread int liftStep = 0; //height of step in inches int liftRamp = 0; //height of ramp in inches double grabPower; bool backOut; uint8_t toSend[10];//array of bytes to send over I2C uint8_t toReceive[10];//array of bytes to receive over I2C uint8_t numToSend = 1;//number of bytes to send uint8_t numToReceive = 0;//number of bytes to receive toSend[0] = 1;//set the byte to send to 1 i2c.Transaction(toSend, 1, toReceive, 0);//send over I2C bool isGrabbing = false;//whether or not grabbing thread is running bool isLifting = false;//whether or not lifting thread is running bool isBraking = false;//whether or not braking thread is running float driveX = 0; float driveY = 0; float driveZ = 0; float driveGyro = 0; bool liftLastState = false; bool liftState = false; //button pressed double liftLastTime = 0; double liftTime = 0; bool liftRan = true; Timer switchTimer; Timer grabTimer; switchTimer.Start(); grabTimer.Start(); while (IsOperatorControl() && IsEnabled()) { // Use the joystick X axis for lateral movement, Y axis for forward movement, and Z axis for rotation. // This sample does not use field-oriented drive, so the gyro input is set to zero. toSend[0] = 1; numToSend = 1; driveX = driveStick.GetRawAxis(Constants::driveXAxis);//starts driving code driveY = driveStick.GetRawAxis(Constants::driveYAxis); driveZ = driveStick.GetRawAxis(Constants::driveZAxis); driveGyro = gyro.GetAngle() + Constants::driveGyroTeleopOffset; if (driveStick.GetRawButton(Constants::driveOneAxisButton)) {//if X is greater than Y and Z, then it will only go in the direction of X toSend[0] = 6; numToSend = 1; if (fabs(driveX) > fabs(driveY) && fabs(driveX) > fabs(driveZ)) { driveY = 0; driveZ = 0; } else if (fabs(driveY) > fabs(driveX) && fabs(driveY) > fabs(driveZ)) {//if Y is greater than X and Z, then it will only go in the direction of Y driveX = 0; driveZ = 0; } else {//if Z is greater than X and Y, then it will only go in the direction of Z driveX = 0; driveY = 0; } } if (driveStick.GetRawButton(Constants::driveXYButton)) {//Z lock; only lets X an Y function toSend[0] = 7; driveZ = 0;//Stops Z while Z lock is pressed } if (!driveStick.GetRawButton(Constants::driveFieldLockButton)) {//robot moves based on the orientation of the field driveGyro = 0;//gyro stops while field lock is enabled } driveX = Constants::scaleJoysticks(driveX, Constants::driveXDeadZone, Constants::driveXMax * (.5 - (driveStick.GetRawAxis(Constants::driveThrottleAxis) / 2)), Constants::driveXDegree); driveY = Constants::scaleJoysticks(driveY, Constants::driveYDeadZone, Constants::driveYMax * (.5 - (driveStick.GetRawAxis(Constants::driveThrottleAxis) / 2)), Constants::driveYDegree); driveZ = Constants::scaleJoysticks(driveZ, Constants::driveZDeadZone, Constants::driveZMax * (.5 - (driveStick.GetRawAxis(Constants::driveThrottleAxis) / 2)), Constants::driveZDegree); robotDrive.MecanumDrive_Cartesian(driveX, driveY, driveZ, driveGyro);//makes the robot drive if (pdp.GetCurrent(Constants::grabPdpChannel) < Constants::grabManualCurrent) { pickup.setGrabber(Constants::scaleJoysticks(grabStick.GetX(), Constants::grabDeadZone, Constants::grabMax, Constants::grabDegree)); //defines the grabber if(grabTimer.Get() < 1) { toSend[0] = 6; } } else { pickup.setGrabber(0); grabTimer.Reset(); toSend[0] = 6; } if (Constants::grabLiftInverted) { pickup.setLifter(-Constants::scaleJoysticks(grabStick.GetY(), Constants::liftDeadZone, Constants::liftMax, Constants::liftDegree)); //defines the lifter } else { pickup.setLifter(Constants::scaleJoysticks(grabStick.GetY(), Constants::liftDeadZone, Constants::liftMax, Constants::liftDegree)); //defines the lifter } SmartDashboard::PutNumber("Lift Power", Constants::scaleJoysticks(grabStick.GetY(), Constants::liftDeadZone, Constants::liftMax, Constants::liftDegree)); SmartDashboard::PutBoolean("Is Lifting", isLifting); if (Constants::scaleJoysticks(grabStick.GetY(), Constants::liftDeadZone, Constants::liftMax, Constants::liftDegree) != 0 || isLifting) { //if the robot is lifting isBraking = false; //stop braking thread SmartDashboard::PutBoolean("Braking", false); } else if(!isBraking) { isBraking = true; //run braking thread pickup.lifterBrake(isBraking);//brake the pickup } if (grabStick.GetRawButton(Constants::liftFloorButton)) { liftHeight = 0; pickup.lifterPosition(liftHeight, isLifting, grabStick);//start lifting thread liftRan = true; } liftTime = timer.Get(); liftState = grabStick.GetRawButton(Constants::liftButton); if (liftState) { //if button is pressed if (!liftLastState) { if (liftTime - liftLastTime < Constants::liftMaxTime) { if (liftHeightBoxes < Constants::liftMaxHeightBoxes) { liftHeightBoxes++; //adds 1 to liftHeightBoxes } } else { liftHeightBoxes = 1; liftRamp = 0; liftStep = 0; } } liftLastTime = liftTime; liftLastState = true; liftRan = false; } else if (grabStick.GetRawButton(Constants::liftRampButton)) { if (liftTime - liftLastTime > Constants::liftMaxTime) { liftHeight = 0; liftStep = 0; } liftRamp = 1; //prepares to go up ramp liftLastTime = liftTime; liftRan = false; } else if (grabStick.GetRawButton(Constants::liftStepButton)) { if (liftTime - liftLastTime > Constants::liftMaxTime) { liftHeight = 0; liftRamp = 0; } liftStep = 1; //prepares robot for step liftLastTime = liftTime; liftRan = false; } else { if (liftTime - liftLastTime > Constants::liftMaxTime && !liftRan) { liftHeight = liftHeightBoxes * Constants::liftBoxHeight + liftRamp * Constants::liftRampHeight + liftStep * Constants::liftStepHeight; //sets liftHeight if (liftHeightBoxes > 0) { liftHeight -= Constants::liftBoxLip; } pickup.lifterPosition(liftHeight, isLifting, grabStick);//start lifting thread liftRan = true; } liftLastState = false; } if (grabStick.GetRawButton(Constants::grabToteButton)) {//if grab button is pressed grabPower = Constants::grabToteCurrent; backOut = true; if (!isGrabbing) { pickup.grabberGrab(isGrabbing, grabPower, backOut, grabStick);//start grabber thread } } else if (grabStick.GetRawButton(Constants::grabBinButton)) {//if grab button is pressed grabPower = Constants::grabBinCurrent; backOut = false; if (!isGrabbing) { pickup.grabberGrab(isGrabbing, grabPower, backOut, grabStick);//start grabber thread } } else if (grabStick.GetRawButton(Constants::grabChuteButton)) {//if grab button is presset SmartDashboard::PutBoolean("Breakpoint -2", false); SmartDashboard::PutBoolean("Breakpoint -1", false); SmartDashboard::PutBoolean("Breakpoint 0", false); SmartDashboard::PutBoolean("Breakpoint 1", false); SmartDashboard::PutBoolean("Breakpoint 2", false); SmartDashboard::PutBoolean("Breakpoint 3", false); SmartDashboard::PutBoolean("Breakpoint 4", false); //Wait(.5); if (!isGrabbing) { //pickup.grabberChute(isGrabbing, grabStick);//start grabber thread } } //determines what the LED's look like based on what the Robot is doing if (isGrabbing) { toSend[0] = 5; numToSend = 1; } if (isLifting) {//if the grabbing thread is running if (Constants::encoderToDistance(liftEncoder.Get(),Constants::liftEncoderTicks, Constants::liftEncoderBase, Constants::liftEncoderRadius) < liftHeight) { toSend[0] = 3; } else { toSend[0] = 4; } numToSend = 1;//sends 1 byte to I2C } if(!grabOuterLimit.Get()) { //tells if outer limit is hit with lights if(switchTimer.Get() < 1) { toSend[0] = 6; } } else { switchTimer.Reset(); } if (driveStick.GetRawButton(Constants::sneakyMoveButton)) { toSend[0] = 0; numToSend = 1; } float distance = prox.GetVoltage() * Constants::ultrasonicVoltageToInches / 12; // distance from ultrasonic sensor float rotations = (float) liftEncoder.Get(); // rotations on encoder SmartDashboard::PutNumber("Distance", distance); // write stuff to smart dash SmartDashboard::PutNumber("Current", pdp.GetCurrent(Constants::grabPdpChannel)); SmartDashboard::PutNumber("LED Current", pdp.GetCurrent(Constants::ledPdpChannel)); SmartDashboard::PutNumber("Lift Encoder", rotations); SmartDashboard::PutNumber("Lift Height", liftHeight); SmartDashboard::PutNumber("Grab Encoder", grabEncoder.Get()); SmartDashboard::PutBoolean("Grab Inner", grabInnerLimit.Get()); SmartDashboard::PutBoolean("Grab Outer", grabOuterLimit.Get()); SmartDashboard::PutNumber("Drive Front Left Current", pdp.GetCurrent(Constants::driveFrontLeftPin)); SmartDashboard::PutNumber("Drive Front Right Current", pdp.GetCurrent(Constants::driveFrontRightPin)); SmartDashboard::PutNumber("Drive Rear Left Current", pdp.GetCurrent(Constants::driveRearLeftPin)); SmartDashboard::PutNumber("Drive Rear Right Current", pdp.GetCurrent(Constants::driveRearRightPin)); SmartDashboard::PutNumber("Throttle", grabStick.GetZ()); i2c.Transaction(toSend, 1, toReceive, 0);//send and receive information from arduino over I2C Wait(0.005); // wait 5ms to avoid hogging CPU cycles } //end of teleop isBraking = false; toSend[0] = 0; i2c.Transaction(toSend, numToSend, toReceive, numToReceive); }
void Autonomous() { Timer timer; float power = 0; bool isLifting = false; bool isGrabbing = false; double liftHeight = Constants::liftBoxHeight-Constants::liftBoxLip; double grabPower = Constants::grabAutoCurrent; bool backOut; uint8_t toSend[1];//array of bytes to send over I2C uint8_t toReceive[0];//array of bytes to receive over I2C uint8_t numToSend = 1;//number of bytes to send uint8_t numToReceive = 0;//number of bytes to receive toSend[0] = 2;//set the byte to send to 1 i2c.Transaction(toSend, numToSend, toReceive, numToReceive);//send over I2C bool isSettingUp = true; //pickup.setGrabber(-1); //open grabber all the way pickup.setLifter(0.8); while (isSettingUp && IsEnabled() && IsAutonomous()) { isSettingUp = false; /*if (grabOuterLimit.Get() == false) { pickup.setGrabber(0); //open until limit } else { isSettingUp = true; }*/ if (liftLowerLimit.Get()) { pickup.setLifter(0); //down till bottom } else { isSettingUp = true; } } gyro.Reset(); liftEncoder.Reset(); grabEncoder.Reset(); if (grabStick.GetZ() > .8) { timer.Reset(); timer.Start(); while (timer.Get() < 1) { robotDrive.MecanumDrive_Cartesian(0, power, 0, gyro.GetAngle()); // drive back if(power>-.4){ power-=0.005; Wait(.005); } } robotDrive.MecanumDrive_Cartesian(0, 0, 0, gyro.GetAngle()); // STOP!!! timer.Stop(); timer.Reset(); Wait(1); } power = 0; while (isLifting && IsEnabled() && IsAutonomous()) { Wait(.005); } backOut = Constants::autoBackOut; pickup.grabberGrab(isGrabbing, grabPower, backOut, grabStick); Wait(.005); while (isGrabbing && IsEnabled() && IsAutonomous()) { Wait(.005); } liftHeight = 3*Constants::liftBoxHeight; Wait(.005); pickup.lifterPosition(liftHeight, isLifting, grabStick); Wait(.005); while (isLifting && IsEnabled() && IsAutonomous()) { Wait(.005); } while(prox.GetVoltage() * Constants::ultrasonicVoltageToInches / 12 < 2 && IsEnabled() && IsAutonomous()); // while the nearest object is closer than 2 feet timer.Start(); while(prox.GetVoltage() * Constants::ultrasonicVoltageToInches < Constants::autoBackupDistance && timer.Get() < Constants::autoMaxDriveTime && IsEnabled() && IsAutonomous()) { // while the nearest object is further than 12 feet if (power < .45) { //ramp up the power slowly power += .00375; } robotDrive.MecanumDrive_Cartesian(0, power, 0, gyro.GetAngle()); // drive back float distance = prox.GetVoltage() * Constants::ultrasonicVoltageToInches / 12; // distance from ultrasonic sensor SmartDashboard::PutNumber("Distance", distance); // write stuff to smart dash SmartDashboard::PutNumber("Drive Front Left Current", pdp.GetCurrent(Constants::driveFrontLeftPin)); SmartDashboard::PutNumber("Drive Front Right Current", pdp.GetCurrent(Constants::driveFrontRightPin)); SmartDashboard::PutNumber("Drive Rear Left Current", pdp.GetCurrent(Constants::driveRearLeftPin)); SmartDashboard::PutNumber("Drive Rear Right Current", pdp.GetCurrent(Constants::driveRearRightPin)); SmartDashboard::PutNumber("Gyro Angle", gyro.GetAngle()); SmartDashboard::PutNumber("Distance (in)", prox.GetVoltage() * Constants:: ultrasonicVoltageToInches); Wait(.005); } timer.Reset(); while(timer.Get() < Constants::autoBrakeTime && IsEnabled() && IsAutonomous()) { // while the nearest object is further than 12 feet robotDrive.MecanumDrive_Cartesian(0,Constants::autoBrakePower,0); ///Brake } float turn = 0; while (fabs(turn) < 85 && IsEnabled() && IsAutonomous()) { //turn 90(ish) degrees robotDrive.MecanumDrive_Cartesian(0,0,.1); turn = gyro.GetAngle(); if (turn > 180) { turn -= 360; } } robotDrive.MecanumDrive_Cartesian(0,0,0); ///STOP!!! timer.Stop(); toSend[0] = 8; i2c.Transaction(toSend, numToSend, toReceive, numToReceive); while(IsAutonomous() && IsEnabled()); toSend[0] = 0; i2c.Transaction(toSend, numToSend, toReceive, numToReceive); }
void GameEngine::onMouseOver() { //Render inventory item information if (lib.mouseLoc().x() > lib.res().x() - 4) { Pickup item = invLog.getItemAtLoc(lib.mouseLoc()); if (item.getLoc() != Point(-1, -1)) { if (item.getPickupDef().getType() != 0) log.renderMouseOver(item.getPickupDef().getName(), item.getPickupDef().getDESC(), "Uses left: " + to_string(item.getUsesLeft()), ""); else log.renderMouseOver(item.getPickupDef().getName(), item.getPickupDef().getDESC(), "", ""); } } //Render world information else { Point relMouseLoc = screenOrientation - lib.res() / 2 + lib.mouseLoc(); if (relMouseLoc.x() > 0 && relMouseLoc.y() > 0 && relMouseLoc.x() <= worldSize - 1 && relMouseLoc.y() <= worldSize - 1 && !shop.isShopping()) { if (!map[relMouseLoc.x()][relMouseLoc.y()].isVisible()) { Pickup* p = map[relMouseLoc.x()][relMouseLoc.y()].getPickup(); Actor* a = map[relMouseLoc.x()][relMouseLoc.y()].getActor(); if (a != NULL) log.renderMouseOver(a->getActorDef().getDESC(), "HP: " + to_string(a->getHP()), "ATK: " + a->getActorDef().actorATK().to_str(), "DEF: " + to_string(a->getActorDef().getDEF())); else if (p != NULL) { if (p->getPickupDef().getType() != 0) log.renderMouseOver(p->getPickupDef().getName(), p->getPickupDef().getDESC(), "Uses left: " + to_string(p->getUsesLeft()), ""); else log.renderMouseOver(p->getPickupDef().getName(), p->getPickupDef().getDESC(), "", ""); } } } } tl_scale(1); }
//main void GameEngine::run() { lib.framestart(); if (!gameStart) renderGameMenu(); else { //Move the char left, right, up, and down if (!deadHero && !shop.isShopping()) { onPlayerTurn((lib.keywentdown("left") || lib.keywentdown("a")), Point(-1, 0)); onPlayerTurn((lib.keywentdown("up") || lib.keywentdown("w")), Point(0, -1)); onPlayerTurn((lib.keywentdown("right") || lib.keywentdown("d")), Point(1, 0)); onPlayerTurn((lib.keywentdown("down") || lib.keywentdown("s")), Point(0, 1)); //Enter stairs if (lib.keywentdown("return") || lib.keywentdown("enter")) { if (hero->getLoc() == stairLoc) { currLevel++; generateLevel(); } if (hero->getLoc() == shop.getLoc()) shop.activateShop(); if (hero->getLoc() == bossLoc) enterBossRoom(); if (hero->getLoc() == stairAscend) leaveBossRoom(); } if (tl_buttonwentdown(1)) { Pickup p = registerInventoryClick(); if (p.getLoc() != Point(-1, -1)) { if (p.getPickupDef().getType() == 0) log.registerEvent(("Used a " + p.getPickupDef().getName() + "!").c_str()); else log.registerEvent(("Equipped a " + p.getPickupDef().getName() + "!").c_str()); } if (lib.mouseLoc() >= Point(lib.res().x() / 2 - 3, lib.res().y() - 4) && lib.mouseLoc() <= Point(lib.res().x() / 2 + 3, lib.res().y() - 4)) useSpell(lib.mouseLoc()); } if (tl_buttonwentdown(3)) { Pickup p; if (invLog.overInvOrGear(lib.mouseLoc()) == 0) { p = invLog.dropItem(lib.mouseLoc()); if (p.getLoc() != Point(-1, -1)) log.registerEvent(("Dropped the " + p.getPickupDef().getName() + "!").c_str()); } else if (invLog.overInvOrGear(lib.mouseLoc()) == 1) { OutputDebugString("herpderp"); p = invLog.unequipGear(lib.mouseLoc()); if (p.getLoc() != Point(-1, -1)) log.registerEvent(("Unequipped the " + p.getPickupDef().getName() + "!").c_str()); } } } drawMap(); onMouseOver(); if(deadHero) { if (lib.keywentdown("escape")) gameStart = false; } } }
void BattleStage::Render() { BattlePlayer* ply; al_draw_bitmap( backgroundImage, 0, 0, 0 ); for( std::list<Pickup*>::const_iterator i = GamePickups.begin(); i != GamePickups.end(); i++ ) { Pickup* p = (*i); p->Render(); } for( std::list<Projectile*>::const_reverse_iterator i = GameObjects.rbegin(); i != GameObjects.rend(); i++ ) { Projectile* p = (*i); p->Render(); } LeftPlayer->Render(); RightPlayer->Render(); // Boundaries of playing area al_draw_line( MAIN_POS_X - MAIN_BORDER, MAIN_POS_Y - MAIN_BORDER/ 2.0f, MAIN_POS_X + MAIN_WIDTH + MAIN_BORDER, MAIN_POS_Y - MAIN_BORDER/ 2.0f, al_map_rgb( 255, 255, 255 ), MAIN_BORDER ); al_draw_line( MAIN_POS_X + MAIN_WIDTH + MAIN_BORDER / 2.0f, MAIN_POS_Y, MAIN_POS_X + MAIN_WIDTH + MAIN_BORDER / 2.0f, MAIN_POS_Y + MAIN_HEIGHT, al_map_rgb( 255, 255, 255 ), MAIN_BORDER ); al_draw_line( MAIN_POS_X - MAIN_BORDER, MAIN_POS_Y + MAIN_HEIGHT + MAIN_BORDER/ 2.0f, MAIN_POS_X + MAIN_WIDTH + MAIN_BORDER, MAIN_POS_Y + MAIN_HEIGHT + MAIN_BORDER/ 2.0f, al_map_rgb( 255, 255, 255 ), MAIN_BORDER ); al_draw_line( MAIN_POS_X - MAIN_BORDER/ 2.0f, MAIN_POS_Y, MAIN_POS_X - MAIN_BORDER/ 2.0f, MAIN_POS_Y + MAIN_HEIGHT, al_map_rgb( 255, 255, 255 ), MAIN_BORDER ); ply = (BattlePlayer*)LeftPlayer; // Left player inventory al_draw_text( labelFont, al_map_rgb( 255, 255, 255 ), INVENTORY_LEFT_X_POS + INVENTORY_ITEM_SIZE / 2.0f, INVENTORY_LEFT_Y_POS - LABEL_FONT_SIZE * 1.2f, ALLEGRO_ALIGN_CENTRE, "items" ); inventoryIcons->DrawSprite( ply->Inventory[0], INVENTORY_LEFT_X_POS, INVENTORY_LEFT_Y_POS, 1.0f, 1.0f, 0 ); inventoryIcons->DrawSprite( ply->Inventory[1], INVENTORY_LEFT_X_POS, INVENTORY_LEFT_Y_POS + INVENTORY_ITEM_SIZE + INVENTORY_LEFT_Y_SPACING, 1.0f, 1.0f, 0 ); inventoryIcons->DrawSprite( ply->Inventory[2], INVENTORY_LEFT_X_POS, INVENTORY_LEFT_Y_POS + (INVENTORY_ITEM_SIZE + INVENTORY_LEFT_Y_SPACING) * 2, 1.0f, 1.0f, 0 ); // Left player health al_draw_text( labelFont, al_map_rgb( 255, 255, 255 ), HEALTH_LEFT_X_POS + HEALTH_LEFT_WIDTH / 2.0f, HEALTH_LEFT_Y_POS - HEALTH_LEFT_BORDER - LABEL_FONT_SIZE * 1.2f, ALLEGRO_ALIGN_CENTRE, "health" ); al_draw_filled_rectangle( HEALTH_LEFT_X_POS - HEALTH_LEFT_BORDER, HEALTH_LEFT_Y_POS - HEALTH_LEFT_BORDER, HEALTH_LEFT_X_POS + HEALTH_LEFT_WIDTH + HEALTH_LEFT_BORDER, HEALTH_LEFT_Y_POS + HEALTH_LEFT_HEIGHT + HEALTH_LEFT_BORDER, al_map_rgb( 255, 255, 255 ) ); al_draw_filled_rectangle( HEALTH_LEFT_X_POS, HEALTH_LEFT_Y_POS + (1.0f - (ply->Health / (float)ply->MaxHealth)) * HEALTH_LEFT_HEIGHT, HEALTH_LEFT_X_POS + HEALTH_LEFT_WIDTH, HEALTH_LEFT_Y_POS + HEALTH_LEFT_HEIGHT, al_map_rgb( 168, 0, 0 ) ); ply = (BattlePlayer*)RightPlayer; // Right player inventory al_draw_text( labelFont, al_map_rgb( 255, 255, 255 ), INVENTORY_RIGHT_X_POS + INVENTORY_ITEM_SIZE / 2.0f, INVENTORY_RIGHT_Y_POS - LABEL_FONT_SIZE * 1.2f, ALLEGRO_ALIGN_CENTRE, "items" ); inventoryIcons->DrawSprite( ply->Inventory[0], INVENTORY_RIGHT_X_POS, INVENTORY_RIGHT_Y_POS, 1.0f, 1.0f, 0 ); inventoryIcons->DrawSprite( ply->Inventory[1], INVENTORY_RIGHT_X_POS, INVENTORY_RIGHT_Y_POS + INVENTORY_ITEM_SIZE + INVENTORY_RIGHT_Y_SPACING, 1.0f, 1.0f, 0 ); inventoryIcons->DrawSprite( ply->Inventory[2], INVENTORY_RIGHT_X_POS, INVENTORY_RIGHT_Y_POS + (INVENTORY_ITEM_SIZE + INVENTORY_RIGHT_Y_SPACING) * 2, 1.0f, 1.0f, 0 ); // Right player health al_draw_text( labelFont, al_map_rgb( 255, 255, 255 ), HEALTH_RIGHT_X_POS + HEALTH_RIGHT_WIDTH / 2.0f, HEALTH_RIGHT_Y_POS - HEALTH_RIGHT_BORDER- LABEL_FONT_SIZE * 1.2f, ALLEGRO_ALIGN_CENTRE, "health" ); al_draw_filled_rectangle( HEALTH_RIGHT_X_POS - HEALTH_RIGHT_BORDER, HEALTH_RIGHT_Y_POS - HEALTH_RIGHT_BORDER, HEALTH_RIGHT_X_POS + HEALTH_RIGHT_WIDTH + HEALTH_RIGHT_BORDER, HEALTH_RIGHT_Y_POS + HEALTH_RIGHT_HEIGHT + HEALTH_RIGHT_BORDER, al_map_rgb( 255, 255, 255 ) ); al_draw_filled_rectangle( HEALTH_RIGHT_X_POS, HEALTH_RIGHT_Y_POS + (1.0f - (ply->Health / (float)ply->MaxHealth)) * HEALTH_RIGHT_HEIGHT, HEALTH_RIGHT_X_POS + HEALTH_RIGHT_WIDTH, HEALTH_RIGHT_Y_POS + HEALTH_RIGHT_HEIGHT, al_map_rgb( 168, 0, 0 ) ); }
int main(int argc, char* argv[]) { #if defined(_WIN32) || (_WIN64) cout << "Running on Windows..." << endl; //get the current working directory string s_cwd(getcwd(NULL, 0)); // create a string linking to the images folder string images_dir = s_cwd + "\\HHResource\\images\\"; // create a string linking to the audio folder string audio_dir = s_cwd + "\\HHResource\\audio\\"; #endif #if defined(__APPLE__) cout << "Running on Apple..." << endl; //get the current working directory string s_cwd(getcwd(NULL, 0)); // create a sting linking to the mac's images folder string images_dir = s_cwd + "/HHResource/images/"; //create a string link to the audio folder on __APPLE__ string audio_dir = s_cwd + "/HHResource/audio/"; #endif SDL_Window *window; // Declare a pointer SDL_Init(SDL_INIT_EVERYTHING); // Initialize SDL2 // Create an application window with the following settings: window = SDL_CreateWindow("Night of the Horseman", // window title SDL_WINDOWPOS_UNDEFINED, // initial x position SDL_WINDOWPOS_UNDEFINED, // initial y position 1024, // width, in pixels 768, // height, in pixels SDL_WINDOW_OPENGL // flags - see below ); // Check that the window was successfully created if (window == NULL) { // In the case that the window could not be made... printf("Could not create window: %s\n", SDL_GetError()); return 1; } // keyboard input const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL); //SDL Event to handle event input SDL_Event event; //Renderer SDL_Renderer * renderer = NULL; //create the renderer renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); //transition numbers enum slide { up, down, left, right }; //CREATE THE PLAYER Player player(renderer, images_dir, 0, 320); //create the background images Scenery BKGD(renderer, images_dir, "background.png", 0, 0); Scenery MDGD2(renderer, images_dir, "midground2.png", 0, -728); Scenery MDGD1(renderer, images_dir, "midground1.png", 0, -728); Scenery FRGD(renderer, images_dir, "foreground.png", 0, -728); Scenery instruct(renderer, images_dir, "fireInstruct.png", 0, 668); instruct.Pos.w = 256; instruct.Pos.h = 100; //create the pickups Pickup * ammo[2]; ammo[0] = new Pickup(renderer, images_dir, "pumkinB.png", 600, 400); ammo[1] = new Pickup(renderer, images_dir, "pumkinB.png", 1150, 400); ammo[0]->active = false; ammo[1]->active = false; Pickup * winBox = new Pickup(renderer, images_dir, "pumkinB.png", 2850, 350); Pickup * health[2]; health[0] = new Pickup(renderer, images_dir, "spiritOrb.png", 350, 350); health[1] = new Pickup(renderer, images_dir, "spiritOrb.png", 1885, 350); health[0]->active = false; health[1]->active = false; Pickup *bat[3]; bat[0] = new Pickup(renderer, images_dir, "bat.png", 500, -500); bat[1] = new Pickup(renderer, images_dir, "bat.png", 2700, -500); bat[2] = new Pickup(renderer, images_dir, "bat.png", 1800, 1000); //Make the enemy turrets Turret * marksman[3]; marksman[0] = new Turret(renderer, images_dir, 1175, 240); marksman[1] = new Turret(renderer, images_dir, 1875, 240); marksman[2] = new Turret(renderer, images_dir, 2515, 240); //make the enemies that chase the player ChaseEnemy * skull[3]; skull[0] = new ChaseEnemy(renderer, images_dir, 400, -500); skull[1] = new ChaseEnemy(renderer, images_dir, 2600, -500); skull[2] = new ChaseEnemy(renderer, images_dir, 1700, 1000); //GUI objects Button title(renderer, images_dir, "title.png", "title.png", 256, 64); title.Pos.w = 512; title.Pos.h = 128; Button playGame(renderer, images_dir, "play-n.png", "play-o.png", 512 - 64, 384); Button quitGame(renderer, images_dir, "quit-n.png", "quit-o.png", 512 - 64, 384 + 128); SDL_Rect mousePos; mousePos.x = 0; mousePos.y = 0; mousePos.w = 16; mousePos.h = 16; SDL_Rect winRect; winRect.x = 2800; winRect.y = 320; winRect.w = 100; winRect.h = 100; BKGD.Pos.w = 1024; BKGD.Pos.h = 768; //Win screen and Lose screen Scenery win(renderer, images_dir, "win.png", 0, 0); win.Pos.w = 1024; win.Pos.h = 768; Scenery lose(renderer, images_dir, "lose.png", 0, 0); lose.Pos.w = 1024; lose.Pos.h = 768; //bool values to control game states bool quit = false; enum GameState { GAME, WIN, LOSE, TITLE }; GameState state; state = TITLE; //game loop while (!quit) { switch (state) { case GAME: { /* //reset everything player.currentHealth = 100; player.pos_X = 1; player.pos_Y = 320; BKGD.pos_X = 0; BKGD.pos_Y = 0; MDGD2.pos_X = 0; MDGD2.pos_Y = -728; MDGD1.pos_X = 0; MDGD1.pos_Y = -728; FRGD.pos_X = 0; FRGD.pos_Y = -728; ammo[0]->pos_X = 600; ammo[0]->pos_Y = 400; ammo[1]->pos_X = 1150; ammo[1]->pos_Y = 400; health[0]->pos_X = 350; health[0]->pos_Y = 350; health[1]->pos_X = 1885; health[1]->pos_Y = 350; bat[0]->pos_X = 500; bat[0]->pos_Y = -500; bat[1]->pos_X = 2700; bat[1]->pos_Y = -500; bat[2]->pos_X = 1800; bat[2]->pos_Y = 1000; marksman[0]->pos_X = 1175; marksman[0]->pos_Y = 240; marksman[1]->pos_X = 1875; marksman[1]->pos_Y = 240; marksman[2]->pos_X = 2515; marksman[2]->pos_Y = 240; skull[0]->pos_X = 400; skull[0]->pos_Y = -500; skull[1]->pos_X = 2600; skull[1]->pos_Y = -500; skull[2]->pos_X = 1700; skull[2]->pos_Y = 1000; */ //Create deltaTime thisTime = SDL_GetTicks(); deltaTime = (float) (thisTime - lastTime) / 1000; lastTime = thisTime; player.fireRate += 3 * deltaTime; //check for input events if (SDL_PollEvent(&event)) { //if the window is closed if (event.type == SDL_QUIT) { quit = true; break; } } switch (event.type) { case SDL_MOUSEBUTTONDOWN: if (event.button.button == SDL_BUTTON_LEFT) { if (player.fireRate > 1 && player.ammoCount > 0) { player.ammoCount--; player.fireRate = 0; player.throwPumpkin(); } } break; case SDL_MOUSEMOTION: mouseX = event.button.x; mouseY = event.button.y; break; } //to quit the game if (currentKeyStates[SDL_SCANCODE_ESCAPE]) { //quitting SDL_Delay(200); quit = true; } //move up if (currentKeyStates[SDL_SCANCODE_W]) { //move things down if (FRGD.Pos.y < 0 && player.Pos.y < 384 - 64) { FRGD.move(0, down, deltaTime); MDGD1.move(10, down, deltaTime); MDGD2.move(20, down, deltaTime); winBox->move(0, down, deltaTime); //pickups for (int i = 0; i < 2; i++) { ammo[i]->move(0, down, deltaTime); health[i]->move(0, down, deltaTime); } for (int i = 0; i < 3; i++) { bat[i]->move(0, down, deltaTime); } //bullets for (int i = 0; i < player.bulletList.size(); i++) { if (player.bulletList[i]->active) { player.bulletList[i]->move(0, down, deltaTime); } } //enemies for (int i = 0; i < 3; i++) { skull[i]->move(0, down, deltaTime); marksman[i]->move(0, down, deltaTime); for (int j = 0; j < marksman[i]->bulletList.size(); j++) { marksman[i]->bulletList[j]->move(0, down, deltaTime); } } } else if (player.Pos.y > 0) player.move(0, up, deltaTime); if (player.flipped) { if (player.angle < 45) player.angle += 100 * deltaTime; } else { if (player.angle > -45) player.angle -= 100 * deltaTime; } } //move down if (currentKeyStates[SDL_SCANCODE_S]) { //move the foreground if (FRGD.Pos.y > -1536 && player.Pos.y > 384 - 64) { FRGD.move(0, up, deltaTime); MDGD1.move(10, up, deltaTime); MDGD2.move(20, up, deltaTime); winBox->move(0, up, deltaTime); //pickups for (int i = 0; i < 2; i++) { ammo[i]->move(0, up, deltaTime); health[i]->move(0, up, deltaTime); } for (int i = 0; i < 3; i++) { bat[i]->move(0, up, deltaTime); } //bullets for (int i = 0; i < player.bulletList.size(); i++) { if (player.bulletList[i]->active) { player.bulletList[i]->move(0, up, deltaTime); } } //enemies for (int i = 0; i < 3; i++) { skull[i]->move(0, up, deltaTime); marksman[i]->move(0, up, deltaTime); for (int j = 0; j < marksman[i]->bulletList.size(); j++) { marksman[i]->bulletList[j]->move(0, up, deltaTime); } } } else if (player.Pos.y < 768 - 128) player.move(0, down, deltaTime); if (player.flipped) { if (player.angle > -90) player.angle -= 100 * deltaTime; } else { if (player.angle < 90) player.angle += 100 * deltaTime; } } //move left if (currentKeyStates[SDL_SCANCODE_A]) { //move the foreground if (player.Pos.x < 512 - 64 && FRGD.Pos.x < 1) { FRGD.move(0, right, deltaTime); MDGD1.move(10, right, deltaTime); MDGD2.move(20, right, deltaTime); winBox->move(0, right, deltaTime); //pickups for (int i = 0; i < 2; i++) { ammo[i]->move(0, right, deltaTime); health[i]->move(0, right, deltaTime); } for (int i = 0; i < 3; i++) { bat[i]->move(0, right, deltaTime); } //bullets for (int i = 0; i < player.bulletList.size(); i++) { if (player.bulletList[i]->active) { player.bulletList[i]->move(0, right, deltaTime); } } //enemies for (int i = 0; i < 3; i++) { skull[i]->move(0, right, deltaTime); marksman[i]->move(0, right, deltaTime); for (int j = 0; j < marksman[i]->bulletList.size(); j++) { marksman[i]->bulletList[j]->move(0, right, deltaTime); } } } else if (player.Pos.x > 0) player.move(0, left, deltaTime); if (player.flipped == false) { player.flipped = true; player.angle *= -1; } } //move right if (currentKeyStates[SDL_SCANCODE_D]) { //move the foreground if (player.Pos.x > 512 - 64 && FRGD.Pos.x > -2048) { FRGD.move(0, left, deltaTime); MDGD1.move(10, left, deltaTime); MDGD2.move(20, left, deltaTime); winBox->move(0, left, deltaTime); //pickups for (int i = 0; i < 2; i++) { ammo[i]->move(0, left, deltaTime); health[i]->move(0, left, deltaTime); } for (int i = 0; i < 3; i++) { bat[i]->move(0, left, deltaTime); } for (int i = 0; i < player.bulletList.size(); i++) { if (player.bulletList[i]->active) { player.bulletList[i]->move(0, left, deltaTime); } } //enemies for (int i = 0; i < 3; i++) { skull[i]->move(0, left, deltaTime); marksman[i]->move(0, left, deltaTime); for (int j = 0; j < marksman[i]->bulletList.size(); j++) { marksman[i]->bulletList[j]->move(0, left, deltaTime); } } } else if (player.Pos.x < 1024 - 128) player.move(0, right, deltaTime); if (player.flipped == true) { player.flipped = false; player.angle *= -1; } } //spacebar if (currentKeyStates[SDL_SCANCODE_SPACE]) { } //check for collision - ammo for (int i = 0; i < 2; i++) { if (SDL_HasIntersection(&player.Pos, &ammo[i]->Pos)) { if (ammo[i]->active) { player.ammoCount = 10; ammo[i]->active = false; } } //check for collision - health + if (SDL_HasIntersection(&player.Pos, &health[i]->Pos)) { if (health[i]->active) { player.currentHealth += 25; health[i]->active = false; } } } for (int i = 0; i < 3; i++) { //check for collision - item 1 if (SDL_HasIntersection(&player.Pos, &bat[i]->Pos)) { if (bat[i]->active) { player.batCount++; bat[i]->active = false; } } } //collision, player's bullets to enemies for (int i = 0; i < player.bulletList.size(); i++) { for (int j = 0; j < 3; j++) { if (player.bulletList[i]->active) { if (marksman[j]->health > 0) { //if it hits a marksman thats alive if (3000 //distance formula > ((player.bulletList[i]->Pos.x - (marksman[j]->Pos.x + marksman[j]->Pos.w * 3 / 4)) * (player.bulletList[i]->Pos.x - (marksman[j]->Pos.x + marksman[j]->Pos.w * 3 / 4)) + (player.bulletList[i]->Pos.y - (marksman[j]->Pos.y + marksman[j]->Pos.h / 2)) * (player.bulletList[i]->Pos.y - (marksman[j]->Pos.y + marksman[j]->Pos.h / 2)))) { player.bulletList[i]->reset(); marksman[j]->health--; marksman[j]->active = false; } } if (skull[j]->health > 0) { // if it hits a skull that is alive if (1600 //distance formula > ((player.bulletList[i]->Pos.x + 16 - (skull[j]->Pos.x + skull[j]->Pos.w / 2)) * (player.bulletList[i]->Pos.x + 16 - (skull[j]->Pos.x + skull[j]->Pos.w / 2)) + (player.bulletList[i]->Pos.y + 16 - (skull[j]->Pos.y + skull[j]->Pos.h / 2)) * (player.bulletList[i]->Pos.y + 16 - (skull[j]->Pos.y + skull[j]->Pos.h / 2)))) { player.bulletList[i]->reset(); skull[j]->health--; skull[j]->active = false; } } } } } //collision for enemies bullets to player for (int i = 0; i < 3; i++) { for (int j = 0; j < marksman[i]->bulletList.size(); j++) { if (marksman[i]->bulletList[j]->active) { if (3000 > (((player.Pos.x + player.Pos.w / 2) - (marksman[i]->bulletList[j]->Pos.x + marksman[i]->bulletList[j]->Pos.w / 2)) * ((player.Pos.x + player.Pos.w / 2) - (marksman[i]->bulletList[j]->Pos.x + marksman[i]->bulletList[j]->Pos.w / 2)) + ((player.Pos.y + player.Pos.h / 2) - (marksman[i]->bulletList[j]->Pos.y + marksman[i]->bulletList[j]->Pos.h / 2)) * ((player.Pos.y + player.Pos.h / 2) - (marksman[i]->bulletList[j]->Pos.y + marksman[i]->bulletList[j]->Pos.h / 2)))) { player.currentHealth -= 10; marksman[i]->bulletList[j]->reset(); } } } } //collision with chasing enemies to the player for (int i = 0; i < 3; i++) { if (skull[i]->active) { if (2500 > (((player.Pos.x + player.Pos.w / 2) - (skull[i]->Pos.x + skull[i]->Pos.w / 2)) * ((player.Pos.x + player.Pos.w / 2) - (skull[i]->Pos.x + skull[i]->Pos.w / 2)) + ((player.Pos.y + player.Pos.h / 2) - (skull[i]->Pos.y + skull[i]->Pos.h / 2)) * ((player.Pos.y + player.Pos.h / 2) - (skull[i]->Pos.y + skull[i]->Pos.h / 2)))) { if (SDL_GetTicks() > skull[i]->attackTime) { player.currentHealth -= 3; } } } } //Update player.update(deltaTime, mouseX, mouseY); for (int i = 0; i < 3; i++) { marksman[i]->update(deltaTime, player.Pos); skull[i]->update(deltaTime, player.Pos); } //pickups for (int i = 0; i < 2; i++) { ammo[i]->update(deltaTime); health[i]->update(deltaTime); health[i]->bobble(deltaTime); } //bats for (int i = 0; i < 3; i++) { bat[i]->update(deltaTime); } winBox->update(deltaTime); //provide the player with ammo or health if (ammo[0]->active == false && ammo[1]->active == false && player.ammoCount < 1) { int temp = rand() % 2; ammo[temp]->active = true; } //provide the player with ammo or health if (health[0]->active == false && health[1]->active == false && player.currentHealth < 70) { int temp = rand() % 2; health[temp]->active = true; } //foreground and midground FRGD.update(); MDGD1.update(); MDGD2.update(); if (player.currentHealth < 10) { state = LOSE; } //check win collision if (SDL_HasIntersection(&player.Pos, &winBox->Pos) && player.batCount == 3) { state = WIN; } //Draw //clear the renderer SDL_RenderClear(renderer); //Draw the BKGD BKGD.draw(renderer); //Draw the MDGD2 MDGD2.draw(renderer); //Draw the MDGD1 MDGD1.draw(renderer); //Draw the FRGD FRGD.draw(renderer); //draw the ammo for (int i = 0; i < 2; i++) { ammo[i]->draw(renderer); health[i]->draw(renderer); } for (int i = 0; i < 3; i++) { bat[i]->draw(renderer); } //draw enemy for (int i = 0; i < 3; i++) { marksman[i]->draw(renderer); skull[i]->draw(renderer); } //Draw the Player player.draw(renderer); //present renderer SDL_RenderPresent(renderer); break; } //end GAME case TITLE: { //check for input events if (SDL_PollEvent(&event)) { //if the window is closed if (event.type == SDL_QUIT) { quit = true; break; } } switch (event.type) { case SDL_MOUSEBUTTONDOWN: if (event.button.button == SDL_BUTTON_LEFT) { //collision if (SDL_HasIntersection(&mousePos, &playGame.Pos)) { playGame.ov = true; state = GAME; } else { playGame.ov = false; } //collision if (SDL_HasIntersection(&mousePos, &quitGame.Pos)) { quitGame.ov = true; quit = true; } else { quitGame.ov = false; } } break; case SDL_MOUSEMOTION: mouseX = event.button.x; mouseY = event.button.y; break; } //to quit the game if (currentKeyStates[SDL_SCANCODE_ESCAPE]) { //quitting SDL_Delay(200); quit = true; } //update mousePos.x = mouseX; mousePos.y = mouseY; //collision if (SDL_HasIntersection(&mousePos, &playGame.Pos)) playGame.ov = true; else playGame.ov = false; //collision if (SDL_HasIntersection(&mousePos, &quitGame.Pos)) quitGame.ov = true; else quitGame.ov = false; //Draw //clear the renderer SDL_RenderClear(renderer); BKGD.draw(renderer); title.draw(renderer); playGame.draw(renderer); quitGame.draw(renderer); //present renderer SDL_RenderPresent(renderer); break; } //end Title case WIN: { //check for input events if (SDL_PollEvent(&event)) { //if the window is closed if (event.type == SDL_QUIT) { quit = true; break; } } switch (event.type) { case SDL_MOUSEBUTTONDOWN: if (event.button.button == SDL_BUTTON_LEFT) { //collision if (SDL_HasIntersection(&mousePos, &playGame.Pos)) { playGame.ov = true; //reset everything player.currentHealth = 100; player.pos_X = 1; player.pos_Y = 320; BKGD.pos_X = 0; BKGD.pos_Y = 0; MDGD2.pos_X = 0; MDGD2.pos_Y = -728; MDGD1.pos_X = 0; MDGD1.pos_Y = -728; FRGD.pos_X = 0; FRGD.pos_Y = -728; ammo[0]->pos_X = 600; ammo[0]->pos_Y = 400; ammo[1]->pos_X = 1150; ammo[1]->pos_Y = 400; health[0]->pos_X = 350; health[0]->pos_Y = 350; health[1]->pos_X = 1885; health[1]->pos_Y = 350; bat[0]->pos_X = 500; bat[0]->pos_Y = -500; bat[0]->active = true; bat[1]->pos_X = 2700; bat[1]->pos_Y = -500; bat[1]->active = true; bat[2]->pos_X = 1800; bat[2]->pos_Y = 1000; bat[2]->active = true; marksman[0]->pos_X = 1175; marksman[0]->pos_Y = 240; marksman[0]->active = true; marksman[1]->pos_X = 1875; marksman[1]->pos_Y = 240; marksman[1]->active = true; marksman[2]->pos_X = 2515; marksman[2]->pos_Y = 240; marksman[2]->active = true; skull[0]->pos_X = 400; skull[0]->pos_Y = -500; skull[0]->active = true; skull[1]->pos_X = 2600; skull[1]->pos_Y = -500; skull[1]->active = true; skull[2]->pos_X = 1700; skull[2]->pos_Y = 1000; skull[2]->active = true; state = GAME; } else { playGame.ov = false; } //collision if (SDL_HasIntersection(&mousePos, &quitGame.Pos)) { quitGame.ov = true; quit = true; } else { quitGame.ov = false; } } break; case SDL_MOUSEMOTION: mouseX = event.button.x; mouseY = event.button.y; break; } //to quit the game if (currentKeyStates[SDL_SCANCODE_ESCAPE]) { //quitting SDL_Delay(200); quit = true; } //update mousePos.x = mouseX; mousePos.y = mouseY; //collision if (SDL_HasIntersection(&mousePos, &playGame.Pos)) playGame.ov = true; else playGame.ov = false; //collision if (SDL_HasIntersection(&mousePos, &quitGame.Pos)) quitGame.ov = true; else quitGame.ov = false; //Draw //clear the renderer SDL_RenderClear(renderer); win.draw(renderer); playGame.draw(renderer); quitGame.draw(renderer); //present renderer SDL_RenderPresent(renderer); break; } //end WIN case LOSE: { //check for input events if (SDL_PollEvent(&event)) { //if the window is closed if (event.type == SDL_QUIT) { quit = true; break; } } switch (event.type) { case SDL_MOUSEBUTTONDOWN: if (event.button.button == SDL_BUTTON_LEFT) { //collision if (SDL_HasIntersection(&mousePos, &playGame.Pos)) { playGame.ov = true; //reset everything player.currentHealth = 100; player.pos_X = 1; player.pos_Y = 320; BKGD.pos_X = 0; BKGD.pos_Y = 0; MDGD2.pos_X = 0; MDGD2.pos_Y = -728; MDGD1.pos_X = 0; MDGD1.pos_Y = -728; FRGD.pos_X = 0; FRGD.pos_Y = -728; ammo[0]->pos_X = 600; ammo[0]->pos_Y = 400; ammo[1]->pos_X = 1150; ammo[1]->pos_Y = 400; health[0]->pos_X = 350; health[0]->pos_Y = 350; health[1]->pos_X = 1885; health[1]->pos_Y = 350; bat[0]->pos_X = 500; bat[0]->pos_Y = -500; bat[0]->active = true; bat[1]->pos_X = 2700; bat[1]->pos_Y = -500; bat[1]->active = true; bat[2]->pos_X = 1800; bat[2]->pos_Y = 1000; bat[2]->active = true; marksman[0]->pos_X = 1175; marksman[0]->pos_Y = 240; marksman[0]->active = true; marksman[1]->pos_X = 1875; marksman[1]->pos_Y = 240; marksman[1]->active = true; marksman[2]->pos_X = 2515; marksman[2]->pos_Y = 240; marksman[2]->active = true; skull[0]->pos_X = 400; skull[0]->pos_Y = -500; skull[0]->active = true; skull[1]->pos_X = 2600; skull[1]->pos_Y = -500; skull[1]->active = true; skull[2]->pos_X = 1700; skull[2]->pos_Y = 1000; skull[2]->active = true; state = GAME; } else { playGame.ov = false; } //collision if (SDL_HasIntersection(&mousePos, &quitGame.Pos)) { quitGame.ov = true; quit = true; } else { quitGame.ov = false; } } break; case SDL_MOUSEMOTION: mouseX = event.button.x; mouseY = event.button.y; break; } //to quit the game if (currentKeyStates[SDL_SCANCODE_ESCAPE]) { //quitting SDL_Delay(200); quit = true; } //update mousePos.x = mouseX; mousePos.y = mouseY; //collision if (SDL_HasIntersection(&mousePos, &playGame.Pos)) playGame.ov = true; else playGame.ov = false; //collision if (SDL_HasIntersection(&mousePos, &quitGame.Pos)) quitGame.ov = true; else quitGame.ov = false; //Draw //clear the renderer SDL_RenderClear(renderer); lose.draw(renderer); playGame.draw(renderer); quitGame.draw(renderer); //present renderer SDL_RenderPresent(renderer); break; } //End LOSE default: break; } //end switch } for (int i = 0; i < 3; i++) { delete skull[i]; delete bat[i]; delete marksman[i]; } for (int i = 0; i < 2; i++) { delete ammo[i]; delete health[i]; } // Close and destroy the window SDL_DestroyWindow(window); // Clean up SDL_Quit(); return 0; }