float Mage::hurt(BasicCollider* collider, bool dirKnockMode) { if (_isalive == true) { auto damage = collider->getDamage(); //calculate the real damage bool critical = false; auto knock = collider->getKnock(); if (CCRANDOM_0_1() < collider->getCriticalChance()) { damage *= 1.5; critical = true; knock *= 2; } damage = damage + damage * CCRANDOM_MINUS1_1() * 0.15 - _defense; damage = floor(damage); if (damage <= 0) damage = 1; _hp -= damage; if (_hp > 0) { if (critical == true) { knockMode(collider, dirKnockMode); hurtSoundEffects(); } else hurtSoundEffects(); } else { _hp = 0; _isalive = false; dyingMode(getPosTable(collider), knock); } auto blood = _hpCounter->showBloodLossNum(damage, this, critical); blood->setCameraMask(2); if (_name == "Rat") blood->setPositionZ(Director::getInstance()->getVisibleSize().height * 0.25); addEffect(blood); //Transfer data to UILayer struct MESSAGE_BLOOD_MINUS bloodMinus = { _name, _maxhp, _hp, _bloodBar, _bloodBarClone, _avatar }; MessageDispatchCenter::getInstance()->dispatchMessage(BLOOD_MINUS, this); struct MESSAGE_ANGRY_CHANGE angryChange = { _name, _angry,_angryMax }; MessageDispatchCenter::getInstance()->dispatchMessage(ANGRY_CHANGE, this); _angry += damage; return damage; } return 0; }
void ModelDataModel::setPosition(const Vector3f position) { setPositionX(position[0]); setPositionY(position[1]); setPositionZ(position[2]); }
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } auto visibleSize = Director::getInstance()->getVisibleSize(); auto origin = Director::getInstance()->getVisibleOrigin(); camera = Camera::createPerspective(30, (float)visibleSize.width / visibleSize.height, 1.0, 1000); camera->setBackgroundBrush(CameraBackgroundBrush::createColorBrush(Color4F(0.0f, 1.0f, 1.0f, 0.5f), 1.0f)); camera->setPosition3D(Vec3(0.0f, 50.0f, 100.0f)); camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0, 1.0, 0.0)); camera->setCameraFlag(CameraFlag::USER2); this->addChild(camera); /** //Create Plane Physics3DRigidBodyDes rbd_plan; rbd_plan.mass = 0.0f; rbd_plan.shape = Physics3DShape::createBox(Vec3(500, 5.0f, 500)); auto physics_rbd_plan = Physics3DRigidBody::create(&rbd_plan); physics_rbd_plan->setFriction(20); auto component_plan = Physics3DComponent::create(physics_rbd_plan); _plan = Sprite3D::create("box.c3t"); _plan->setTexture("plane.png"); _plan->setScale(30.0f); _plan->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0)); _plan->setScaleX(500); _plan->setScaleY(5); _plan->setScaleZ(500); _plan->setPositionZ(0); _plan->setPositionY(0); _plan->setPositionX(0); _plan->setGlobalZOrder(-1); _plan->addComponent(component_plan); component_plan->syncNodeToPhysics(); component_plan->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::NONE); _plan->setCameraMask((unsigned short)CameraFlag::USER2); this->addChild(_plan, 1); **/ Terrain::DetailMap r("dirt.jpg"); Terrain::TerrainData data("heightmap16.jpg", "alphamap.png", r, r, r, r, Size(32, 32), 5.0f, 1.0f); auto _terrain = Terrain::create(data, Terrain::CrackFixedType::SKIRT); _terrain->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0)); _terrain->setPositionZ(0); _terrain->setPositionY(0); _terrain->setPositionX(0); _terrain->setLODDistance(64, 128, 192); _terrain->setDrawWire(false); _terrain->setMaxDetailMapAmount(4); _terrain->setSkirtHeightRatio(2); _terrain->setCameraMask((unsigned short)CameraFlag::USER2); //create terrain std::vector<float> heidata = _terrain->getHeightData(); auto size = _terrain->getTerrainSize(); Physics3DColliderDes colliderDes; colliderDes.shape = Physics3DShape::createHeightfield(size.width, size.height, &heidata[0], 1.0f, _terrain->getMinHeight(), _terrain->getMaxHeight(), true, false, true); auto collider = Physics3DCollider::create(&colliderDes); auto component = Physics3DComponent::create(collider); _terrain->addComponent(component); component->syncNodeToPhysics(); component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::NONE); this->addChild(_terrain); //////////////////////////////////////////////// car Physics3DRigidBodyDes rbd_cabine; rbd_cabine.disableSleep = true; rbd_cabine.mass = 500.0f; rbd_cabine.shape = Physics3DShape::createBox(Vec3(4, 2.0f, 8)); physics_rbd_cabine = Physics3DRigidBody::create(&rbd_cabine); auto cabine_component = Physics3DComponent::create(physics_rbd_cabine); car_cabine = Sprite3D::create("T-90.c3t"); car_cabine->setTexture("Main Body 2.png"); car_cabine->setScale(3); //car_cabine->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0)); car_cabine->setPositionX(0); car_cabine->setPositionY(10); car_cabine->setPositionZ(0); car_cabine->setGlobalZOrder(-1); car_cabine->addComponent(cabine_component); cabine_component->syncNodeToPhysics(); cabine_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); car_cabine->setCameraMask((unsigned short)CameraFlag::USER2); this->addChild(car_cabine, 1); Physics3DRigidBodyDes rbd_wheel1; rbd_wheel1.disableSleep = true; rbd_wheel1.mass = 15.0f; //rbd_wheel1.shape = Physics3DShape::createCylinder(3, 1); rbd_wheel1.shape = Physics3DShape::createSphere(1.5); auto physics_rbd_wheel1 = Physics3DRigidBody::create(&rbd_wheel1); auto wheel1_component = Physics3DComponent::create(physics_rbd_wheel1); wheel1 = Sprite3D::create(); //wheel1->setTexture("Gun.png"); //wheel1->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0)); wheel1->setPositionX(0); wheel1->setPositionY(10); wheel1->setPositionZ(0); wheel1->setGlobalZOrder(-1); wheel1->addComponent(wheel1_component); wheel1_component->syncNodeToPhysics(); wheel1_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); wheel1->setCameraMask((unsigned short)CameraFlag::USER2); this->addChild(wheel1, 1); Physics3DRigidBodyDes rbd_wheel2; rbd_wheel2.disableSleep = true; rbd_wheel2.mass = 15.0f; //rbd_wheel2.shape = Physics3DShape::createCylinder(3, 1); rbd_wheel2.shape = Physics3DShape::createSphere(1.5); auto physics_rbd_wheel2 = Physics3DRigidBody::create(&rbd_wheel2); auto wheel2_component = Physics3DComponent::create(physics_rbd_wheel2); wheel2 = Sprite3D::create(); //wheel2->setTexture("Gun.png"); //wheel2->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0)); wheel2->setPositionX(0); wheel2->setPositionY(10); wheel2->setPositionZ(0); wheel2->setGlobalZOrder(-1); wheel2->addComponent(wheel2_component); wheel2_component->syncNodeToPhysics(); wheel2_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); wheel2->setCameraMask((unsigned short)CameraFlag::USER2); this->addChild(wheel2, 1); Physics3DRigidBodyDes rbd_wheel3; rbd_wheel3.disableSleep = true; rbd_wheel3.mass = 15.0f; rbd_wheel3.shape = Physics3DShape::createSphere(1.5); //rbd_wheel3.shape = Physics3DShape::createCylinder(3, 1); physics_rbd_wheel3 = Physics3DRigidBody::create(&rbd_wheel3); auto wheel3_component = Physics3DComponent::create(physics_rbd_wheel3); wheel3 = Sprite3D::create(); //wheel3->setTexture("Gun.png"); //wheel3->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0)); wheel3->setPositionX(0); wheel3->setPositionY(10); wheel3->setPositionZ(0); wheel3->setGlobalZOrder(-1); wheel3->addComponent(wheel3_component); wheel3_component->syncNodeToPhysics(); wheel3_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); wheel3->setCameraMask((unsigned short)CameraFlag::USER2); this->addChild(wheel3, 1); Physics3DRigidBodyDes rbd_wheel4; rbd_wheel4.disableSleep = true; rbd_wheel4.mass = 15.0f; //rbd_wheel4.shape = Physics3DShape::createCylinder(3, 1); rbd_wheel4.shape = Physics3DShape::createSphere(1.5); physics_rbd_wheel4 = Physics3DRigidBody::create(&rbd_wheel4); auto wheel4_component = Physics3DComponent::create(physics_rbd_wheel4); wheel4 = Sprite3D::create(); //wheel3->setTexture("Gun.png"); //wheel4->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0)); wheel4->setPositionX(0); wheel4->setPositionY(10); wheel4->setPositionZ(0); wheel4->setGlobalZOrder(-1); wheel4->addComponent(wheel4_component); wheel4_component->syncNodeToPhysics(); wheel4_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); wheel4->setCameraMask((unsigned short)CameraFlag::USER2); this->addChild(wheel4, 1); Physics3DRigidBodyDes rbd_wheel_bar; rbd_wheel_bar.disableSleep = true; rbd_wheel_bar.mass = 400.0f; rbd_wheel_bar.shape = Physics3DShape::createBox(Vec3(2, 2.0f, 6)); physics_rbd_wheel_bar = Physics3DRigidBody::create(&rbd_wheel_bar); auto car_wheel_bar_component = Physics3DComponent::create(physics_rbd_wheel_bar); car_wheel_bar = Sprite3D::create(); //car_wheel_bar->setTexture("Gun.png"); car_wheel_bar->setScale(0.01); car_wheel_bar->setPositionX(0); car_wheel_bar->setPositionY(10); car_wheel_bar->setPositionZ(0); //car_wheel_bar->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0)); car_wheel_bar->setGlobalZOrder(-1); car_wheel_bar->addComponent(car_wheel_bar_component); car_wheel_bar_component->syncNodeToPhysics(); car_wheel_bar_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); car_wheel_bar->setCameraMask((unsigned short)CameraFlag::USER2); this->addChild(car_wheel_bar, 1); auto constraint = Physics3DHingeConstraint::create(physics_rbd_cabine, physics_rbd_wheel1, Vec3(0.0f, 0.0, 6.0f), Vec3(0.f, -3.0f, 0.f), Vec3(1.0f, 0.0, 0.0f), Vec3(0.f, 1.0f, 0.f)); psychics_scene->getPhysics3DWorld()->addPhysics3DConstraint(constraint); auto constraint2 = Physics3DHingeConstraint::create(physics_rbd_cabine, physics_rbd_wheel2, Vec3(0.0f, 0.0, 6.0f), Vec3(0.f, 3.0f, 0.f), Vec3(1.0f, 0.0, 0.0f), Vec3(0.f, 1.0f, 0.f)); psychics_scene->getPhysics3DWorld()->addPhysics3DConstraint(constraint2); constraint5 = Physics3DHingeConstraint::create(physics_rbd_cabine, physics_rbd_wheel_bar, Vec3(0.0f, 0.0, -6.0f), Vec3(0.f, 0.0f, 0.f), Vec3(0.0f, 1.0, 0.0f), Vec3(0.f, 1.0f, 0.f)); constraint5->setLimit(CC_DEGREES_TO_RADIANS(-0.5), CC_DEGREES_TO_RADIANS(0.5)); psychics_scene->getPhysics3DWorld()->addPhysics3DConstraint(constraint5); auto constraint3 = Physics3DHingeConstraint::create(physics_rbd_wheel_bar, physics_rbd_wheel3, Vec3(0.0f, 0.0, -3.0f), Vec3(0.f, -3.0f, 0.f), Vec3(1.0f, 0.0, 0.0f), Vec3(0.f, 1.0f, 0.f)); psychics_scene->getPhysics3DWorld()->addPhysics3DConstraint(constraint3); auto constraint4 = Physics3DHingeConstraint::create(physics_rbd_wheel_bar, physics_rbd_wheel4, Vec3(0.0f, 0.0, -3.0f), Vec3(0.f, 3.0f, 0.f), Vec3(1.0f, 0.0, 0.0f), Vec3(0.f, 1.0f, 0.f)); psychics_scene->getPhysics3DWorld()->addPhysics3DConstraint(constraint4); //add a point light auto light = PointLight::create(Vec3(0, 50, 0), Color3B(255, 255, 255), 150); addChild(light); //set the ambient light auto ambient = AmbientLight::create(Color3B(55, 55, 55)); addChild(ambient); /** Physics3DRigidBodyDes boxesrbDes; boxesrbDes.mass = 1.f; boxesrbDes.shape = Physics3DShape::createBox(Vec3(5, 5, 5)); float start_x = 10- ARRAY_SIZE_X / 2; float start_y = 0 + 5.0f; float start_z = 10 - ARRAY_SIZE_Z / 2; for (int k = 0; k<ARRAY_SIZE_Y; k++) { for (int i = 0; i<ARRAY_SIZE_X; i++) { for (int j = 0; j<ARRAY_SIZE_Z; j++) { float x = 1.0*i + start_x; float y = 5.0 + 1.0*k + start_y; float z = 1.0*j + start_z; boxesrbDes.originalTransform.setIdentity(); boxesrbDes.originalTransform.translate(x, y, z); auto sprite = PhysicsSprite3D::create("box.c3t", &boxesrbDes); sprite->setTexture("plane.png"); sprite->setCameraMask((unsigned short)CameraFlag::USER1); sprite->setScale(1.0f / sprite->getContentSize().width); this->addChild(sprite); sprite->setPosition3D(Vec3(x, y, z)); sprite->syncNodeToPhysics(); sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); } } } **/ auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //psychics_scene->setPhysics3DDebugCamera(camera); schedule(schedule_selector(HelloWorld::myupdate), .005); //this->scheduleUpdate(); return true; }
RenderTextureZbuffer::RenderTextureZbuffer() { auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(RenderTextureZbuffer::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(RenderTextureZbuffer::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(RenderTextureZbuffer::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto size = Director::getInstance()->getWinSize(); auto label = Label::createWithTTF("vertexZ = 50", "fonts/Marker Felt.ttf", 64); label->setPosition(Point(size.width / 2, size.height * 0.25f)); this->addChild(label); auto label2 = Label::createWithTTF("vertexZ = 0", "fonts/Marker Felt.ttf", 64); label2->setPosition(Point(size.width / 2, size.height * 0.5f)); this->addChild(label2); auto label3 = Label::createWithTTF("vertexZ = -50", "fonts/Marker Felt.ttf", 64); label3->setPosition(Point(size.width / 2, size.height * 0.75f)); this->addChild(label3); label->setPositionZ(50); label2->setPositionZ(0); label3->setPositionZ(-50); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Images/bugs/circle.plist"); mgr = SpriteBatchNode::create("Images/bugs/circle.png", 9); this->addChild(mgr); sp1 = Sprite::createWithSpriteFrameName("circle.png"); sp2 = Sprite::createWithSpriteFrameName("circle.png"); sp3 = Sprite::createWithSpriteFrameName("circle.png"); sp4 = Sprite::createWithSpriteFrameName("circle.png"); sp5 = Sprite::createWithSpriteFrameName("circle.png"); sp6 = Sprite::createWithSpriteFrameName("circle.png"); sp7 = Sprite::createWithSpriteFrameName("circle.png"); sp8 = Sprite::createWithSpriteFrameName("circle.png"); sp9 = Sprite::createWithSpriteFrameName("circle.png"); mgr->addChild(sp1, 9); mgr->addChild(sp2, 8); mgr->addChild(sp3, 7); mgr->addChild(sp4, 6); mgr->addChild(sp5, 5); mgr->addChild(sp6, 4); mgr->addChild(sp7, 3); mgr->addChild(sp8, 2); mgr->addChild(sp9, 1); sp1->setPositionZ(400); sp2->setPositionZ(300); sp3->setPositionZ(200); sp4->setPositionZ(100); sp5->setPositionZ(0); sp6->setPositionZ(-100); sp7->setPositionZ(-200); sp8->setPositionZ(-300); sp9->setPositionZ(-400); sp9->setScale(2); sp9->setColor(Color3B::YELLOW); }
void Node::setPosition3D(const Vec3& position) { setPositionZ(position.z); setPosition(position.x, position.y); }
//-------------------------------------------------------------- void LayerTransform::processOscCommand(const string& command, const ofxOscMessage& m) { if(isMatch(command,"position") || isMatch(command,"p")) { if(validateOscSignature("([if][if]?[if]?)|([s][if])", m)) { if(m.getArgType(0) == OFXOSC_TYPE_STRING) { char c = tolower(getArgAsStringUnchecked(m,0)[0]); if(c == 'x') { setPositionX(getArgAsFloatUnchecked(m,1)); } else if(c == 'y') { setPositionY(getArgAsFloatUnchecked(m,1)); } else if(c == 'z') { setPositionZ(getArgAsFloatUnchecked(m,1)); } else if(c == 'n') { setPositionNormalized(getArgAsBoolUnchecked(m,1)); } else { ofLogError("LayerTransform") << "invalid arg type: " << c << " " << command << "."; } } else { setPositionX(getArgAsFloatUnchecked(m,0)); if(m.getNumArgs() > 1) { setPositionY(getArgAsFloatUnchecked(m,1)); if(m.getNumArgs() > 2) { setPositionZ(getArgAsFloatUnchecked(m,2)); } } } } } else if(isMatch(command,"anchorpoint") || isMatch(command,"a")) { if(validateOscSignature("([if][if]?[if]?)|([s][if])", m)) { if(m.getArgType(0) == OFXOSC_TYPE_STRING) { char c = tolower(getArgAsStringUnchecked(m,0)[0]); if(c == 'x') { setAnchorPointX(getArgAsFloatUnchecked(m,1)); } else if(c == 'y') { setAnchorPointY(getArgAsFloatUnchecked(m,1)); } else if(c == 'z') { setAnchorPointZ(getArgAsFloatUnchecked(m,1)); } else if(c == 'n') { setAnchorPointNormalized(getArgAsBoolUnchecked(m,1)); } else { ofLog(OF_LOG_ERROR, "LayerTransform: invalid arg type: " + ofToString(c) + " " + command); } } else { setAnchorPointX(getArgAsFloatUnchecked(m,0)); if(m.getNumArgs() > 1) { setAnchorPointY(getArgAsFloatUnchecked(m,1)); if(m.getNumArgs() > 2) { setAnchorPointZ(getArgAsFloatUnchecked(m,2)); } } } } } else if(isMatch(command,"rotate") || isMatch(command,"r")) { if(validateOscSignature("([if][if]?[if]?)|([s][if][if]?[if]?)", m)) { if(m.getArgType(0) == OFXOSC_TYPE_STRING) { char c = tolower(getArgAsStringUnchecked(m,0)[0]); if(c == 'x') { setRotationX(getArgAsFloatUnchecked(m,1)); } else if(c == 'y') { setRotationY(getArgAsFloatUnchecked(m,1)); } else if(c == 'z') { setRotationZ(getArgAsFloatUnchecked(m,1)); } else if(c == 'd') { setRotation(getArgsAsPoint(m, 1)); } else if(c == 'n') { setRotationNormalized(getArgAsBoolUnchecked(m,1)); } else { ofLog(OF_LOG_ERROR, "LayerTransform: invalid arg type: " + ofToString(c) + " " + command); } } else { setRotationX(getArgAsFloatUnchecked(m,0)); if(m.getNumArgs() > 1) { setRotationY(getArgAsFloatUnchecked(m,1)); if(m.getNumArgs() > 2) { setRotationZ(getArgAsFloatUnchecked(m,2)); } } } } } else if(isMatch(command,"scale") || isMatch(command,"s")) { if(validateOscSignature("([f][f]?[f]?)|([s][f])", m)) { if(m.getArgType(0) == OFXOSC_TYPE_STRING) { char c = tolower(getArgAsStringUnchecked(m,0)[0]); float val = getArgAsFloatUnchecked(m,1); if(c == 'x') { setScaleX(val); } else if(c == 'y') { setScaleY(val); } else if(c == 'z') { setScaleZ(val); } else { ofLog(OF_LOG_ERROR, "LayerTransform: invalid arg type: " + ofToString(c) + " " + command); } } else { setScaleX(getArgAsFloatUnchecked(m,0)); if(m.getNumArgs() > 1) { setScaleY(getArgAsFloatUnchecked(m,1)); if(m.getNumArgs() > 2) { setScaleZ(getArgAsFloatUnchecked(m,2)); } } } } } else if(isMatch(command,"opacity") || isMatch(command,"o")) { if(validateOscSignature("[if]", m)) { setOpacity(getArgAsFloatUnchecked(m,0)); } } else if(isMatch(command,"opacityn") || isMatch(command,"on")) { if(validateOscSignature("[if]", m)) { setOpacityNormalized(getArgAsFloatUnchecked(m,0)); } } else if(isMatch(command,"size") || isMatch(command, "sz")) { if(validateOscSignature("[if][if]", m)) { setSize(getArgAsFloatUnchecked(m,0),getArgAsFloatUnchecked(m,1)); } } else if(isMatch(command,"width") || isMatch(command, "w")) { if(validateOscSignature("[if]", m)) { setWidth(getArgAsFloatUnchecked(m,0)); } } else if(isMatch(command,"height") || isMatch(command, "h")) { if(validateOscSignature("[if]", m)) { setHeight(getArgAsFloatUnchecked(m,0)); } } }