void XYMoveRectangleTool() { foreach (var iso_world in _viewCenters.Keys.ToList()) { EditorGUI.BeginChangeCheck(); var old_center = _viewCenters[iso_world]; var new_center = IsoEditorUtils.GizmoRectangle(old_center); if (EditorGUI.EndChangeCheck()) { Undo.RecordObjects( _isoParents[iso_world].Select(p => p.transform).ToArray(), _isoParents[iso_world].Count > 1 ? "Move IsoParents" : "Move IsoParent"); var old_delta = new_center - old_center; var new_delta = iso_world.IsoToScreen(IsoEditorUtils.XYMoveIsoObjects( false, iso_world, _isoObjects, _otherObjects, iso_world.ScreenToIso(old_delta))); _viewCenters[iso_world] = old_center + IsoUtils.Vec3FromVec2(new_delta); foreach (var parent in _isoParents[iso_world]) { parent.transform.position += IsoUtils.Vec3FromVec2(new_delta); } foreach (var iso_object in _isoObjects[iso_world]) { iso_object.FixIsoPosition(); iso_object.positionXY = IsoUtils.VectorBeautifier(iso_object.positionXY); } } } }
// --------------------------------------------------------------------- // // Debug draw // // --------------------------------------------------------------------- #if UNITY_EDITOR static void DrawTop(IsoWorld iso_world, Vector3 pos, Vector3 size, Vector3 rotate) { if (iso_world) { var points = new Vector3[]{ iso_world.IsoToScreen(pos), iso_world.IsoToScreen(pos + IsoUtils.Vec3FromX(size.x)), iso_world.IsoToScreen(pos + IsoUtils.Vec3FromXY(size.x, size.y)), iso_world.IsoToScreen(pos + IsoUtils.Vec3FromY(size.y)), iso_world.IsoToScreen(pos) }; if(rotate.y > 0) { // direita Handles.DrawLine(points[0], points[1] - rotate); // frente Handles.DrawLine(points[1] - rotate, points[2] - rotate); // esquerda Handles.DrawLine(points[2] - rotate, points[3]); // traz Handles.DrawLine(points[3], points[0]); }else if(rotate.x > 0) { // direita Handles.DrawLine(points[0] - new Vector3(0, rotate.x, 0), points[1] - new Vector3(0, rotate.x, 0)); // frente Handles.DrawLine(points[1] - new Vector3(0, rotate.x, 0) , points[2] ); // esquerda Handles.DrawLine(points[2] , points[3]); // traz Handles.DrawLine(points[3] , points[0] - new Vector3(0, rotate.x, 0)); } else { //normal // direita Handles.DrawLine(points[0], points[1]); // frente Handles.DrawLine(points[1], points[2]); // esquerda Handles.DrawLine(points[2], points[3]); // traz Handles.DrawLine(points[3], points[0]); } } }
void FixedUpdate() { if ( !IsoUtils.Vec3Approximately(_lastPosition, isoObject.position) ) { _lastPosition = transform.position = isoObject.position; } else if ( !IsoUtils.Vec3Approximately(_lastPosition, transform.position) ) { _lastPosition = isoObject.position = transform.position; } }
void PlaceIsoObject(IsoObject iso_object, float depth) { var iso_internal = iso_object.Internal; var old_position = iso_internal.Transform.position; iso_internal.Transform.position = IsoUtils.Vec3FromVec2(old_position, depth); }
// --------------------------------------------------------------------- // // Callbacks // // --------------------------------------------------------------------- public void OnAddIsoObject(IsoObject iso_object) { iso_object.Internal.QTItem = _quadTree.AddItem( iso_object.Internal.QTBounds, iso_object); _minIsoXY = IsoUtils.Vec2Min(_minIsoXY, iso_object.position); RegisterIsoObjectParent(iso_object); }
static void DrawIsoCubeVert(IsoWorld iso_world, Vector3 pos, Vector3 size) { if (iso_world) { var point0 = iso_world.IsoToScreen(pos); var point1 = iso_world.IsoToScreen(pos + IsoUtils.Vec3FromZ(size.z)); Handles.DrawLine(point0, point1); } }
public void LookUp(IsoRect bounds) { #if UNITY_EDITOR IsoUtils.DrawSolidRect( bounds, IsoUtils.ColorChangeA(Color.red, 0.05f), Color.red); #endif }
static void DrawVert(IsoWorld iso_world, Vector3 pos, Vector3 size) { if (iso_world) { Handles.DrawLine( iso_world.IsoToScreen(pos), iso_world.IsoToScreen(pos + IsoUtils.Vec3FromZ(size.z))); } }
void ZMoveSlider() { var iso_world = GameObject.FindObjectOfType<IsoWorld>(); if ( iso_world ) { Handles.color = Handles.zAxisColor; var delta = Handles.Slider(_viewCenter, IsoUtils.vec3OneY) - _viewCenter; if ( Mathf.Abs(delta.y) > Mathf.Epsilon ) { float tmp_y = ZMoveIsoObjects((_viewCenter.y - _center.y + delta.y) / iso_world.tileHeight); _viewCenter = _center + IsoUtils.Vec3FromY(tmp_y * iso_world.tileHeight); } } }
public static void DrawCube(IsoWorld iso_world, Vector3 center, Vector3 size, Color color, Vector3 rotate) { if (iso_world) { Handles.color = color; var pos = center - size * 0.5f; DrawTop(iso_world, pos, size, rotate); DrawTop(iso_world, pos + IsoUtils.Vec3FromZ(size.z), size, rotate); DrawVert(iso_world, pos, size); DrawVert(iso_world, pos + IsoUtils.Vec3FromX(size.x), size); DrawVert(iso_world, pos + IsoUtils.Vec3FromY(size.y), size); DrawVert(iso_world, pos + IsoUtils.Vec3FromXY(size.x, size.y), size); } }
// --------------------------------------------------------------------- // // Debug draw // // --------------------------------------------------------------------- #if UNITY_EDITOR static void DrawIsoCubeTop(IsoWorld iso_world, Vector3 pos, Vector3 size) { if (iso_world) { var point0 = iso_world.IsoToScreen(pos); var point1 = iso_world.IsoToScreen(pos + IsoUtils.Vec3FromX(size.x)); var point2 = iso_world.IsoToScreen(pos + IsoUtils.Vec3FromXY(size.x, size.y)); var point3 = iso_world.IsoToScreen(pos + IsoUtils.Vec3FromY(size.y)); Handles.DrawLine(point0, point1); Handles.DrawLine(point1, point2); Handles.DrawLine(point2, point3); Handles.DrawLine(point3, point0); } }
void GrabPositions() { var iso_world = GameObject.FindObjectOfType<IsoWorld>(); if ( iso_world ) { _positions = targets .Where(p => p as IsoObject) .Select(p => p as IsoObject) .ToDictionary(p => p, p => p.transform.position); _iso_zpositions = targets .Where(p => p as IsoObject) .Select(p => p as IsoObject) .ToDictionary(p => p, p => p.position.z); _center = _viewCenter = _positions.Aggregate(Vector3.zero, (AccIn, p) => { return AccIn + IsoUtils.Vec3FromVec2(iso_world.IsoToScreen(p.Key.position + p.Key.size * 0.5f)); }) / _positions.Count; } }
void CheckChangedTransform(IsoObject iso_object) { var iso_world = iso_object.isoWorld; if (iso_world) { var precision = Mathf.Min(iso_world.tileSize, iso_world.tileHeight) * 0.01f; var needed_position = iso_world.IsoToScreen(iso_object.position); var current_position = iso_object.transform.position; if (!IsoUtils.Vec2Approximately(needed_position, current_position, precision)) { Debug.LogWarning( "Don't change 'IsoObject.transform.position' manually!\n" + "Use 'IsoObject.position' instead.", iso_object); iso_object.FixTransform(); } } }
void XYMoveRectangleTool() { foreach (var iso_world in _viewCenters.Keys.ToList()) { EditorGUI.BeginChangeCheck(); var old_center = _viewCenters[iso_world]; var new_center = IsoEditorUtils.GizmoRectangle(old_center); if (EditorGUI.EndChangeCheck()) { var old_delta = new_center - old_center; var new_delta = iso_world.IsoToScreen(IsoEditorUtils.XYMoveIsoObjects( true, iso_world, _isoObjects, _otherObjects, iso_world.ScreenToIso(old_delta))); _viewCenters[iso_world] = old_center + IsoUtils.Vec3FromVec2(new_delta); } } }
public bool OnMarkDirtyIsoObject(IsoObject iso_object) { if (iso_object.Internal.QTItem != null) { iso_object.Internal.QTItem = _quadTree.MoveItem( iso_object.Internal.QTBounds, iso_object.Internal.QTItem); } else { iso_object.Internal.QTItem = _quadTree.AddItem( iso_object.Internal.QTBounds, iso_object); } _minIsoXY = IsoUtils.Vec2Min(_minIsoXY, iso_object.position); if (!iso_object.Internal.Dirty) { iso_object.Internal.Dirty = true; return(true); } return(false); }
// --------------------------------------------------------------------- // // // // --------------------------------------------------------------------- void PrepareTargets() { _isoObjects = targets .OfType <IsoObject>() .Where(p => p.isoWorld) .GroupBy(p => p.isoWorld) .ToDictionary(p => p.Key, p => p.ToList()); _viewCenters = _isoObjects.ToDictionary( pair => pair.Key, pair => { var iso_world = pair.Key; return(pair.Value.Aggregate(Vector3.zero, (AccIn, p) => { return AccIn + IsoUtils.Vec3FromVec2( iso_world.IsoToScreen(p.position + p.size * 0.5f)); }) / pair.Value.Count); }); _otherObjects = FindObjectsOfType <IsoObject>() .Where(p => p.IsActive() && p.isoWorld) .Where(p => _isoObjects.ContainsKey(p.isoWorld)) .Where(p => !_isoObjects[p.isoWorld].Contains(p)) .GroupBy(p => p.isoWorld) .ToDictionary(p => p.Key, p => p.ToList()); }
void ZMoveSliderTool() { foreach (var iso_world in _viewCenters.Keys.ToList()) { EditorGUI.BeginChangeCheck(); var old_center = _viewCenters[iso_world]; var new_center = IsoEditorUtils.GizmoSlider( Handles.zAxisColor, old_center, IsoUtils.vec3OneY); if (EditorGUI.EndChangeCheck()) { var old_delta = new_center - old_center; var new_delta = IsoEditorUtils.ZMoveIsoObjects( true, iso_world, _isoObjects, _otherObjects, old_delta.y / iso_world.tileHeight) * iso_world.tileHeight; _viewCenters[iso_world] = old_center + IsoUtils.Vec3FromY(new_delta); } } }
// --------------------------------------------------------------------- // // Editor // // --------------------------------------------------------------------- public static float ZMoveIsoObjects( bool move, IsoWorld iso_world, Dictionary <IsoWorld, List <IsoObject> > all_iso_objects, Dictionary <IsoWorld, List <IsoObject> > all_other_objects, float delta) { List <IsoObject> iso_objects; if (all_iso_objects.TryGetValue(iso_world, out iso_objects)) { if (move) { Undo.RecordObjects( iso_objects.ToArray(), iso_objects.Count > 1 ? "Move IsoObjects" : "Move IsoObject"); } var snapping_z = false; if (IsSnapByObjectsEnabled(iso_world)) { List <IsoObject> other_objects; if (all_other_objects.TryGetValue(iso_world, out other_objects) && other_objects.Count > 0) { foreach (var iso_object in iso_objects) { var iso_orig_z = iso_object.positionZ; var result_p_z = iso_orig_z + delta; foreach (var other in other_objects) { var x_snap_overlaps = IsoEditorUtils.IsSnapOverlaps( iso_world.snappingDistance, iso_object.positionX, iso_object.sizeX, other.positionX, other.sizeX); var y_snap_overlaps = IsoEditorUtils.IsSnapOverlaps( iso_world.snappingDistance, iso_object.positionY, iso_object.sizeY, other.positionY, other.sizeY); if (x_snap_overlaps && y_snap_overlaps) { var new_snapping_z = !snapping_z && IsoEditorUtils.IsSnapOverlaps( iso_world.snappingDistance, result_p_z, iso_object.sizeZ, other.positionZ, other.sizeZ) ? IsoEditorUtils.SnapProcess( iso_world.snappingDistance, ref result_p_z, iso_object.sizeZ, other.positionZ, other.sizeZ) : false; if (new_snapping_z) { delta = result_p_z - iso_orig_z; snapping_z = true; break; } } } if (snapping_z) { break; } } } } if (IsSnapByCellsEnabled(iso_world) && !snapping_z) { foreach (var iso_object in iso_objects) { var iso_orig_z = iso_object.positionZ; var result_p_z = iso_orig_z + delta; var new_snapping_z = IsoEditorUtils.SnapProcess( iso_world.snappingDistance, ref result_p_z, iso_object.sizeZ, iso_object.tilePositionZ, 1.0f); if (new_snapping_z) { delta = result_p_z - iso_orig_z; snapping_z = true; break; } } } return(iso_objects.Aggregate(0.0f, (AccIn, iso_object) => { var iso_orig_z = iso_object.positionZ; var result_p_z = iso_orig_z + delta; if (move) { iso_object.positionZ = IsoUtils.FloatBeautifier(result_p_z); } var z_delta = result_p_z - iso_orig_z; return Mathf.Abs(z_delta) > Mathf.Abs(AccIn) ? z_delta : AccIn; })); } return(delta); }
public static Vector3 XYMoveIsoObjects( bool move, IsoWorld iso_world, Dictionary <IsoWorld, List <IsoObject> > all_iso_objects, Dictionary <IsoWorld, List <IsoObject> > all_other_objects, Vector3 iso_delta) { List <IsoObject> iso_objects; if (all_iso_objects.TryGetValue(iso_world, out iso_objects)) { if (move) { Undo.RecordObjects( iso_objects.ToArray(), iso_objects.Count > 1 ? "Move IsoObjects" : "Move IsoObject"); } var snapping_x = false; var snapping_y = false; if (IsSnapByObjectsEnabled(iso_world)) { List <IsoObject> other_objects; if (all_other_objects.TryGetValue(iso_world, out other_objects) && other_objects.Count > 0) { foreach (var iso_object in iso_objects) { var iso_orig_p = iso_object.position; var result_pos_iso = iso_orig_p + iso_delta; foreach (var other in other_objects) { var z_snap_overlaps = IsoEditorUtils.IsSnapOverlaps( iso_world.snappingDistance, iso_object.positionZ, iso_object.sizeZ, other.positionZ, other.sizeZ); if (z_snap_overlaps) { var new_snapping_x = !snapping_x && IsoEditorUtils.IsSnapOverlaps( iso_world.snappingDistance, result_pos_iso.y, iso_object.sizeY, other.positionY, other.sizeY) ? IsoEditorUtils.SnapProcess( iso_world.snappingDistance, ref result_pos_iso.x, iso_object.sizeX, other.positionX, other.sizeX) : false; var new_snapping_y = !snapping_y && IsoEditorUtils.IsSnapOverlaps( iso_world.snappingDistance, result_pos_iso.x, iso_object.sizeX, other.positionX, other.sizeX) ? IsoEditorUtils.SnapProcess( iso_world.snappingDistance, ref result_pos_iso.y, iso_object.sizeY, other.positionY, other.sizeY) : false; if (new_snapping_x || new_snapping_y) { if (new_snapping_x) { snapping_x = true; iso_delta.x = result_pos_iso.x - iso_orig_p.x; iso_delta.y = result_pos_iso.y - iso_orig_p.y; } if (new_snapping_y) { snapping_y = true; iso_delta.x = result_pos_iso.x - iso_orig_p.x; iso_delta.y = result_pos_iso.y - iso_orig_p.y; } if (snapping_x && snapping_y) { break; } } } } if (snapping_x && snapping_y) { break; } } } } if (IsSnapByCellsEnabled(iso_world) && !snapping_x && !snapping_y) { foreach (var iso_object in iso_objects) { var iso_orig_p = iso_object.position; var result_pos_iso = iso_orig_p + iso_delta; var new_snapping_x = IsoEditorUtils.SnapProcess( iso_world.snappingDistance, ref result_pos_iso.x, iso_object.sizeX, iso_object.tilePositionX, 1.0f); var new_snapping_y = IsoEditorUtils.SnapProcess( iso_world.snappingDistance, ref result_pos_iso.y, iso_object.sizeY, iso_object.tilePositionY, 1.0f); if (new_snapping_x || new_snapping_y) { if (new_snapping_x) { iso_delta.x = result_pos_iso.x - iso_orig_p.x; iso_delta.y = result_pos_iso.y - iso_orig_p.y; snapping_x = true; } if (new_snapping_y) { iso_delta.x = result_pos_iso.x - iso_orig_p.x; iso_delta.y = result_pos_iso.y - iso_orig_p.y; snapping_y = true; } if (snapping_x && snapping_y) { break; } } } } return(iso_objects.Aggregate(Vector3.zero, (AccIn, iso_object) => { var iso_orig_p = iso_object.position; var result_pos_iso = iso_orig_p + iso_delta; if (move) { iso_object.position = IsoUtils.VectorBeautifier(result_pos_iso); } var pos_delta = result_pos_iso - iso_orig_p; return pos_delta.magnitude > AccIn.magnitude ? pos_delta : AccIn; })); } return(iso_delta); }
void OnTriggerExit(Collider collider) { isoObject.gameObject.SendMessage( "OnIsoTriggerExit", IsoUtils.IsoConvertCollider(collider), SendMessageOptions.DontRequireReceiver); }