Vector2D Player::handleMovement() { Vector2D newVelocity; newVelocity.setX(0); newVelocity.setY(0); //Move up if (InputManager::instance()->isKeyDown(SDL_SCANCODE_W) && m_position.getY() >= 0) { newVelocity.setY(-m_movementSpeed); } //Move right else if (InputManager::instance()->isKeyDown(SDL_SCANCODE_D) && !CollisionHandler::gameBoundsWidth(this)) { newVelocity.setX(m_movementSpeed); } //Move down else if (InputManager::instance()->isKeyDown(SDL_SCANCODE_S) && !CollisionHandler::gameBoundsHeight(this)) { newVelocity.setY(m_movementSpeed); } //Move left else if (InputManager::instance()->isKeyDown(SDL_SCANCODE_A) && m_position.getX() >= 0) { newVelocity.setX(-m_movementSpeed); } return newVelocity; }
void Animal::handleCollisions(Environment *environment) { Vector2D boundsCorrection; if (getBounds().getMinX() < environment->getBounds().getMinX()) { boundsCorrection.setX(environment->getBounds().getMinX() - getBounds().getMinX()); } if (getBounds().getMinY() < environment->getBounds().getMinY()) { boundsCorrection.setY(environment->getBounds().getMinY() - getBounds().getMinY()); } if (getBounds().getMaxX() > environment->getBounds().getMaxX()) { boundsCorrection.setX(environment->getBounds().getMaxX() - getBounds().getMaxX()); } if (getBounds().getMaxY() > environment->getBounds().getMaxY()) { boundsCorrection.setY(environment->getBounds().getMaxY() - getBounds().getMaxY()); } translate(boundsCorrection); std::vector<Object *> nearestObjects = environment->getNearestObjects(this); for (int k = 0; k < nearestObjects.size(); k++) { Object *nearestObject = nearestObjects[k]; double distance = getPosition().distanceTo(nearestObject->getPosition()); double radius = (OBJECT_WIDTH / 2); if (distance < 2.0 * radius) { Vector2D overlapVector = Vector2D(nearestObject->getPosition(), getPosition()); overlapVector = overlapVector.normalize(); double overlapLength = 2.0 * radius - distance; overlapVector = overlapVector.mult(overlapLength); translate(overlapVector); handleCollision(environment, nearestObject); } } }
bool Circle::calcTangentIntersectionPoints(const Vector2D startPoint, Vector2D &point1, Vector2D &point2){ if(isInside(startPoint)){ // Startpoint is inside circle -> there are no tangent interception points return(false); } //float d = posCenter.getLength()-startPoint.getLength(); float d = (posCenter-startPoint).getLength(); float r = radius; float alphaRad = asin(r/d); float p = sqrt(d*d-r*r); point1.setX(cos(alphaRad)*p); point1.setY(sin(alphaRad)*p); point2.setX(cos(-alphaRad)*p); point2.setY(sin(-alphaRad)*p); point1=point1.rotate((posCenter-startPoint).getDirection()); point2=point2.rotate((posCenter-startPoint).getDirection()); point1+=startPoint; point2+=startPoint; return(true); }
void Items::GenerateItemNow( Vector2D pos, Vector2D vel ) { if( pos.getX() < 10 ) pos.setX( 10.f ); if( pos.getX() > m_iScreenWidth-10 ) pos.setX( m_iScreenWidth-10.f ); if( pos.getY() < 100 && vel.getY() < 5 ) vel.setY( 5 ); int itemType = GetRandValue( ITEM_APPEAR_CHANCES, NR_ITEM_TYPES ); Item *item = new Item( pos, vel, (ItemTypes)itemType, m_iItemLifeTime, m_iScreenWidth ); AddItem( item ); }
Oro::Oro(int x,int y) { TheTextureManager::Instance()->load("assets/resources.png","oro", TheGame::Instance()->getRenderer()); float possx = (x+1) * TheGame::TILE_WIDTH/2; float possy = (y+1) * TheGame::TILE_HEIGHT; Vector2D* vec = new Vector2D(0,0); vec->setX(possx); vec->setY(possy); vec->toIsometric(); GameObject::load( vec->getX(), vec->getY(), width, height, destWidth, destHeight, numFrames, "oro",false); m_mapPosition2.setX(x); m_mapPosition2.setY(y); GameObject::setFrame(frame); GameObject::setOffset(offsetX,offsetY); GameObject::setRow(row); GameObject::setAlto(1); GameObject::setAncho(1); this->recurso = true; this->name = "Oro"; this->cantidad = 1; delete vec; }
bool GameObject::tileCollisions(Vector2D pos){ for(vector<TileLayer*>::iterator i = collisionLayers->begin(); i != collisionLayers->end(); i++){ TileLayer* tileLayer = (*i); vector<vector<int>> tiles = tileLayer->getTileIDs(); Vector2D layerPos = tileLayer->getPosition(); int x, y, tileColumn, tileRow, tileid = 0; x = layerPos.X() / tileLayer->getTileSize(); y = layerPos.Y() / tileLayer->getTileSize(); Vector2D startPos = pos; startPos.setX( startPos.X() + 3); startPos.setY( startPos.Y() + 4); Vector2D endPos(pos.X() + collisionWidth, pos.Y() + collisionHeight - 4); for(int i = startPos.X(); i < endPos.X(); i++) { for(int j = startPos.Y(); j < endPos.Y(); j++) { tileColumn = i / tileLayer->getTileSize(); tileRow = j / tileLayer->getTileSize(); tileid = tiles[tileRow + y][tileColumn + x]; if(tileid != 0) { return true; } } } } return false; }
Vector2D<T> operator+(Vector2D<T> const& cpy) { Vector2D<T> v; v.setX(this->_table[0] + cpy._table[0]); v.setY(this->_table[1] + cpy._table[1]); return (v); }
const Vector2D Vector2D::operator/(double scalar) const { Vector2D result; result.setX(this->x / scalar); result.setY(this->y / scalar); return result; }
// Add/Subtract two Vector2D objects const Vector2D Vector2D::operator+(const Vector2D& otherVector) const { Vector2D result; result.setX(this->x + otherVector.x); result.setY(this->y + otherVector.y); return result; }
void Level::addObject(GameObject* gameObject, int x, int y) { Vector2D virtualPosition; virtualPosition.setX(x); //El cero esta en la pos alto de ventana e incrementa hacia arriba virtualPosition.setY(Game::Instance()->getGameHeight() - y - gameObject->getHeight()); gameObject->setPosition(virtualPosition); m_levelObjects.push_back(gameObject); }
void InputHandler::update() { SDL_Event event; while (SDL_PollEvent(&event)) { m_keystates = SDL_GetKeyboardState(0); if (event.type == SDL_MOUSEBUTTONDOWN) { if (event.button.button == SDL_BUTTON_LEFT) { m_mouseButtonStates[LEFT] = true; } if (event.button.button == SDL_BUTTON_MIDDLE) { m_mouseButtonStates[MIDDLE] = true; } if (event.button.button == SDL_BUTTON_RIGHT) { m_mouseButtonStates[RIGHT] = true; } } if (event.type == SDL_MOUSEBUTTONUP) { if (event.button.button == SDL_BUTTON_MIDDLE) { m_mouseButtonStates[MIDDLE] = false; } if (event.button.button == SDL_BUTTON_RIGHT) { m_mouseButtonStates[RIGHT] = false; } if (event.button.button == SDL_BUTTON_LEFT) { m_mouseButtonStates[LEFT] = false; } } if (event.type == SDL_MOUSEMOTION) { m_mousePosition.setX(event.motion.x); m_mousePosition.setY(event.motion.y); } } if (event.type == SDL_MOUSEBUTTONUP) { if (event.button.button == SDL_BUTTON_LEFT) { m_mouseButtonStates[LEFT] = false; } } }
///MAIN METHOD int main(){ Vector2D A; int Xa, Ya; cout <<"Enter X coordinate of vector A: " ; cin >>Xa;// please enter ONLY INTEGERS A.setX(Xa); cout <<"Enter Y coordinate of vector A: " ; cin >>Ya;// please enter ONLY INTEGERS A.setY(Ya); cout << endl; Vector2D B;// please enter ONLY INTEGERS int Xb, Yb; cout <<"Enter X coordinate of vector B: " ; cin >>Xb;// please enter ONLY INTEGERS B.setX(Xb); cout <<"Enter Y coordinate of vector B: " ; cin >>Yb;// please enter ONLY INTEGERS B.setY(Yb); cout << endl<< "The dot product of vector A and B is: " << A.getX() << " * " << B.getX() <<" + " << A.getY() << " * " << B.getY() <<" = " << (A * B) << endl; cout << endl<< "The sum of vector A and vector B is: "; (A + B).display(); return 0; }
void Items::generateItemNow( Vector2D pos, Vector2D vel ) { if ( pos.getX() < 10 ) pos.setX( 10 ); if ( pos.getX() > SCREEN_WIDTH-10 ) pos.setX( SCREEN_WIDTH-10 ); if ( pos.getY() < 100 && vel.getY() < 5 ) vel.setY( 5 ); int itemType; // 10 tries for a correct item for ( int i = 0; i < 10; i++ ) { itemType = getRandValue( ITEM_APPEAR_CHANCES, NR_ITEM_TYPES ); if ( ( racers->isShipTypeActive( LIGHT_FIGHTER ) && racers->isShipTypeActive( HEAVY_FIGHTER ) ) || ( racers->isShipTypeActive( LIGHT_FIGHTER ) && ( itemType == ITEM_PRIMARY_UPGRADE || itemType == ITEM_DUMBFIRE_DOUBLE || itemType == ITEM_KICK_ASS_ROCKET || itemType == ITEM_HELLFIRE || itemType == ITEM_MACHINE_GUN || itemType == ITEM_HEALTH || itemType == ITEM_HEATSEEKER || itemType == ITEM_NUKE || itemType == ITEM_ENERGY_BEAM ) ) || ( racers->isShipTypeActive( HEAVY_FIGHTER ) && ( itemType == ITEM_PRIMARY_UPGRADE || itemType == ITEM_DUMBFIRE_DOUBLE || itemType == ITEM_KICK_ASS_ROCKET || itemType == ITEM_HEALTH || itemType == ITEM_HEATSEEKER || itemType == ITEM_NUKE || itemType == ITEM_DEFLECTOR || itemType == ITEM_LASER ) ) ) { Item *item = new Item( pos, vel, (ItemTypes)itemType ); addItem( item ); break; } } }
void Pantalla::draw(SDL_Renderer* m_pRenderer, Map* m_pMap ,vector<GameObject*> entidades) { SDL_Rect sector; ////////////////////////////////////////////////////////////////// sector = sectores.at("barra"); TheTextureManager::Instance()->drawArea("barra",sector,m_pRenderer); SDL_Color textColor = { 255, 255, 255 }; map<string, int>myMap = TheGame::Instance()->m_pBarra->getMapRecursos(); int acum =0; for (const auto& recurso : myMap) { ostringstream convert; // stream used for the conversion convert << (recurso.second); string text = recurso.first+ ": " + convert.str(); sector.x=5+acum; sector.y=5; acum+= 100; TheTextureManager::Instance()->drawText(text,textColor,sector,m_pRenderer); } ////////////////////////////////////////////////////////////////// sector = sectores.at("barra_bajo"); TheTextureManager::Instance()->drawArea("barra_bajo",sector,m_pRenderer); textColor = { 0, 0, 0 }; for (uint i=0; i<entidades.size(); i++) { if (entidades[i]->m_isClicked) { string text = "NOMBRE: " + entidades[i]->name; sector.x = 20; sector.y= 30; TheTextureManager::Instance()->drawText(text,textColor,sector,m_pRenderer); text = "DESCRIPCION: " + entidades[i]->descripcion; sector.x = 20; sector.y += 30; TheTextureManager::Instance()->drawText(text,textColor,sector,m_pRenderer); } } ////////////////////////////////////////////////////////////////// sector = sectores.at("minimapa"); //TheTextureManager::Instance()->drawArea("minimapa",sector,m_pRenderer); SDL_RenderSetViewport( m_pRenderer, §or ); for(int i = 0 ; i < m_pMap->getMapSize().getX(); i++) { for(int j = 0 ; j < m_pMap->getMapSize().getY() ; j++) { if (m_pMap->getVisionMapValue(i,j) == 1 || m_pMap->getVisionMapValue(i,j) == 2) { Vector2D* vector = new Vector2D(0,0); vector->setX(i); vector->setY(j); vector->toIsometric(); int tileValue1 = TheGame::Instance()->m_pMap->getValue(i,j); if (tileValue1 == 3 ) { TheTextureManager::Instance()->drawFrame("puntoRojo",vector->getX()+95,vector->getY()-5,180,150,50,50,1,0,m_pRenderer); } else if (tileValue1 == 0) { TheTextureManager::Instance()->drawFrame("puntoAmarillo",vector->getX()+95,vector->getY()-5,180,150,50,50,1,0,m_pRenderer); } else TheTextureManager::Instance()->drawFrame("puntoVerde",vector->getX()+100,vector->getY(),180,150,50,50,1,0,m_pRenderer); delete vector; } } } ////////////////////////////////////////////////////////////////// sector = sectores.at("mapa"); SDL_RenderSetViewport( m_pRenderer, §or ); m_pMap->draw(); //primero dibuja entidades, luego el personaje (siempre aparece por arriba de las cosas for (uint i=0; i<entidades.size(); i++) { if (entidades[i]->m_isClicked) { entidades[i]->drawSelected(); } //SDL_RenderSetViewport( m_pRenderer, §or ); if (entidades[i] && (entidades[i]->m_atSight || entidades[i]->m_wasSeen)) entidades[i]->draw(); } SDL_RenderPresent(m_pRenderer); ////////////////////////////////////////////////////////////////// }
void Enemy::load(const LoaderParams *pParams) { GameObject::load(pParams); m_originalVelocity.setX(m_velocity.getX()); }
void AddKinematicUnitNearPlayerMessage::process() { Vector2D vel(0.0f, 0.0f); int theta = (rand() % 360); int opp = sin(theta) * mRadius; int adj = cos(theta) * mRadius; Vector2D playerVector = UNIT_MANAGER->getKinematicUnit("player")->getPosition(); Vector2D spawnPos = Vector2D((playerVector.getX() + adj) + mRadius, (playerVector.getY() + opp) + mRadius); spawnPos.setX(spawnPos.getX() - (playerVector.getX() / 2)); spawnPos.setY(spawnPos.getY() - (playerVector.getY() / 2)); /* Distance confirmation (It's close enough). int x_dist = abs(playerVector.getX() - spawnPos.getX()); int y_dist = abs(playerVector.getY() - spawnPos.getY()); std::cout << sqrt((x_dist * x_dist) + (y_dist * y_dist)) << std::endl; */ KinematicUnit *temp = new KinematicUnit(gpGame->getSpriteManager()->getSprite(2), mTypeArray[mSteerType] + "AI", spawnPos, 1.0f, vel, 1.0f, 180.0f, 200.0f); switch (mSteerType) { case 0: temp->dynamicSeek(UNIT_MANAGER->getKinematicUnit("player")); break; case 1: temp->dynamicArrive(UNIT_MANAGER->getKinematicUnit("player")); break; case 2: temp->dynamicFlee(UNIT_MANAGER->getKinematicUnit("player")); break; case 3: temp->dynamicWander(); break; case 4: temp->dynamicAlign(UNIT_MANAGER->getKinematicUnit("player")); break; case 5: temp->dynamicPursue(UNIT_MANAGER->getKinematicUnit("player")); break; case 6: temp->dynamicFace(UNIT_MANAGER->getKinematicUnit("player")); break; case 7: temp->dynamicWanderAndSeek(UNIT_MANAGER->getKinematicUnit("player"), 100); break; case 8: temp->dynamicWanderAndFlee(UNIT_MANAGER->getKinematicUnit("player"), 100); break; case 9: temp->dynamicSeparation(); break; case 10: temp->dynamicCohesion(); break; case 11: temp->dynamicFlocking(); break; case 12: temp->dynamicVelocityMatch(UNIT_MANAGER->getKinematicUnit("player")); break; } UNIT_MANAGER->addKinematicUnit(temp); }
void Unit::handleInput() { // deberia checkear tambien si la unidad está seleccionada. La variable isSelected la puse en GameObject, ya que consideré que // se puede apretar en cualquier objeto, como en el age. //TEMPORAL, DEBUG TP 2 /////////////////////////////////////////// if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_SPACE) && m_isClicked) { TheCamera::Instance()->centerAt(m_screenPosition); } //////////////////////////////////////////////////////////////////// if (TheInputHandler::Instance()->getMouseButtonState(RIGHT) && m_isClicked) { if (!m_bChangingDestination) { //El click no puede darse fuera del panel del mapa, se pueden poner funcionalidades del menú SDL_Rect sector = TheGame::Instance()->m_pPantalla->sectores.at("mapa"); if ((TheInputHandler::Instance()->getMousePosition()->getY() >= sector.y + sector.h ) || (TheInputHandler::Instance()->getMousePosition()->getY() <= sector.y )){ return; } float backUpCartesianX = m_destination.getX(); float backUpCartesianY = m_destination.getY(); //Calcula la coordenada a moverse en coordenadas cartesianas de mapa float coordX = (TheInputHandler::Instance()->getMousePosition()->getX() + TheCamera::Instance()->offsetX) - (TheGame::Instance()->TILE_WIDTH/2); float coordY = (TheInputHandler::Instance()->getMousePosition()->getY() + TheCamera::Instance()->offsetY) - (TheGame::Instance()->TILE_HEIGHT/2); //vector para que guarda temporalmente la posicion a donde se quiere mover el jugador Vector2D* goalDestination = new Vector2D(); goalDestination->setX(coordX); goalDestination->setY(coordY); goalDestination->screenToWorld(); goalDestination->m_x-=1; goalDestination->m_y-=1; cout << "Tile clicked = ( " << (int) goalDestination->m_x << " , " << (int)goalDestination->m_y << " ) \n"; //cout << "Screen pos clicked = ( " << (int) coordX << " , " << (int)coordY << " ) \n"; if (((m_mapPosition.getX() == goalDestination->getX()) && (m_mapPosition.getY() == goalDestination->getY()))) return; //analiza que no se cliquee fuera de los bordes if ((goalDestination->getX() >= TheGame::Instance()->getMapWidth()) || (goalDestination->getY() >= TheGame::Instance()->getMapHeight())|| (goalDestination->getX() < 0) || (goalDestination->getY() < 0)) { //revierte en caso positivo m_bChangingDestination = false; m_destination.setX(backUpCartesianX); m_destination.setY(backUpCartesianY); if (goalDestination) delete goalDestination; return; } //Si pasa las condiciones, calcula el path m_bChangingDestination = true; bool pathFound = calculatePath(*goalDestination); delete goalDestination; //si encuentra camino setea el comienzo del movimiento if (pathFound) { m_node = astarsearch.GetSolutionStart(); //actualiza la ultima posicion TheGame::Instance()->changeMapGrid(m_lastMapPosition.getX(), m_lastMapPosition.getY(), 1); //cout << "Liberada = ( " << (int) m_lastMapPosition.m_x << " , " << (int)m_lastMapPosition.m_y << " ) \n"; m_lastMapPosition.setX(m_mapPosition.getX()); m_lastMapPosition.setY(m_mapPosition.getY()); if (!m_node) { m_bMoving = false; astarsearch.FreeSolutionNodes(); return; } //cout << "First node = ( " << (int) m_node->x << " , " << (int)m_node->y << " ) \n"; m_destination.setX(m_node->x); m_destination.setY(m_node->y); } m_bMoving = pathFound; } } else { m_bChangingDestination = false; } }