Browse Source

Adds a speed factor to AnimatedSprite

groud 7 years ago
parent
commit
89fe7e2f92
2 changed files with 21 additions and 2 deletions
  1. 17 2
      scene/2d/animated_sprite.cpp
  2. 4 0
      scene/2d/animated_sprite.h

+ 17 - 2
scene/2d/animated_sprite.cpp

@@ -351,7 +351,7 @@ void AnimatedSprite::_notification(int p_what) {
 			if (frame < 0)
 				return;
 
-			float speed = frames->get_animation_speed(animation);
+			float speed = frames->get_animation_speed(animation) * speed_scale;
 			if (speed == 0)
 				return; //do nothing
 
@@ -481,6 +481,16 @@ int AnimatedSprite::get_frame() const {
 	return frame;
 }
 
+void AnimatedSprite::set_speed_scale(float p_speed_scale) {
+
+	speed_scale = MAX(p_speed_scale, 0.0f);
+}
+
+float AnimatedSprite::get_speed_scale() const {
+
+	return speed_scale;
+}
+
 void AnimatedSprite::set_centered(bool p_center) {
 
 	centered = p_center;
@@ -570,7 +580,7 @@ void AnimatedSprite::_reset_timeout() {
 		return;
 
 	if (frames.is_valid() && frames->has_animation(animation)) {
-		float speed = frames->get_animation_speed(animation);
+		float speed = frames->get_animation_speed(animation) * speed_scale;
 		if (speed > 0) {
 			timeout = 1.0 / speed;
 		} else {
@@ -636,6 +646,9 @@ void AnimatedSprite::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite::set_frame);
 	ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite::get_frame);
 
+	ClassDB::bind_method(D_METHOD("set_speed_scale", "speed_scale"), &AnimatedSprite::set_speed_scale);
+	ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedSprite::get_speed_scale);
+
 	ClassDB::bind_method(D_METHOD("_res_changed"), &AnimatedSprite::_res_changed);
 
 	ADD_SIGNAL(MethodInfo("frame_changed"));
@@ -644,6 +657,7 @@ void AnimatedSprite::_bind_methods() {
 	ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames");
 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "animation"), "set_animation", "get_animation");
 	ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "frame", PROPERTY_HINT_SPRITE_FRAME), "set_frame", "get_frame");
+	ADD_PROPERTYNO(PropertyInfo(Variant::REAL, "speed_scale"), "set_speed_scale", "get_speed_scale");
 	ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "playing"), "_set_playing", "_is_playing");
 	ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
 	ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
@@ -658,6 +672,7 @@ AnimatedSprite::AnimatedSprite() {
 	vflip = false;
 
 	frame = 0;
+	speed_scale = 1.0f;
 	playing = false;
 	animation = "default";
 	timeout = 0;

+ 4 - 0
scene/2d/animated_sprite.h

@@ -129,6 +129,7 @@ class AnimatedSprite : public Node2D {
 	bool playing;
 	StringName animation;
 	int frame;
+	float speed_scale;
 
 	bool centered;
 	Point2 offset;
@@ -172,6 +173,9 @@ public:
 	void set_frame(int p_frame);
 	int get_frame() const;
 
+	void set_speed_scale(float p_speed_scale);
+	float get_speed_scale() const;
+
 	void set_centered(bool p_center);
 	bool is_centered() const;