void loadBody(StadiumObject stadiumObject) { StaticBody2D body = new StaticBody2D(); ImageTexture texture = new ImageTexture(); texture.Load(stadiumObject.TexturePath); Sprite sprite = new Sprite(); sprite.Texture = texture; if (stadiumObject.Shape == StadiumObject.Type.RECT) { sprite.Centered = false; } body.AddChild(sprite); float positionX = (float)(stadiumObject.ShapeStruct.Position.X); float positionY = (float)(stadiumObject.ShapeStruct.Position.Y); body.Position = new Godot.Vector2(positionX, positionY); if (stadiumObject.Shape == StadiumObject.Type.RECT) { body = loadRectBody(stadiumObject, body); } else { body = loadCircBody(stadiumObject, body); } AddChild(body); }
public void AddStaticBodyCollider(Area2D prop) { var shape = prop.GetNode <CollisionShape2D>("Shape"); var body = new StaticBody2D(); body.AddChild(shape.Duplicate()); body.CollisionLayer = 1024; prop.AddChild(body); }
/** * Handles inputs for the simulation */ public override void _Input(InputEvent inputEvent) { //Adds a liquid particle to the mouse's position if the left mouse button is pressed if (Input.IsActionPressed("add_liquid_particle") && this._framesUntilNextLiquidParticleCreation == 0) { var liquidParticleInstance = (Node2D)this._liquidParticleScene.Instance(); liquidParticleInstance.Position = this.GetViewport().GetMousePosition(); this.AddChild(liquidParticleInstance); this._liquidParticles.Add((LiquidParticle)liquidParticleInstance); this._framesUntilNextLiquidParticleCreation = 5; } //If the right mouse button is being pressed, creates a blocker from the start of the press to the end of the press if (Input.IsActionPressed("add_blocker")) { if (this._blockerSegmentBeingPlaced == null) { this._blockerSegmentBeingPlaced = new SegmentShape2D { A = this.GetViewport().GetMousePosition(), B = this.GetViewport().GetMousePosition() }; var body = new StaticBody2D(); body.AddChild(new CollisionShape2D { Shape = this._blockerSegmentBeingPlaced }); this.AddChild(body); this._blockers.Add(this._blockerSegmentBeingPlaced); this.Update(); } else { this._blockerSegmentBeingPlaced.B = this.GetViewport().GetMousePosition(); this.Update(); } } else { this._blockerSegmentBeingPlaced = null; this.Update(); } //Removes all children if the escape key is pressed if (Input.IsActionPressed("clear_everything")) { foreach (var child in this.GetChildren()) { this.RemoveChild((Node)child); } this._liquidParticles.Clear(); this._springs.Clear(); this._blockers.Clear(); this.Update(); } }
StaticBody2D loadCircBody(StadiumObject stadiumObject, StaticBody2D body) { CollisionShape2D collider = new CollisionShape2D(); CircleShape2D shape = new CircleShape2D(); CircStruct circStruct = (CircStruct)stadiumObject.ShapeStruct; shape.Radius = (float)circStruct.Size.X / 2; collider.Shape = shape; body.AddChild(collider); ImageTexture texture = new ImageTexture(); texture.Load(stadiumObject.TexturePath); Sprite sprite = new Sprite(); sprite.Texture = texture; body.AddChild(sprite); return(body); }
StaticBody2D loadRectBody(StadiumObject stadiumObject, StaticBody2D body) { CollisionShape2D collider = new CollisionShape2D(); RectangleShape2D shape = new RectangleShape2D(); RectStruct rectStruct = (RectStruct)stadiumObject.ShapeStruct; shape.Extents = new Godot.Vector2((float)rectStruct.Position.X / 2, (float)rectStruct.Position.Y / 2); collider.Shape = shape; body.AddChild(collider); return(body); }