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 AnimatedSprite2D::OnBlendModeChanged() { for (unsigned i = 0; i < numTracks_; ++i) { if (!trackNodes_[i]) continue; StaticSprite2D* staticSprite = trackNodes_[i]->GetComponent<StaticSprite2D>(); staticSprite->SetBlendMode(blendMode_); } }
void AnimatedSprite2D::SetBlendMode(BlendMode blendMode) { if (blendMode == blendMode_) return; blendMode_ = blendMode; for (unsigned i = 0; i < timelineNodes_.Size(); ++i) { if (!timelineNodes_[i]) continue; StaticSprite2D* staticSprite = timelineNodes_[i]->GetComponent<StaticSprite2D>(); staticSprite->SetBlendMode(blendMode_); } }
void AnimatedSprite2D::SetAnimation(Animation2D* animation, LoopMode2D loopMode) { if (animation == animation_) { SetLoopMode(loopMode_); currentTime_ = 0.0f; UpdateAnimation(0.0f); return; } for (unsigned i = 0; i < numTracks_; ++i) { if (trackNodes_[i]) trackNodes_[i]->SetEnabled(false); } numTracks_ = 0; trackNodes_.Clear(); trackNodeInfos_.Clear(); animation_ = animation; if (!animation_) return; currentTime_ = 0.0f; if (!rootNode_) { rootNode_ = GetNode()->CreateChild("_root_", LOCAL); rootNode_->SetTemporary(true); } numTracks_ = animation_->GetNumTracks(); trackNodes_.Resize(numTracks_); trackNodeInfos_.Resize(numTracks_); for (unsigned i = 0; i < numTracks_; ++i) { const AnimationTrack2D& track = animation->GetTrack(i); SharedPtr<Node> trackNode(rootNode_->GetChild(track.name_)); StaticSprite2D* staticSprite = 0; if (trackNode) { // Enable track node trackNode->SetEnabled(true); // Get StaticSprite2D component if (track.hasSprite_) staticSprite = trackNode->GetComponent<StaticSprite2D>(); } else { // Create new track node trackNode = rootNode_->CreateChild(track.name_, LOCAL); trackNode->SetTemporary(true); // Create StaticSprite2D component if (track.hasSprite_) staticSprite = trackNode->CreateComponent<StaticSprite2D>(); } if (staticSprite) { staticSprite->SetLayer(layer_); staticSprite->SetBlendMode(blendMode_); staticSprite->SetFlip(flipX_, flipY_); staticSprite->SetUseHotSpot(true); } trackNodes_[i] = trackNode; trackNodeInfos_[i].hasSprite = track.hasSprite_; } SetLoopMode(loopMode); UpdateAnimation(0.0f); MarkNetworkUpdate(); }
void AnimatedSprite2D::SetAnimation(Animation2D* animation, LoopMode2D loopMode) { if (animation == animation_) { SetLoopMode(loopMode_); currentTime_ = 0.0f; UpdateAnimation(0.0f); return; } for (unsigned i = 0; i < timelineNodes_.Size(); ++i) { if (timelineNodes_[i]) timelineNodes_[i]->SetEnabled(false); } timelineNodes_.Clear(); animation_ = animation; if (!animation_) return; currentTime_ = 0.0f; if (!rootNode_) { rootNode_ = GetNode()->CreateChild("RootNode", LOCAL); rootNode_->SetTemporary(true); } timelineNodes_.Resize(animation_->GetNumTimelines()); timelineTransformInfos_.Resize(animation_->GetNumTimelines()); for (unsigned i = 0; i < animation_->GetNumTimelines(); ++i) { const Timeline2D& timeline = animation->GetTimeline(i); SharedPtr<Node> timelineNode(rootNode_->GetChild(timeline.name_)); StaticSprite2D* staticSprite = 0; if (timelineNode) { // Enable timeline node timelineNode->SetEnabled(true); // Get StaticSprite2D component if (timeline.type_ == OT_SPRITE) staticSprite = timelineNode->GetComponent<StaticSprite2D>(); } else { // Create new timeline node timelineNode = rootNode_->CreateChild(timeline.name_, LOCAL); // Create StaticSprite2D component if (timeline.type_ == OT_SPRITE) staticSprite = timelineNode->CreateComponent<StaticSprite2D>(); } if (staticSprite) { staticSprite->SetLayer(layer_); staticSprite->SetBlendMode(blendMode_); staticSprite->SetFlip(flipX_, flipY_); staticSprite->SetUseHotSpot(true); } timelineNodes_[i] = timelineNode; timelineTransformInfos_[i].parent_ = timeline.parent_; } SetLoopMode(loopMode); UpdateAnimation(0.0f); MarkNetworkUpdate(); }