public Bullet CreateBullet() { GameObject obj = new GameObject("Bullet"); Transform transform = obj.AddComponent<Transform>(); RigidBody body = obj.AddComponent<RigidBody>(); SpriteRenderer sprite = obj.AddComponent<SpriteRenderer>(); Bullet bullet = obj.AddComponent<Bullet>(); Material spriteMaterial = this.spriteMaterial.Res ?? Material.SolidWhite.Res; Vector2 spriteSize = spriteMaterial.MainTexture.IsAvailable ? spriteMaterial.MainTexture.Res.Size : new Vector2(5, 5); float spriteRadius = MathF.Max(spriteSize.X, spriteSize.Y) * 0.25f; body.ClearShapes(); CircleShapeInfo circleShape = new CircleShapeInfo(spriteRadius, Vector2.Zero, 1.0f); circleShape.IsSensor = true; body.AddShape(circleShape); body.CollisionCategory = CollisionCategory.Cat3; body.CollidesWith &= ~CollisionCategory.Cat3; sprite.SharedMaterial = this.spriteMaterial; sprite.Rect = Rect.Align(Alignment.Center, 0.0f, 0.0f, spriteSize.X * 0.5f, spriteSize.Y * 0.5f); bullet.InitFrom(this); return bullet; }
protected override void OnCopyTo(ShapeInfo target) { base.OnCopyTo(target); CircleShapeInfo c = target as CircleShapeInfo; c.radius = this.radius; c.position = this.position; }
public override void BeginAction() { base.BeginAction(); this.beginLocalPos = this.Environment.ActiveBodyPos; this.actionCircle = new CircleShapeInfo(1.0f, this.Environment.ActiveBodyPos, 1.0f); UndoRedoManager.Do(new CreateRigidBodyShapeAction(this.Environment.ActiveBody, this.actionCircle)); this.Environment.SelectShapes(new ShapeInfo[] { this.actionCircle }); }
protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); GameObject selGameObj = this.selectedBody != null ? this.selectedBody.GameObj : null; Transform selTransform = selGameObj != null ? selGameObj.Transform : null; if (selTransform == null) return; Vector3 spaceCoord = this.GetSpaceCoord(new Vector3(e.X, e.Y, selTransform.Pos.Z)); Vector2 localPos = selTransform.GetLocalPoint(spaceCoord).Xy; if ((this.SnapToUserGuides & UserGuideType.Position) != UserGuideType.None) { localPos = this.EditingUserGuide.SnapPosition(localPos); } if (this.mouseState == CursorState.CreateCircle) { #region CreateCircle if (e.Button == MouseButtons.Left) { CircleShapeInfo newShape = new CircleShapeInfo(1.0f, localPos, 1.0f); UndoRedoManager.BeginMacro(); UndoRedoManager.Do(new CreateRigidBodyShapeAction(this.selectedBody, newShape)); this.createAction = true; this.LeaveCursorState(); this.SelectObjects(new[] { SelShape.Create(newShape) }); this.BeginAction(ObjectAction.Scale); } else if (e.Button == MouseButtons.Right) { this.LeaveCursorState(); } #endregion } else if (this.mouseState == CursorState.CreatePolygon) { #region CreatePolygon if (e.Button == MouseButtons.Left) { bool success = false; if (!this.allObjSel.Any(sel => sel is SelPolyShape)) { PolyShapeInfo newShape = new PolyShapeInfo(new Vector2[] { localPos, localPos, localPos }, 1.0f); UndoRedoManager.Do(new CreateRigidBodyShapeAction(this.selectedBody, newShape)); this.SelectObjects(new[] { SelShape.Create(newShape) }); this.createPolyIndex++; } else { SelPolyShape selPolyShape = this.allObjSel.OfType<SelPolyShape>().First(); PolyShapeInfo polyShape = selPolyShape.ActualObject as PolyShapeInfo; if (this.createPolyIndex <= 2 || MathF.IsPolygonConvex(polyShape.Vertices)) { Vector2 lockedPos = this.createPolyIndex > 0 ? polyShape.Vertices[this.createPolyIndex - 1] : Vector2.Zero; MathF.TransformCoord(ref lockedPos.X, ref lockedPos.Y, selTransform.Angle); MathF.TransformCoord(ref localPos.X, ref localPos.Y, selTransform.Angle); localPos = this.ApplyAxisLock(localPos, lockedPos); MathF.TransformCoord(ref localPos.X, ref localPos.Y, -selTransform.Angle); if (polyShape.Vertices.Length < PolyShapeInfo.MaxVertices) { List<Vector2> vertices = polyShape.Vertices.ToList(); vertices[this.createPolyIndex] = localPos; if (this.createPolyIndex >= vertices.Count - 1) vertices.Add(localPos); polyShape.Vertices = vertices.ToArray(); selPolyShape.UpdatePolyStats(); this.createPolyIndex++; } else { Vector2[] vertices = polyShape.Vertices; vertices[this.createPolyIndex] = localPos; polyShape.Vertices = vertices; selPolyShape.UpdatePolyStats(); this.LeaveCursorState(); } } } if (success) { DualityEditorApp.NotifyObjPropChanged(this, new ObjectSelection(this.selectedBody), ReflectionInfo.Property_RigidBody_Shapes); } } else if (e.Button == MouseButtons.Right) { if (this.allObjSel.Any(sel => sel is SelPolyShape)) { SelPolyShape selPolyShape = this.allObjSel.OfType<SelPolyShape>().First(); PolyShapeInfo polyShape = selPolyShape.ActualObject as PolyShapeInfo; List<Vector2> vertices = polyShape.Vertices.ToList(); vertices.RemoveAt(this.createPolyIndex); if (vertices.Count < 3 || this.createPolyIndex < 2) { this.DeleteObjects(new SelPolyShape[] { selPolyShape }); } else { polyShape.Vertices = vertices.ToArray(); selPolyShape.UpdatePolyStats(); } DualityEditorApp.NotifyObjPropChanged(this, new ObjectSelection(this.selectedBody), ReflectionInfo.Property_RigidBody_Shapes); } this.LeaveCursorState(); } #endregion } else if (this.mouseState == CursorState.CreateLoop) { #region CreateLoop if (e.Button == MouseButtons.Left) { bool success = false; if (!this.allObjSel.Any(sel => sel is SelLoopShape)) { LoopShapeInfo newShape = new LoopShapeInfo(new Vector2[] { localPos, localPos + Vector2.UnitX, localPos + Vector2.One }); UndoRedoManager.Do(new CreateRigidBodyShapeAction(this.selectedBody, newShape)); this.SelectObjects(new[] { SelShape.Create(newShape) }); success = true; } else { SelLoopShape selPolyShape = this.allObjSel.OfType<SelLoopShape>().First(); LoopShapeInfo polyShape = selPolyShape.ActualObject as LoopShapeInfo; List<Vector2> vertices = polyShape.Vertices.ToList(); Vector2 lockedPos = this.createPolyIndex > 0 ? vertices[this.createPolyIndex - 1] : Vector2.Zero; MathF.TransformCoord(ref lockedPos.X, ref lockedPos.Y, selTransform.Angle); MathF.TransformCoord(ref localPos.X, ref localPos.Y, selTransform.Angle); localPos = this.ApplyAxisLock(localPos, lockedPos); MathF.TransformCoord(ref localPos.X, ref localPos.Y, -selTransform.Angle); vertices[this.createPolyIndex] = localPos; if (this.createPolyIndex >= vertices.Count - 1) vertices.Add(localPos); polyShape.Vertices = vertices.ToArray(); selPolyShape.UpdateLoopStats(); success = true; } if (success) { this.createPolyIndex++; DualityEditorApp.NotifyObjPropChanged(this, new ObjectSelection(this.selectedBody), ReflectionInfo.Property_RigidBody_Shapes); } } else if (e.Button == MouseButtons.Right) { if (this.allObjSel.Any(sel => sel is SelLoopShape)) { SelLoopShape selPolyShape = this.allObjSel.OfType<SelLoopShape>().First(); LoopShapeInfo polyShape = selPolyShape.ActualObject as LoopShapeInfo; List<Vector2> vertices = polyShape.Vertices.ToList(); vertices.RemoveAt(this.createPolyIndex); if (vertices.Count < 3 || this.createPolyIndex < 2) { this.DeleteObjects(new SelLoopShape[] { selPolyShape }); } else { polyShape.Vertices = vertices.ToArray(); selPolyShape.UpdateLoopStats(); } DualityEditorApp.NotifyObjPropChanged(this, new ObjectSelection(this.selectedBody), ReflectionInfo.Property_RigidBody_Shapes); } this.LeaveCursorState(); } #endregion } }
public SelCircleShape(CircleShapeInfo shape) : base(shape) { this.circle = shape; }
private void DrawShapeOutline(Canvas canvas, Transform transform, CircleShapeInfo shape) { Vector3 pos = transform.Pos; float angle = transform.Angle; float scale = transform.Scale; if (this.wrapTexture) { canvas.State.TextureCoordinateRect = new Rect( shape.Radius * 2.0f / canvas.State.TextureBaseSize.X, shape.Radius * 2.0f / canvas.State.TextureBaseSize.Y); } canvas.State.TransformScale = new Vector2(scale, scale); canvas.State.TransformAngle = angle; canvas.State.TransformHandle = -shape.Position; canvas.FillCircleSegment( pos.X, pos.Y, pos.Z, shape.Radius, 0.0f, MathF.RadAngle360, this.outlineWidth); }
public RigidBodyEditorSelCircleShape(CircleShapeInfo shape) : base(shape) { this.circle = shape; }
public override void EndAction() { base.EndAction(); this.actionCircle = null; }