void HUD::draw() { StaticGameEntity::draw(); _healthBar->draw(); _shootBar->draw(); float windowHeight = getMasterInstance()->_window->getHeight(); float windowWidth = getMasterInstance()->_window->getWidth(); float scaleFactor = 0.40f; this->setPosition(Position3D(windowWidth / 2 - (getINDIEntity()->getRegionWidth()*0.5f) / 2, 0, this->getPosition().getZ())); this->getINDIEntity()->setScale(0.5f, 0.50f); float y = windowHeight - (_healthBar->getINDIEntity()->getRegionHeight() * scaleFactor); _healthBar->setPosition(Position3D(0, y, this->getPosition().getZ())); float x = windowWidth - _shootBar->getINDIEntity()->getRegionWidth() * scaleFactor; _shootBar->setPosition(Position3D(x, y, this->getPosition().getZ())); _healthBar->getINDIEntity()->setScale(scaleFactor, scaleFactor); _shootBar->getINDIEntity()->setScale(scaleFactor, scaleFactor); }
void GfxTerrain::updateAABB() { mesh->aabb = AABB(Position3D(sizeInChunks/2.0f * -chunkSize, 0.0f, sizeInChunks/2.0f * -chunkSize), Position3D(sizeInChunks/2.0f * chunkSize, scale, sizeInChunks/2.0f * chunkSize)); }
HUD::HUD(CIndieLib* masterInstance, Position3D position, std::string resource, IND_Surface* surface, SpriteCordinateMapper* spriteMapper, float* deltaTime) :StaticGameEntity(masterInstance, position, resource, surface, spriteMapper, deltaTime) { _healthBarImages = { "red reversed 6", "red reversed 5", "red reversed 4", "red reversed 3", "red reversed 2", "red reversed 1", "red reversed all" }; _healthBar = new StaticGameEntity(masterInstance, Position3D(10000, 10000, position.getZ()), "red reversed all", surface, spriteMapper, deltaTime); _shootBar = new StaticGameEntity(masterInstance, Position3D(10000, 10000, position.getZ()),"orange reversed all", surface, spriteMapper, deltaTime); }
void GameEntity::deserializeEntity(std::string jsonObject) { int positionObjectIndex = jsonObject.find(_positionKey) + 2; std::size_t firstCloseBreket = jsonObject.find_first_of('}'); std::string positionObject = jsonObject.substr((positionObjectIndex + _positionKey.length()), (firstCloseBreket - (positionObjectIndex + _positionKey.length()))); float x = 0, y = 0, z = 0; vector<std::string> positionParts = Common::splitString(positionObject, ','); for (int i = 0; i < positionParts.size(); i++) { vector<std::string> parts = Common::splitString(positionParts[i], ':'); if (Common::trimString(parts[0]) == "x") x = (float)Common::StringToInt(parts[1]); if (Common::trimString(parts[0]) == "y") y = (float)Common::StringToInt(parts[1]); if (Common::trimString(parts[0]) == "z") z = (float)Common::StringToInt(parts[1]); } this->setPosition(Position3D(x, y, z)); vector<std::string> allObjects = Common::splitString(jsonObject, ','); for (int i = 0; i < allObjects.size(); i++) { vector<std::string> parts = Common::splitString(allObjects[i], ':'); if (Common::trimString(parts[0]) == _angleZKey) { _angleZ = (float)Common::StringToInt(Common::trimString(parts[1])); } if (Common::trimString(parts[0]) == _resourcePathKey) { _tempResourcePath = Common::trimString(parts[1].substr(0, parts[1].find("}") - 1)); this->_resourcePath = _tempResourcePath.c_str(); } } }
//------------------------------------------------------------------------------------- int NavTileHandle::raycast(int layer, const Position3D& start, const Position3D& end, std::vector<Position3D>& hitPointVec) { setMapLayer(layer); pCurrNavTileHandle = this; if(pCurrNavTileHandle->pTilemap->GetNumLayers() < layer + 1) { ERROR_MSG(fmt::format("NavTileHandle::raycast: not found layer({})\n", layer)); return NAV_ERROR; } // Create a start state MapSearchNode nodeStart; nodeStart.x = int(start.x / pTilemap->GetTileWidth()); nodeStart.y = int(start.z / pTilemap->GetTileHeight()); // Define the goal state MapSearchNode nodeEnd; nodeEnd.x = int(end.x / pTilemap->GetTileWidth()); nodeEnd.y = int(end.z / pTilemap->GetTileHeight()); std::vector<MapSearchNode> vec; bresenhamLine(nodeStart, nodeEnd, vec); if(vec.size() > 0) { vec.erase(vec.begin()); } std::vector<MapSearchNode>::iterator iter = vec.begin(); for(; iter != vec.end(); iter++) { if(getMap((*iter).x, (*iter).y) == TILE_STATE_CLOSED) break; hitPointVec.push_back(Position3D(float((*iter).x * pTilemap->GetTileWidth()), start.y, float((*iter).y * pTilemap->GetTileWidth()))); } return 1; }
void GameEntity::_move(Position2D newPosition, bool lockInWindowScreen) { float x = newPosition.getX(); float y = newPosition.getY(); if (lockInWindowScreen) { _width = this->getINDIEntity()->getRegionWidth() / 2 - 18; _height = this->getINDIEntity()->getRegionHeight() / 2 - 15; if (x + _width > _masterInstance->_window->getWidth()) x = _masterInstance->_window->getWidth() - _width; if (x - _width < 0) x = 0 + _width; if (y + _height > _masterInstance->_window->getHeight()) y = _masterInstance->_window->getHeight() - _height; if (y - _height < 0) y = _height; } this->setPosition(Position3D(x, y, this->getPosition().getZ())); }
//------------------------------------------------------------------------------------- int NavMeshHandle::raycast(int layer, const Position3D& start, const Position3D& end, std::vector<Position3D>& hitPointVec) { std::map<int, NavmeshLayer>::iterator iter = navmeshLayer.find(layer); if(iter == navmeshLayer.end()) { ERROR_MSG(fmt::format("NavMeshHandle::raycast: not found layer({})\n", layer)); return NAV_ERROR; } dtNavMeshQuery* navmeshQuery = iter->second.pNavmeshQuery; float hitPoint[3]; float spos[3]; spos[0] = start.x; spos[1] = start.y; spos[2] = start.z; float epos[3]; epos[0] = end.x; epos[1] = end.y; epos[2] = end.z; dtQueryFilter filter; filter.setIncludeFlags(0xffff); filter.setExcludeFlags(0); const float extents[3] = {2.f, 4.f, 2.f}; dtPolyRef startRef = INVALID_NAVMESH_POLYREF; float nearestPt[3]; navmeshQuery->findNearestPoly(spos, extents, &filter, &startRef, nearestPt); if (!startRef) { return NAV_ERROR_NEARESTPOLY; } float t = 0; float hitNormal[3]; memset(hitNormal, 0, sizeof(hitNormal)); dtPolyRef polys[MAX_POLYS]; int npolys; navmeshQuery->raycast(startRef, spos, epos, &filter, &t, hitNormal, polys, &npolys, MAX_POLYS); if (t > 1) { // no hit return NAV_ERROR; } else { // Hit hitPoint[0] = spos[0] + (epos[0] - spos[0]) * t; hitPoint[1] = spos[1] + (epos[1] - spos[1]) * t; hitPoint[2] = spos[2] + (epos[2] - spos[2]) * t; if (npolys) { float h = 0; navmeshQuery->getPolyHeight(polys[npolys-1], hitPoint, &h); hitPoint[1] = h; } } hitPointVec.push_back(Position3D(hitPoint[0], hitPoint[1], hitPoint[2])); return 1; }
//------------------------------------------------------------------------------------- int NavTileHandle::findStraightPath(int layer, const Position3D& start, const Position3D& end, std::vector<Position3D>& paths) { setMapLayer(layer); pCurrNavTileHandle = this; if(pCurrNavTileHandle->pTilemap->GetNumLayers() < layer + 1) { ERROR_MSG(fmt::format("NavTileHandle::findStraightPath: not found layer({})\n", layer)); return NAV_ERROR; } // Create a start state nodeStart.x = int(start.x / pTilemap->GetTileWidth()); nodeStart.y = int(start.z / pTilemap->GetTileHeight()); // Define the goal state nodeGoal.x = int(end.x / pTilemap->GetTileWidth()); nodeGoal.y = int(end.z / pTilemap->GetTileHeight()); //DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: start({}, {}), end({}, {})\n", // nodeStart.x, nodeStart.y, nodeGoal.x, nodeGoal.y)); // Set Start and goal states astarsearch.SetStartAndGoalStates(nodeStart, nodeGoal); unsigned int SearchState; unsigned int SearchSteps = 0; do { SearchState = astarsearch.SearchStep(); SearchSteps++; #if DEBUG_LISTS DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: Steps: {}\n", SearchSteps)); int len = 0; DEBUG_MSG("NavTileHandle::findStraightPath: Open:\n"); MapSearchNode *p = astarsearch.GetOpenListStart(); while( p ) { len++; #if !DEBUG_LIST_LENGTHS_ONLY ((MapSearchNode *)p)->printNodeInfo(); #endif p = astarsearch.GetOpenListNext(); } DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: Open list has {} nodes\n", len)); len = 0; DEBUG_MSG("NavTileHandle::findStraightPath: Closed:\n"); p = astarsearch.GetClosedListStart(); while( p ) { len++; #if !DEBUG_LIST_LENGTHS_ONLY p->printNodeInfo(); #endif p = astarsearch.GetClosedListNext(); } DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: Closed list has {} nodes\n", len)); #endif } while( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING ); if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED ) { //DEBUG_MSG("NavTileHandle::findStraightPath: Search found goal state\n"); MapSearchNode *node = astarsearch.GetSolutionStart(); int steps = 0; //node->PrintNodeInfo(); for( ;; ) { node = astarsearch.GetSolutionNext(); if( !node ) { break; } //node->PrintNodeInfo(); steps ++; paths.push_back(Position3D((float)node->x * pTilemap->GetTileWidth(), 0, (float)node->y * pTilemap->GetTileWidth())); }; // DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: Solution steps {}\n", steps)); // Once you're done with the solution you can free the nodes up astarsearch.FreeSolutionNodes(); } else if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_FAILED ) { ERROR_MSG("NavTileHandle::findStraightPath: Search terminated. Did not find goal state\n"); } // Display the number of loops the search went through // DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: SearchSteps: {}\n", SearchSteps)); astarsearch.EnsureMemoryFreed(); return 0; }
void setPlayerPosition(float x, float y, float z){ entityPos_ = Position3D(x, y, z); }
Position3D Position3D::operator* (float pVal) const { return Position3D(x*pVal, y*pVal, z*pVal); }
/* ================== Main ================== */ int IndieLib() { //Sets the working path as the 'exe' directory. All resource paths are relative to this directory CIndieLib *mI = CIndieLib::instance(); if (!mI->init()) return 0; AnimatedGameEntity* shipExplotion = new AnimatedGameEntity(mI, Position3D(0, 0, 1), "../SpaceGame/resources/animations/Ship_Explotion.xml"); shipExplotion->Draw(); AnimatedGameEntity* shipRotate = new AnimatedGameEntity(mI, Position3D(0, 0, 0), "../SpaceGame/resources/animations/kali/three_oclock.xml"); shipExplotion->Draw(); //mDelta = mI->_render->getFrameTime() / 1000.0f; IND_Surface *mSurfaceBeetleship = IND_Surface::newSurface(); if (!mI->_surfaceManager->add(mSurfaceBeetleship, "../../SpaceGame/resources/beetleship.png", IND_ALPHA, IND_32)) return 0; //ufo->setSequence(0); // Font IND_Font *mFontSmall = IND_Font::newFont(); if (!mI->_fontManager->add(mFontSmall, "../../SpaceGame/resources/font_small.png", "../../resources/font_small.xml", IND_ALPHA, IND_32)) return 0; // Creating 2d entity for the bettleship IND_Entity2d *mBeetleship = IND_Entity2d::newEntity2d(); mI->_entity2dManager->add(mBeetleship); // Entity adding mBeetleship->setSurface(mSurfaceBeetleship); // Set the surface into the entity // ----- Font creation ----- IND_Entity2d *mTextSmallWhite = IND_Entity2d::newEntity2d(); mI->_entity2dManager->add(mTextSmallWhite); // Entity adding mTextSmallWhite->setFont(mFontSmall); // Set the font into the entity mTextSmallWhite->setLineSpacing(18); mTextSmallWhite->setCharSpacing(-8); mTextSmallWhite->setPosition(5, 5, 1); mTextSmallWhite->setAlign(IND_LEFT); mBeetleship->setHotSpot(0.5f, 0.5f); GameEntity* space = new Space(mI, Position3D(0, 0, 0), "../SpaceGame/resources/planetki new/space.jpg"); space->Draw(); //GameEntity* planet1 = new Planet(mI, Position3D(0, 0, 1), "../SpaceGame/resources/a4203_planetes_g.png"); //planet1->DrawRegion(new Region(100, 220, 140, 150)); //GameEntity* planet2 = new Planet(mI, Position3D(300, 0, 1), "../SpaceGame/resources/a4203_planetes_g.png"); //planet1->setPosition(Position3D(300, 0, 1)); //planet1->DrawRegion(new Region(100, 220, 140, 150)); //GameEntity* ship = new Ship(mI, Position3D(300, 200, 1), "../SpaceGame/resources/rocket.png"); //ship->Draw(); float mAngle = 0; float mPos = 400; int mSpeed = 200; float mDelta; char mText[2048]; mText[0] = 0; while (!mI->_input->onKeyPress(IND_ESCAPE) && !mI->_input->quit()) { strcat(mText, "Use left and right arrow keys for moving the ships\n"); strcat(mText, "Press CTRL + X or ESC key to quit"); mTextSmallWhite->setText(mText); mDelta = mI->_render->getFrameTime() / 1000.0f; // Move entities when pressing right if (mI->_input->isKeyPressed(IND_KEYRIGHT)){ mPos += mSpeed * mDelta; } // Move entities when pressing left if (mI->_input->isKeyPressed(IND_KEYLEFT)){ mPos -= mSpeed * mDelta; } // If CTRL + X pressed then exit if (mI->_input->isKeyPressed(IND_LCTRL) && mI->_input->isKeyPressed(IND_X)){ mI->_render->endScene(); mI->end(); exit(0); } mAngle += (mSpeed / 4) * mDelta; mBeetleship->setPosition((float)mPos, 140, 1); mBeetleship->setAngleXYZ(0, 0, (float)mPos); mI->_input->update(); mI->_render->beginScene(); mI->_entity2dManager->renderEntities2d(); mI->_render->endScene(); } mI->end(); return 0; }