void Urho2DConstraints::HandleTouchBegin3(StringHash eventType, VariantMap& eventData) { Graphics* graphics = GetSubsystem<Graphics>(); PhysicsWorld2D* physicsWorld = scene_->GetComponent<PhysicsWorld2D>(); using namespace TouchBegin; RigidBody2D* rigidBody = physicsWorld->GetRigidBody(Vector2(eventData[P_X].GetInt(), eventData[P_Y].GetInt())); // Raycast for RigidBody2Ds to pick if (rigidBody) { pickedNode = rigidBody->GetNode(); StaticSprite2D* staticSprite = pickedNode->GetComponent<StaticSprite2D>(); staticSprite->SetColor(Color(1.0f, 0.0f, 0.0f, 1.0f)); // Temporary modify color of the picked sprite RigidBody2D* rigidBody = pickedNode->GetComponent<RigidBody2D>(); // Create a ConstraintMouse2D - Temporary apply this constraint to the pickedNode to allow grasping and moving with touch ConstraintMouse2D* constraintMouse = pickedNode->CreateComponent<ConstraintMouse2D>(); Vector3 pos = camera_->ScreenToWorldPoint(Vector3((float)eventData[P_X].GetInt() / graphics->GetWidth(), (float)eventData[P_Y].GetInt() / graphics->GetHeight(), 0.0f)); constraintMouse->SetTarget(Vector2(pos.x_, pos.y_)); constraintMouse->SetMaxForce(1000 * rigidBody->GetMass()); constraintMouse->SetCollideConnected(true); constraintMouse->SetOtherBody(dummyBody); // Use dummy body instead of rigidBody. It's better to create a dummy body automatically in ConstraintMouse2D constraintMouse->SetDampingRatio(0); } SubscribeToEvent(E_TOUCHMOVE, HANDLER(Urho2DConstraints, HandleTouchMove3)); SubscribeToEvent(E_TOUCHEND, HANDLER(Urho2DConstraints, HandleTouchEnd3)); }
void Urho2DSprite::CreateScene() { scene_ = new Scene(context_); scene_->CreateComponent<Octree>(); // Create camera node cameraNode_ = scene_->CreateChild("Camera"); // Set camera's position cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f)); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetOrthographic(true); Graphics* graphics = GetSubsystem<Graphics>(); camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE); ResourceCache* cache = GetSubsystem<ResourceCache>(); // Get sprite Sprite2D* sprite = cache->GetResource<Sprite2D>("Urho2D/Aster.png"); if (!sprite) return; float halfWidth = graphics->GetWidth() * 0.5f * PIXEL_SIZE; float halfHeight = graphics->GetHeight() * 0.5f * PIXEL_SIZE; for (unsigned i = 0; i < NUM_SPRITES; ++i) { SharedPtr<Node> spriteNode(scene_->CreateChild("StaticSprite2D")); spriteNode->SetPosition(Vector3(Random(-halfWidth, halfWidth), Random(-halfHeight, halfHeight), 0.0f)); StaticSprite2D* staticSprite = spriteNode->CreateComponent<StaticSprite2D>(); // Set random color staticSprite->SetColor(Color(Random(1.0f), Random(1.0f), Random(1.0f), 1.0f)); // Set blend mode staticSprite->SetBlendMode(BLEND_ALPHA); // Set sprite staticSprite->SetSprite(sprite); // Set move speed spriteNode->SetVar(VAR_MOVESPEED, Vector3(Random(-2.0f, 2.0f), Random(-2.0f, 2.0f), 0.0f)); // Set rotate speed spriteNode->SetVar(VAR_ROTATESPEED, Random(-90.0f, 90.0f)); // Add to sprite node vector spriteNodes_.Push(spriteNode); } // Get animation set AnimationSet2D* animationSet = cache->GetResource<AnimationSet2D>("Urho2D/GoldIcon.scml"); if (!animationSet) return; SharedPtr<Node> spriteNode(scene_->CreateChild("AnimatedSprite2D")); spriteNode->SetPosition(Vector3(0.0f, 0.0f, -1.0f)); AnimatedSprite2D* animatedSprite = spriteNode->CreateComponent<AnimatedSprite2D>(); // Set animation animatedSprite->SetAnimation(animationSet, "idle"); }
void Urho2DConstraints::HandleTouchEnd3(StringHash eventType, VariantMap& eventData) { if (pickedNode) { StaticSprite2D* staticSprite = pickedNode->GetComponent<StaticSprite2D>(); staticSprite->SetColor(Color(1.0f, 1.0f, 1.0f, 1.0f)); // Restore picked sprite color pickedNode->RemoveComponent<ConstraintMouse2D>(); // Remove temporary constraint pickedNode = NULL; } UnsubscribeFromEvent(E_TOUCHMOVE); UnsubscribeFromEvent(E_TOUCHEND); }
void Clockwork2DConstraints::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData) { if (pickedNode) { StaticSprite2D* staticSprite = pickedNode->GetComponent<StaticSprite2D>(); staticSprite->SetColor(Color(1.0f, 1.0f, 1.0f, 1.0f)); // Restore picked sprite color pickedNode->RemoveComponent<ConstraintMouse2D>(); // Remove temporary constraint pickedNode = NULL; } UnsubscribeFromEvent(E_MOUSEMOVE); UnsubscribeFromEvent(E_MOUSEBUTTONUP); }
void Urho2DConstraints::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData) { Input* input = GetSubsystem<Input>(); PhysicsWorld2D* physicsWorld = scene_->GetComponent<PhysicsWorld2D>(); RigidBody2D* rigidBody = physicsWorld->GetRigidBody(input->GetMousePosition().x_, input->GetMousePosition().y_, M_MAX_UNSIGNED, camera_); // Raycast for RigidBody2Ds to pick if (rigidBody) { pickedNode = rigidBody->GetNode(); //log.Info(pickedNode.name); StaticSprite2D* staticSprite = pickedNode->GetComponent<StaticSprite2D>(); staticSprite->SetColor(Color(1.0f, 0.0f, 0.0f, 1.0f)); // Temporary modify color of the picked sprite // Create a ConstraintMouse2D - Temporary apply this constraint to the pickedNode to allow grasping and moving with the mouse ConstraintMouse2D* constraintMouse = pickedNode->CreateComponent<ConstraintMouse2D>(); constraintMouse->SetTarget(GetMousePositionXY()); constraintMouse->SetMaxForce(1000 * rigidBody->GetMass()); constraintMouse->SetCollideConnected(true); constraintMouse->SetOtherBody(dummyBody); // Use dummy body instead of rigidBody. It's better to create a dummy body automatically in ConstraintMouse2D constraintMouse->SetDampingRatio(0.0f); } SubscribeToEvent(E_MOUSEMOVE, HANDLER(Urho2DConstraints, HandleMouseMove)); SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(Urho2DConstraints, HandleMouseButtonUp)); }
void AnimatedSprite2D::UpdateAnimation(float timeStep) { if (!animation_) return; currentTime_ += timeStep * speed_; float time; float animtationLength = animation_->GetLength(); if (looped_) { time = fmodf(currentTime_, animtationLength); if (time < 0.0f) time += animation_->GetLength(); } else time = Clamp(currentTime_, 0.0f, animtationLength); // Update timeline's local transform for (unsigned i = 0; i < timelineTransformInfos_.Size(); ++i) { const Timeline2D& timeline = animation_->GetTimeline(i); const Vector<TimelineKey2D>& objectKeys = timeline.timelineKeys_; unsigned index = objectKeys.Size() - 1; for (unsigned j = 0; j < objectKeys.Size() - 1; ++j) { if (time <= objectKeys[j + 1].time_) { index = j; break; } } const TimelineKey2D& currKey = objectKeys[index]; if (index < objectKeys.Size() - 1) { const TimelineKey2D& nextKey = objectKeys[index + 1]; float t = (time - currKey.time_) / (nextKey.time_ - currKey.time_); timelineTransformInfos_[i].worldSpace_ = false; timelineTransformInfos_[i].transform_ = currKey.transform_.Lerp(nextKey.transform_, t, currKey.spin_); // Update sprite's sprite and hot spot and color Node* timelineNode = timelineNodes_[i]; if (timelineNode) { StaticSprite2D* staticSprite = timelineNode->GetComponent<StaticSprite2D>(); if (staticSprite) { staticSprite->SetSprite(currKey.sprite_); staticSprite->SetHotSpot(currKey.hotSpot_.Lerp(nextKey.hotSpot_, t)); float alpha = Lerp(currKey.alpha_, nextKey.alpha_, t); staticSprite->SetColor(Color(color_.r_, color_.g_, color_.b_, color_.a_ * alpha)); } } } else { timelineTransformInfos_[i].worldSpace_ = false; timelineTransformInfos_[i].transform_ = currKey.transform_; // Update sprite's sprite and hot spot and color Node* timelineNode = timelineNodes_[i]; if (timelineNode) { StaticSprite2D* staticSprite = timelineNode->GetComponent<StaticSprite2D>(); if (staticSprite) { staticSprite->SetSprite(currKey.sprite_); staticSprite->SetHotSpot(currKey.hotSpot_); staticSprite->SetColor(Color(color_.r_, color_.g_, color_.b_, color_.a_ * currKey.alpha_)); } } } } // Calculate timeline world transform. for (unsigned i = 0; i < timelineTransformInfos_.Size(); ++i) CalculateTimelineWorldTransform(i); // Get mainline key const Vector<MainlineKey2D>& mainlineKeys = animation_->GetMainlineKeys(); const MainlineKey2D* mainlineKey = 0; for (unsigned i = 1; i < mainlineKeys.Size(); ++i) { if (time < mainlineKeys[i].time_) { mainlineKey = &mainlineKeys[i - 1]; break; } } if (!mainlineKey) mainlineKey = &mainlineKeys.Back(); // Update node's transform and sprite's z order for (unsigned i = 0; i < timelineNodes_.Size(); ++i) { Node* timelineNode = timelineNodes_[i]; if (!timelineNode) continue; const Reference2D* ref = mainlineKey->GetReference(i); if (!ref) { // Disable node if (timelineNode->IsEnabled()) timelineNode->SetEnabled(false); } else { // Enable node if (!timelineNode->IsEnabled()) timelineNode->SetEnabled(true); // Update node's transform const Transform2D& transform = timelineTransformInfos_[i].transform_; Vector2 position = transform.position_ * PIXEL_SIZE; if (flipX_) position.x_ = -position.x_; if (flipY_) position.y_ = -position.y_; timelineNode->SetPosition(position); float angle = transform.angle_; if (flipX_ != flipY_) angle = -angle; timelineNode->SetRotation(angle); timelineNode->SetScale(transform.scale_); // Update sprite's z order StaticSprite2D* staticSprite = timelineNode->GetComponent<StaticSprite2D>(); if (staticSprite) staticSprite->SetOrderInLayer(orderInLayer_ + ref->zIndex_); } } MarkForUpdate(); }