浏览代码

added valueMove (similar to angleMove)

Nicolas Cannasse 4 年之前
父节点
当前提交
a07ce01435
共有 1 个文件被更改,包括 14 次插入0 次删除
  1. 14 0
      hxd/Math.hx

+ 14 - 0
hxd/Math.hx

@@ -210,6 +210,20 @@ class Math {
 		return if( da > -max && da < max ) b else a + (da < 0 ? -max : max);
 	}
 
+	/**
+		Move a value towards the given target using the max increment. Return the new value.
+	**/
+	public static inline function valueMove( v : Float, target : Float, max : Float ) {
+		if( v < target ) {
+			v += max;
+			if( v > target ) v = target;
+		} else if( v > target ) {
+			v -= max;
+			if( v < target ) v = target;
+		}
+		return v;
+	}
+
 	public static inline function shuffle<T>( a : Array<T> ) {
 		var len = a.length;
 		for( i in 0...len ) {