/** * @brief CollisionSystem::GetBins gets all bins in which an entity belongs * @param e the entity * @param bins the bins the entity belongs to, if return value is true * @return true if entity is completely in defined game world, false otherwise */ bool CollisionSystem::GetBins(Entity *e, std::set<unsigned int>& bins) { bins.clear(); ColliderComponent* cc = e->GetComponent<ColliderComponent>("Collider"); PositionComponent* pc = e->GetComponent<PositionComponent>("Position"); sf::Vector2f pos = pc->GetPosition() + cc->GetOffset(); sf::Vector2f dim = cc->GetDimensions(); unsigned int bin[4]; bin[0] = (pos.y / _BinSize.y) * _NumBinsX + (pos.x / _BinSize.x); bin[1] = ((pos.y + dim.y) / _BinSize.y) * _NumBinsX + (pos.x / _BinSize.x); bin[2] = (pos.y / _BinSize.y) * _NumBinsX + ((pos.x + dim.x) / _BinSize.x); bin[3] = ((pos.y + dim.y) / _BinSize.y) * _NumBinsX + ((pos.x + dim.x) / _BinSize.x); for(unsigned int i = 0; i < 4; i++) { if(bin[i] >= _NumBins) { return false; } bins.insert(bin[i]); } return true; }
bool CreditScene::init() { Graphics::getInstance()->fadeIn(0.6f); _isInit = true; auto playerInput = Factory::get()->loadEntityFromFile("playerInput"); playerInput->init(); Scene::setPlayerInput(playerInput); _credits = Factory::get()->loadEntityFromFile("credits"); _credits->init(); auto PosCmp = _credits->GET_CMP(PositionComponent); PosCmp->setPosition(Graphics::getInstance()->getScreenSize().cast<float>() * 0.5); auto SpCmp = _credits->GET_CMP(SpriteCmp); // 1.5 1.5 looks good SpCmp->rescale(2.0f, 2.0f); Eigen::Vector2f pos = Graphics::getInstance()->getScreenSize().cast<float>() * 0.75; pos.x() = Graphics::getInstance()->getScreenSize().cast<float>().x() * 0.88; Entity* returnButton = Factory::get()->loadEntityFromFile("buttonBack"); returnButton->init(); std::function<void()> optionFunc = [this](){ free(); Director::get()->pushScene(new MenuScene()); }; returnButton->GET_CMP(ButtonCmp)->setCallback(optionFunc); PositionComponent* posCmp = returnButton->GET_CMP(PositionComponent); pos.y() += 200.0f; posCmp->setPosition(pos); _buttons.push_back(returnButton); _cursor = _buttons.begin(); _brighter(); return _isInit; }
Bomb::Image::Image(int posX, int posY, int width, int height, std::string name, std::string path) { this->ctor(Image_o); PositionComponent *pos = new PositionComponent(); pos->AssignEntity(this); this->AddComponent(pos); /* PhysicComponent *physic = new PhysicComponent(UNDESTRUCTIBLE_BOX_HITBOX); physic->AssignEntity(this); this->AddComponent(physic); */ this->posX = posX; this->posY = posY; this->width = width; this->height = height; this->path = path; this->name = name; MeshComponent *graphic = new MeshComponent(GEOMETRY, new std::string(this->name), new std::string(this->path), ImageMeshInit, ImageMeshDraw, G_Renderer); graphic->AssignEntity(this); this->AddComponent(graphic); }
void ProjectileSystem::Update(float _dt) { MouseInputComponent* mouse; PositionComponent* position; EntityMap::iterator it; for (it = m_entityMap.begin(); it != m_entityMap.end(); ++it) { Entity* e = it->second; if (e->GetState() != Entity::ALIVE) continue; mouse = e->GetComponent<MouseInputComponent>(); if (mouse->m_controls.LeftButton == InputState::Pressed) { position = it->second->GetComponent<PositionComponent>(); Entity* e = m_world->CreateEntity(); EntityFactory::GetInstance()->CreateEntity(e, EntityFactory::BALL); e->GetComponent<VelocityComponent>()->m_velocity = VECTOR3(0, 40, 0); e->GetComponent<PositionComponent>()->SetPosition(VECTOR3(position->GetPosition().x, position->GetPosition().y + 2.0f, 0)); m_world->AddEntity(e); } } }
/** * @brief GetEntityPosition gets the position of a specified entity * @param L the calling lua thread * @return the entity position if available, an error string otherwise. */ static int GetEntityPosition(lua_State* L) { if(lua_gettop(L) != 1) { lua_pushstring(L, "Error: incorrect number of parameters"); return 1; } LuaAccessor la; unsigned int id = (unsigned int)lua_tonumber(L, 1); Entity* e = la.GetEntity(id); if(e) { PositionComponent* p = e->GetComponent<PositionComponent>("Position"); if(p) { lua_pushnumber(L, p->GetPosition().x); lua_pushnumber(L, p->GetPosition().y); return 2; } else { lua_pushstring(L, "No position component"); return 1; } } lua_pushstring(L, "Entity does not exist"); return 1; }
Bomb::DefaultBomb::DefaultBomb() { this->ctor(DefaultBomb_o); PositionComponent *position = new PositionComponent(); position->AssignEntity(this); this->AddComponent(position); this->_isDestroy = true; }
void Bomb::DropBonusComponent::Update() { PositionComponent *pos = static_cast<PositionComponent *>(this->GetParent()->GetComponent(Position_c)); LifeComponent *life = static_cast<LifeComponent *>(this->GetParent()->GetComponent(Life_c)); enum GameObjectTag bonusTab[3]; bonusTab[0] = BonusSpeed_o; bonusTab[1] = BonusAddBomb_o; bonusTab[2] = BonusSizeBomb_o; ListObj *list_world = G_ObjectsContainer->GetObjByTag(World_o); if (list_world && life && life->GetLife() <= 0 && !this->_dropped) { IEntity *world = list_world->front(); if (world) { MapComponent *map = static_cast<MapComponent *>(world->GetComponent(Map_c)); if (pos && map) { double posX = pos->GetPosition(PositionComponent::X); double posY = pos->GetPosition(PositionComponent::Y); if (rand() % 10 < 4) { IEntity *bonus = G_EntityFactory->Create(bonusTab[rand() % 3]); if (bonus) { pos = static_cast<PositionComponent *>(bonus->GetComponent(Position_c)); G_ObjectsContainer->AddObj(bonus); G_EvDispatcher->SendEvent(new Event::GraphicEvent(bonus, Graphic::MESHADD), Event::COMMON); pos->SetPosition(posX, posY); map->AddEntity(bonus, posX, posY); bonus->Start(); this->_dropped = true; } } } } } }
void TentacleLogicCmp::checkPlayerInRange() { int nbr = 0; TentacleGearCmp* tmp = _start; PositionComponent* player = _boss->getPlayer()->GET_CMP(PositionComponent); PositionComponent* me = this->getEntity()->GET_CMP(PositionComponent); int xP = player->getX(); int yP = player->getY(); int xMe = me->getX(); int yMe = me->getY(); while (tmp != NULL) { tmp = tmp->getNext(); nbr++; } if (pow((pow((xP - xMe), 2) + pow((yP - yMe), 2)), 0.5) <= 64 * nbr) setInRange(true); else { _inRange = false; _hasBeenReset = false; } getBoss()->getPlayer(); }
Bomb::DestructibleBox::DestructibleBox() { this->ctor(DestructibleBox_o); PositionComponent *pos = new PositionComponent(); pos->AssignEntity(this); this->AddComponent(pos); LifeComponent *life = new LifeComponent(1); life->AssignEntity(this); this->AddComponent(life); PhysicComponent *physic = new PhysicComponent(DESTRUCTIBLE_BOX_HITBOX); physic->AssignEntity(this); this->AddComponent(physic); DropBonusComponent *dropBonus = new DropBonusComponent(); dropBonus->AssignEntity(this); this->AddComponent(dropBonus); MeshComponent *graphic = new MeshComponent(GEOMETRY, new std::string("box2"), new std::string(MODELPATH"box2.tga"), DestructibleBoxMeshInit, DestructibleBoxMeshDraw, G_Renderer); graphic->AssignEntity(this); this->AddComponent(graphic); this->_isDestroy = true; /*AudioComponent *audio = new AudioComponent(); audio->AssignEntity(this); this->AddComponent(audio);*/ }
bool TentacleLogicCmp::isGoodSide(int were) { PositionComponent* player = _boss->getPlayer()->GET_CMP(PositionComponent); PositionComponent* me = this->getEntity()->GET_CMP(PositionComponent); int xP = player->getX(); int yP = player->getY(); int xMe = me->getX(); int yMe = me->getY(); if (were == 0) { //left if (xP < xMe) return true; } else if (were == 1) { //right if (xP > xMe) return true; } else if (were == 2) { //top if (yP > yMe) return true; } else if (were == 3) { //bot if (yP < yMe) return true; } return false; }
string PhysicsController::run(string myState, float time_since_last_tick) { string game_state_request = " "; for(vector<Entity*>::size_type i = 0; i != controlled_entities.size(); i++) { if(controlled_entities[i]->getGameState()==myState) { if(controlled_entities[i]->getActive()) { //PULL THE MOVING OBJECTS INFORMATION PhysicsComponent* physics = (PhysicsComponent*)controlled_entities[i]->getComponent("Physics"); PositionComponent* position = (PositionComponent*)controlled_entities[i]->getComponent("Position"); //PULLING ALL THE MASS AND FORCE INFORMATION float total_mass = 0 + physics->getMass(); float total_translation [2] = {0,0}; float moment_of_inertia = 0 + physics->getMass(); float total_torque = 0; if( controlled_entities[i]->checkIfHasComponent("Collection")) { CollectionComponent* collection = (CollectionComponent*) controlled_entities[i]->getComponent("Collection"); vector<Entity*> attached_entities = collection->getAttachedEntities(); //Cycling through attached entities for(vector<Entity*>::size_type p = 0; p != attached_entities.size(); p++) { Entity* attached_entity = attached_entities[p]; TetheredComponent* tethered = (TetheredComponent*) attached_entity->getComponent("Tethered"); PhysicsComponent* attached_physics = (PhysicsComponent*) attached_entity->getComponent("Physics"); PositionComponent* attached_position = (PositionComponent*) attached_entity->getComponent("Position"); RocketComponent* rocket = (RocketComponent*) attached_entity->getComponent("Rocket"); float radian_from_center_mass = tethered->getDirectionRadian(); float radian_to_center_mass = radian_from_center_mass + 3.1415926; float t = attached_position->getAngle() - radian_to_center_mass ; tethered->setT(t); //Translational vector<float> attached_vector_forces = attached_physics->getNetTranslationalForce(); total_translation[0] += attached_vector_forces[0]; total_translation[1] += attached_vector_forces[1]; total_mass+= attached_physics->getMass(); //Angular float attached_net_angular_force = attached_physics->getNetAngularForce(); total_torque += attached_net_angular_force; float distance = tethered->getDistance(); moment_of_inertia += attached_physics->getMass()*(distance*distance); } } //Apparently on forces that have a distance can spin it //So after we've pulled all the angular forces we can find the angular acceleration //and then you use torque = I*a physics->setAngularAcceleration(total_torque/moment_of_inertia); //PLANAR MOVEMENT //We already have the tethered objects' forces so now we need the forces affectin the main object itself vector<float> vector_forces = physics->getNetTranslationalForce(); total_translation[0] += vector_forces[0]; total_translation[1] += vector_forces[1]; //Calculating the net acceleration net forces and net mass physics->setAccelerationX(total_translation[0]/total_mass); physics->setAccelerationY(total_translation[1]/total_mass); //Determine its new velocity physics->setVelocityX(physics->getVelocity()[0]+physics->getAcceleration()[0]*time_since_last_tick); physics->setVelocityY(physics->getVelocity()[1]+physics->getAcceleration()[1]*time_since_last_tick); //Determine its new position if its not tethered to something if( !controlled_entities[i]->checkIfHasComponent("Tethered")) { position->setX(position->getPosition()[0]+physics->getVelocity()[0]*time_since_last_tick); position->setY(position->getPosition()[1]+physics->getVelocity()[1]*time_since_last_tick); } //ANGULAR MOVEMENT // Both tethered and collected entities get this because a tethered can be rotating on its own // on top of being rotated by the collectors movement. // On it's own, a tethered object cannot have an angular acceleration, it must be machine assisted. physics->setAngularVelocity(physics->getAngularVelocity()+physics->getAngularAcceleration()*time_since_last_tick); position->setAngle(position->getAngle()+physics->getAngularVelocity()*time_since_last_tick); //To make sure the tethered objects get moved we make sure the collector handles them we he is passed into this controller if( controlled_entities[i]->checkIfHasComponent("Collection")) { CollectionComponent* collection = (CollectionComponent*) controlled_entities[i]->getComponent("Collection"); vector<Entity*> attached_entities = collection->getAttachedEntities(); //Go throuhg all the attached entities and update their tethered position wiht the new positon of the collector for(vector<Entity*>::size_type p = 0; p != attached_entities.size(); p++) { Entity* attached_entity = attached_entities[p]; TetheredComponent* tethered = (TetheredComponent*) attached_entity->getComponent("Tethered"); PhysicsComponent* attached_physics = (PhysicsComponent*) attached_entity->getComponent("Physics"); PositionComponent* attached_position = (PositionComponent*) attached_entity->getComponent("Position"); ////Determine its new position //As the object spins so will all the tethered objects tethered->setDirectionRadian( tethered->getDirectionRadian() + physics->getAngularVelocity()*time_since_last_tick); float radian_from_center_mass = tethered->getDirectionRadian(); float radian_to_center_mass = radian_from_center_mass + 3.1415926; attached_position->setAngle(radian_to_center_mass + tethered->getT()); //Need to update the angle of the rocket as the main ship spins //Set the tethered object to the new position in the new direction attached_position->setX( position->getPosition()[0] + cos(tethered->getDirectionRadian())*tethered->getDistance()); attached_position->setY( position->getPosition()[1] + sin(tethered->getDirectionRadian())*tethered->getDistance()); } } position->calculateBucketPosition(BUCKET_WIDTH); } } } return game_state_request; }
string RocketController::run(string myState) { string game_state_request; for(vector<Entity*>::size_type i = 0; i != controlled_entities.size(); i++) { Entity* ent = controlled_entities[i]; if(ent->getGameState() == myState) { if(ent->getActive()) { //SUBTRACT FUEL SpaceshipComponent* spaceship = (SpaceshipComponent*) ent->getComponent("Spaceship"); float current_fuel = spaceship->getCurrentFuel(); CollectionComponent* collection = (CollectionComponent*) ent->getComponent("Collection"); vector<Entity*> attached_entities = collection->getAttachedEntities(); //Cycling through attached entities for(vector<Entity*>::size_type p = 0; p != attached_entities.size(); p++) { Entity* attached_entity = attached_entities[p]; attached_entity->setActive(true); if(attached_entity->checkIfHasComponent("Rocket")) { RocketComponent* rocket = (RocketComponent*) attached_entity->getComponent("Rocket"); TetheredComponent* tethered = (TetheredComponent*) attached_entity->getComponent("Tethered"); PositionComponent* position = (PositionComponent*) attached_entity->getComponent("Position"); PhysicsComponent* physics = (PhysicsComponent*) attached_entity->getComponent("Physics"); physics->clearForces(); if(rocket->getOnOrOff()) { if(spaceship->getCurrentFuel() - rocket->getFuelConsumption() >= 0) { //The rocket is on float force_exerted = rocket->getForceExerted(); float angle = position->getAngle(); float x = rocket->getForceExerted()*cos(angle); float y = rocket->getForceExerted()*sin(angle); physics->addTranslationalForce(x,y); //Angular Forces float distance = tethered->getDistance(); float angular_force = distance * rocket->getForceExerted() * -sin(tethered->getT()); physics->addAngularForce(angular_force); //Burning fuel spaceship->subtractCurrentFuel(rocket->getFuelConsumption()); } } } } } else { CollectionComponent* collection = (CollectionComponent*) ent->getComponent("Collection"); vector<Entity*> attached_entities = collection->getAttachedEntities(); //Cycling through attached entities for(vector<Entity*>::size_type p = 0; p != attached_entities.size(); p++) { attached_entities[p]->setActive(false); } } } } return game_state_request; }