void CManipulator::DestroyChain(cprimitive* current) { if(current->pFirstChild != NULL) DestroyChain(current->pFirstChild); if(current->pSiblings != NULL) DestroyChain(current->pSiblings); //destroy numOfChains--; cube.erase(cube.begin()+current->id); current->Release(); free(current); RenewIndices(); }
void PlayerEnt::OnMouseUp(const baka::mouse_events::ButtonAction& action) { //only player 0 gets keyboard control if (m_nPlayerId != 0) return; if (action.m_button == sf::Mouse::Right) { if (DestroyChain()) return; baka::render::RenderPlugin* pRenderPlug = static_cast<baka::render::RenderPlugin*>(baka::IPlugin::FindPlugin(baka::render::RenderPlugin::Type)); baka::physics::PhysicsPlugin* pPhysicsPlug = static_cast<baka::physics::PhysicsPlugin*>(baka::IPlugin::FindPlugin(baka::physics::PhysicsPlugin::Type)); sf::Vector2f worldCoords = pRenderPlug->GetRenderWindow()->mapPixelToCoords(action.m_pixel, pPhysicsPlug->GetView()); worldCoords.y *= -1; OnRopeEvent(worldCoords); } }
//--------------------------------------------------------------------------------------------- // Name: // Desc: removes selected chain and all of its' ancestors //--------------------------------------------------------------------------------------------- HRESULT CManipulator::RemoveChain() { //vars cprimitive* ptr; cprimitive* parent; // LOOKING FOR A PARENT //------------------------ //check hierarchy parent = GetParent(cube[selectedChain], cube[0]); if(parent == NULL) { MessageBox(0, L"Can't find parent", L"Error!", 0); return E_FAIL; }; //cutting links if(parent->pFirstChild == cube[selectedChain]) { parent->pFirstChild = cube[selectedChain]->pSiblings; //link destroyed } else if(parent->pFirstChild->pSiblings == cube[selectedChain]) { parent->pFirstChild->pSiblings = cube[selectedChain]->pSiblings; //link destroyed } else { for(ptr=parent->pFirstChild->pSiblings; ptr->pSiblings != NULL; ptr = ptr->pSiblings) { if(ptr->pSiblings == cube[selectedChain]) { ptr->pSiblings = cube[selectedChain]; //link destroyed } } } //deleting DestroyChain(cube[selectedChain]); //deselect selectedChain = -1; return S_OK; }
VIRTUAL void PlayerEnt::Exit() { baka::render::RenderPlugin* renderer = static_cast<baka::render::RenderPlugin*>(baka::IPlugin::FindPlugin(baka::render::RenderPlugin::Type)); if (renderer != null) { renderer->RemDrawable(m_pDrawable); m_pDrawable->state->clearTracks(); delete m_pDrawable; } delete m_pAtlas; delete m_pSkelData; delete m_pStateData; m_pDrawable = null; m_pAtlas = null; m_pSkelData = null; m_pStateData = null; m_vGroundContacts.clear(); DestroyChain(); }
void PlayerEnt::Shoot() { if (DestroyChain()) return; m_sound.setBuffer(m_soundBufferShoot); m_sound.play(); const b2Vec2 dir = CalcShootDirection(); b2Vec2 pos = m_pBody->GetPosition(); b2Vec2 endPoint = pos + dir * m_tValue.m_fTetherLength; if (m_pTongueTip != null) { m_pTongueTip->GetWorld()->DestroyBody(m_pTongueTip); m_pTongueTip = null; } //body definition b2BodyDef def; def.type = b2_dynamicBody; def.linearVelocity = dir * m_tValue.m_fTongueTipSpeed; def.bullet = true; //shape definition b2CircleShape circleShape; circleShape.m_radius = m_tValue.m_fTongueTipRadius; //fixture definition b2FixtureDef myFixtureDef; myFixtureDef.shape = &circleShape; myFixtureDef.density = m_tValue.m_fTongueTipDensity; //create dynamic body b2Vec2 startPoint = pos + b2Vec2(aimX, aimY) + b2Vec2(dir.x * aimLenX, dir.y * aimLenY); if (dir.x < 0) startPoint = pos + b2Vec2(-aimX, aimY) + b2Vec2(dir.x * aimLenX, dir.y * aimLenY); def.position.Set(startPoint.x, startPoint.y); m_pTongueTip = m_pBody->GetWorld()->CreateBody(&def); m_pTongueTip->CreateFixture(&myFixtureDef); /*bool hit = OnRopeEvent(sf::Vector2f(endPoint.x, endPoint.y)); if (!hit) { float angle = atan2(dir.y, dir.x); b2Vec2 tmp; tmp.x = cos(angle + m_tValue.m_fTetherAngle * DEG_RAD); tmp.y = sin(angle + m_tValue.m_fTetherAngle * DEG_RAD); tmp.Normalize(); endPoint = pos + tmp * m_tValue.m_fTetherLength; hit = OnRopeEvent(sf::Vector2f(endPoint.x, endPoint.y)); } if (!hit) { float angle = atan2(dir.y, dir.x); b2Vec2 tmp; tmp.x = cos(angle - m_tValue.m_fTetherAngle * DEG_RAD); tmp.y = sin(angle - m_tValue.m_fTetherAngle * DEG_RAD); tmp.Normalize(); endPoint = pos + tmp * m_tValue.m_fTetherLength; hit = OnRopeEvent(sf::Vector2f(endPoint.x, endPoint.y)); }*/ }
VIRTUAL void PlayerEnt::Update(const sf::Time& dt) { if (m_bRespawn) { m_bRespawn = false; DestroyChain(); m_pBody->SetTransform(m_spawnPos, 0); } for (int i = 0; i < EButton::EB_COUNT; ++i) { m_vButtons[i].Flush(dt.asSeconds()); } if (m_vButtons[EB_SHOOT].Pull()) { Shoot(); } if (m_vButtons[EB_DASH].Pull()) { Dash(); } if (m_fFlipTime > 0) { float desiredAngle = 0; m_fFlipTime -= dt.asSeconds(); if (m_fFlipTime <= 0) { float desiredAngle = 0; m_pBody->SetTransform(m_pBody->GetPosition(), 0); m_pBody->SetFixedRotation(true); } else { desiredAngle = 180 * DEG_RAD;// Lerp(0, 360, 1.0f - (m_fFlipTime / m_tValue.m_fFlipTime)); float bodyAngle = m_pBody->GetAngle(); float nextAngle = bodyAngle + m_pBody->GetAngularVelocity() / 60.0; float totalRotation = desiredAngle - nextAngle; while (totalRotation < -180 * DEG_RAD) totalRotation += 360 * DEG_RAD; while (totalRotation > 180 * DEG_RAD) totalRotation -= 360 * DEG_RAD; float desiredAngularVelocity = totalRotation * 60; float change = 10 * DEG_RAD; //allow 1 degree rotation per time step desiredAngularVelocity = Min(change, Max(-change, desiredAngularVelocity)); float impulse = m_pBody->GetInertia() * desiredAngularVelocity; m_pBody->SetFixedRotation(false); m_pBody->ApplyAngularImpulse(impulse, true); } } if (m_vButtons[EB_JUMP].Push() && m_bGrounded) { m_fJumpTime = m_tValue.m_fJumpTime; //m_pBody->ApplyLinearImpulse(b2Vec2(0, m_tValue.m_fJumpImpulse), m_pBody->GetLocalCenter(), true); m_sound.setBuffer(m_soundBufferJump); m_sound.play(); m_pDrawable->state->setAnimationByName(0, "jump", false); float jIpulse = m_pBody->GetMass() * m_tValue.m_fInitialJumpSpeed; m_pBody->ApplyLinearImpulse(b2Vec2(0, jIpulse), m_pBody->GetLocalCenter(), true); b2Vec2 jumpImpulse(0, -jIpulse * 0.5f); for (int i = 0; i < m_vGroundContacts.size(); ++i) { b2Fixture* pFixA = m_vGroundContacts[i]->GetFixtureA(); b2Fixture* pFixB = m_vGroundContacts[i]->GetFixtureB(); if (pFixA == m_pGroundSensor) { pFixB->GetBody()->ApplyLinearImpulse(jumpImpulse, pFixB->GetBody()->GetWorldCenter(), true); } else { pFixA->GetBody()->ApplyLinearImpulse(jumpImpulse, pFixA->GetBody()->GetWorldCenter(), true); } } } else if (m_vButtons[EB_JUMP].Pull()) { m_fJumpTime = -1.0f; } if (m_vButtons[EB_JUMP].Held() && m_fJumpTime > 0.0f) { m_fJumpTime -= dt.asSeconds(); float jIpulse = m_pBody->GetMass() * m_tValue.m_fTerminalJumpSpeed; m_pBody->ApplyLinearImpulse(b2Vec2(0, jIpulse), m_pBody->GetLocalCenter(), true); } m_pDrawable->update(dt.asSeconds()); m_pDrawable->setPosition(m_pBody->GetPosition().x, -m_pBody->GetPosition().y); // m_pDrawable->setRotation(m_pBody->GetAngle() * -RAD_DEG); b2Vec2 vel = m_pBody->GetLinearVelocity(); float desiredVel = 0; bool hasInput = false; if (m_vButtons[EB_RIGHT].Held()) { hasInput = true; if (m_bGrounded) desiredVel = b2Min(vel.x + m_tValue.m_fGroundAcceleration, m_tValue.m_fMaxSpeed); else desiredVel = b2Min(vel.x + m_tValue.m_fAirAcceleration, m_tValue.m_fMaxSpeed); } if (m_vButtons[EB_LEFT].Held()) { hasInput = true; if (m_bGrounded) desiredVel = b2Max(vel.x - m_tValue.m_fGroundAcceleration, -m_tValue.m_fMaxSpeed); else desiredVel = b2Max(vel.x - m_tValue.m_fAirAcceleration, -m_tValue.m_fMaxSpeed); } bool skip = false; if (!hasInput) //apply deceleration { if (m_bGrounded) desiredVel = vel.x * m_tValue.m_fGroundDeceleration; else desiredVel = vel.x * m_tValue.m_fAirDeceleration; } else if((m_vButtons[EB_LEFT].Held() && vel.x < -m_tValue.m_fMaxSpeed) || (m_vButtons[EB_RIGHT].Held() && vel.x > m_tValue.m_fMaxSpeed)) { skip = true; } if (!skip) { float velChange = desiredVel - vel.x; float impulse = m_pBody->GetMass() * velChange; //disregard time factor m_pBody->ApplyLinearImpulse(b2Vec2(impulse, 0), m_pBody->GetWorldCenter(), true); } if (m_vButtons[EB_RIGHT].Held()) m_pDrawable->skeleton->flipX = false; else if (m_vButtons[EB_LEFT].Held()) m_pDrawable->skeleton->flipX = true; //do animation state changes if (m_bGrounded) { if ((m_vButtons[EB_RIGHT].Push() && !m_vButtons[EB_LEFT].Held()) || (m_vButtons[EB_LEFT].Push() && !m_vButtons[EB_RIGHT].Held())) { //m_pStateData->setMixByName("idle", "run", 0.25); m_pDrawable->state->setAnimationByName(0, "run", true); } if ((m_vButtons[EB_RIGHT].Pull() && !m_vButtons[EB_LEFT].Held()) || (m_vButtons[EB_LEFT].Pull() && !m_vButtons[EB_RIGHT].Held())) { //m_pStateData->setMixByName("run", "idle", 0.25); m_pDrawable->state->setAnimationByName(0, "idle", true); } } if (m_pTongueContactUse) { //b2Vec2 vel = m_pTongueTip->GetLinearVelocity(); //b2Vec2 pnt = m_pTongueTip->GetPosition();// +vel.Normalize() * m_tValue.m_fTongueTipRadius; b2Vec2 pnt; b2WorldManifold mani; m_pTongueContactUse->GetWorldManifold(&mani); b2Body* otherBody; if (m_pTongueContactUse->GetFixtureA()->GetBody() == m_pTongueTip) { otherBody = m_pTongueContactUse->GetFixtureB()->GetBody(); pnt = otherBody->GetWorldPoint(m_TongueLocalCoords);// mani.points[0]; } else { otherBody = m_pTongueContactUse->GetFixtureA()->GetBody(); pnt = otherBody->GetWorldPoint(m_TongueLocalCoords);// mani.points[0]; } MakeRopeJoint(otherBody, pnt); m_pBody->GetWorld()->DestroyBody(m_pTongueTip); m_pTongueTip = null; m_pTongueContactUse = null; } if (m_pTongueTip && (m_pTongueTip->GetPosition() - m_pBody->GetPosition()).Length() > m_tValue.m_fTetherLength) { m_pTongueTip->GetWorld()->DestroyBody(m_pTongueTip); m_pTongueTip = null; } b2Vec2 dir = CalcShootDirection(); b2Vec2 pos = m_pBody->GetPosition() + b2Vec2(aimX, aimY) + b2Vec2(dir.x * aimLenX, dir.y * aimLenY); if (dir.x < 0) pos = m_pBody->GetPosition() + b2Vec2(-aimX, aimY) + b2Vec2(dir.x * aimLenX, dir.y * aimLenY); if (m_pTongueTip) { pos = m_pTongueTip->GetPosition(); pos.x -= m_pCircleDraw->getRadius(); pos.y += m_pCircleDraw->getRadius(); } pos.y *= -1; m_pCircleDraw->setPosition(pos.x, pos.y); }