From ead85379ed8314fa0cd5f3854295ff1664bce2db Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Mon, 22 Feb 2016 18:44:02 +0100 Subject: [PATCH] add sample uno-files --- samples/Uno/PlayerPads.uno | 154 +++++++++++++++++++++++++++++++++++++ samples/Uno/Pong.uno | 139 +++++++++++++++++++++++++++++++++ samples/Uno/TowerBlock.uno | 136 ++++++++++++++++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 samples/Uno/PlayerPads.uno create mode 100644 samples/Uno/Pong.uno create mode 100644 samples/Uno/TowerBlock.uno diff --git a/samples/Uno/PlayerPads.uno b/samples/Uno/PlayerPads.uno new file mode 100644 index 00000000..0d71a25e --- /dev/null +++ b/samples/Uno/PlayerPads.uno @@ -0,0 +1,154 @@ +using Uno; +using Uno.Collections; +using Uno.Graphics; +using Uno.Scenes; +using Uno.Designer; +using Uno.Content; +using Uno.Content.Models; +using Uno.UI; + +namespace PONG2D +{ + public class PlayerPads : Node + { + + Image _player1Image; + Image _player2Image; + + [Inline] + public Image Player1 + { + get { return _player1Image; } + set + { + if (_player1Image != value) + { + _player1Image = value; + + } + } + } + + [Inline] + public Image Player2 + { + get { return _player2Image; } + set + { + if (_player2Image != value) + { + _player2Image = value; + + } + } + } + + [Hide] + public float2 Player1Pos + { + get { return (Player1.ActualPosition); } + set + { + if (Player1 != null) + Player1.Position = value; + + } + } + + [Hide] + public float2 Player2Pos + { + get { return (Player2.ActualPosition); } + set + { + if (Player2 != null) + Player2.Position = value; + + } + } + + public Rect Player1Rect + { + get { return new Rect(Player1Pos, float2(Player1.Width, Player2.Height)); } + set + { + Player1Pos = value.Position; + if (Player1 != null) + { + Player1.Width = value.Size.X; + Player1.Height = value.Size.Y; + } + } + } + + public Rect Player2Rect + { + get { return new Rect(Player2Pos, float2(Player2.Width, Player2.Height)); } + set + { + Player2Pos = value.Position; + if (Player2 != null) + { + Player2.Width = value.Size.X; + Player2.Height = value.Size.Y; + } + } + } + + public Ball Ball + { + get; + set; + } + + public float PadVelocity { get; set; } + + public PlayerPads() + { + + } + + void UpdatePositions() + { + + } + + protected override void OnUpdate() + { + base.OnUpdate(); + + if (Input.IsKeyDown(Uno.Platform.Key.W)) + { + Player1Pos = float2(0, Player1Pos.Y - PadVelocity); + } + + if (Input.IsKeyDown(Uno.Platform.Key.S)) + { + Player1Pos = float2(0, Player1Pos.Y + PadVelocity); + } + + if (Input.IsKeyDown(Uno.Platform.Key.Up)) + { + Player2Pos = float2(0, Player2Pos.Y - PadVelocity); + } + + if (Input.IsKeyDown(Uno.Platform.Key.Down)) + { + Player2Pos = float2(0, Player2Pos.Y + PadVelocity); + } + + if (Ball != null) + { + + if (Ball.BallRectangle.Intersects(Player1Rect) || + Ball.BallRectangle.Intersects(Player2Rect)) + { + + Ball.BallVelocity = float2(Ball.BallVelocity.X * -1f, Ball.BallVelocity.Y); + } + } + + } + + } +} \ No newline at end of file diff --git a/samples/Uno/Pong.uno b/samples/Uno/Pong.uno new file mode 100644 index 00000000..4e578250 --- /dev/null +++ b/samples/Uno/Pong.uno @@ -0,0 +1,139 @@ +using Uno; +using Uno.Collections; +using Uno.Graphics; +using Uno.Scenes; +using Uno.Content; +using Uno.Content.Models; + +namespace PONG2D +{ + public class Pong : Node + { + float2 _player1Pos; + float2 _player2Pos; + float2 ballPosition; + float2 ballVelocity; + float2 rectangleSize; + + Rect player1Rect; + Rect player2Rect; + Rect ballRect; + + float2 resolution = Context.VirtualResolution; + + Random random = new Random(1); + + + float2 Player1Pos + { + get { return _player1Pos; } + set + { + _player1Pos = Math.Clamp(value, float2(0, 0), resolution - rectangleSize); + } + } + + float2 Player2Pos + { + get { return _player2Pos; } + set + { + _player2Pos = Math.Clamp(value, float2(0, 0), resolution - rectangleSize); + } + } + + public Pong() + { + Uno.Scenes.Input.AddGlobalListener(this); + } + + protected override void OnInitialize() + { + base.OnInitialize(); + UpdateValues(); + + } + + void UpdateValues() + { + rectangleSize = float2(resolution.X / 80f, resolution.Y / 5f); + _player1Pos = float2(0f); + _player2Pos = float2(Context.VirtualResolution.X - rectangleSize.X, 0f); + + player1Rect = new Rect(_player1Pos, rectangleSize); + player2Rect = new Rect(_player2Pos, rectangleSize); + + ballPosition = float2(resolution.X * 0.5f - 10f, resolution.Y * 0.5f - 10f); + ballRect = new Rect(ballPosition, float2(20f)); + + + SpwanBall(); + + } + + void SpwanBall() + { + ballRect.Position = float2(resolution.X * 0.5f - 10f, resolution.Y * 0.5f - 10f); + ballVelocity = float2(5f, 10f) * 0.5f; + } + + void OnWindowResize(object sender, EventArgs args) + { + //UpdateValues(); + } + + protected override void OnUpdate() + { + base.OnUpdate(); + + var padVelocity = resolution.Y * (float)Application.Current.FrameInterval * 4f; + if (Input.IsKeyDown(Uno.Platform.Key.Up)) + { + Player1Pos = float2(Player1Pos.X, Player1Pos.Y - padVelocity); + } + + if (Input.IsKeyDown(Uno.Platform.Key.Down)) + { + Player1Pos = float2(Player1Pos.X, Player1Pos.Y + padVelocity); + } + + if (Input.IsKeyDown(Uno.Platform.Key.W)) + { + Player2Pos = float2(Player2Pos.X, Player2Pos.Y - padVelocity); + } + + if (Input.IsKeyDown(Uno.Platform.Key.S)) + { + Player2Pos = float2(Player2Pos.X, Player2Pos.Y + padVelocity); + } + player1Rect.Position = Player1Pos; + player2Rect.Position = Player2Pos; + + if (ballRect.Position.X > resolution.X || ballRect.Position.X < 0) + { + SpwanBall(); + } + if (ballRect.Position.Y > resolution.Y || + ballRect.Position.Y < 0) + { + ballVelocity.Y *= -1f; + } + + if (ballRect.Intersects(player1Rect) || + ballRect.Intersects(player2Rect)) + { + ballVelocity.X *= -1f; + } + + ballRect.Position += ballVelocity; + + } + + protected override void OnDraw() + { + Uno.Drawing.RoundedRectangle.Draw(player1Rect.Position, player1Rect.Size, float4(1f), 0); + Uno.Drawing.RoundedRectangle.Draw(player2Rect.Position, player2Rect.Size, float4(1f), 0); + Uno.Drawing.RoundedRectangle.Draw(ballRect.Position, ballRect.Size, float4(1f), 0f); + } + } +} \ No newline at end of file diff --git a/samples/Uno/TowerBlock.uno b/samples/Uno/TowerBlock.uno new file mode 100644 index 00000000..a25d6849 --- /dev/null +++ b/samples/Uno/TowerBlock.uno @@ -0,0 +1,136 @@ +using Uno; +using Uno.Collections; +using Uno.Graphics; +using Uno.Scenes; +using Uno.Content; +using Uno.Content.Models; +using Uno.Physics.Box2D; + +using TowerBuilder.Box2DMath; + +namespace TowerBuilder +{ + public class TowerBlock : TestBed + { + Body floorBody, deleteBody, mouseBody; + + private List bodies = new List(); + private List bodiesToDelete = new List(); + + private ContactListener contactListener; + + protected override void OnInitializeTestBed() + { + World.Gravity = float2(0, -25.0f); + World.ContactListener = contactListener = new ContactListener(this); + + bodies.Clear(); + bodiesToDelete.Clear(); + + CreateFloor(); + CreateDeleteBody(); + CreateBox2(); + } + + void CreateFloor() + { + var bodyDef = new BodyDef(); + bodyDef.position = float2(0, -40.0f); + + floorBody = World.CreateBody(bodyDef); + + var shape = new PolygonShape(); + shape.SetAsBox(30.0f, 10.0f); + + var fixtureDef = new FixtureDef(); + fixtureDef.shape = shape; + fixtureDef.density = 1.0f; + + floorBody.CreateFixture(fixtureDef); + } + + void CreateDeleteBody() + { + var bodyDef = new BodyDef(); + bodyDef.position = float2(0, -44.0f); + + deleteBody = World.CreateBody(bodyDef); + + var shape = new PolygonShape(); + shape.SetAsBox(200.0f, 10.0f); + + var fixtureDef = new FixtureDef(); + fixtureDef.shape = shape; + fixtureDef.density = 1.0f; + + deleteBody.CreateFixture(fixtureDef); + } + + Random random = new Random((int) (Uno.Diagnostics.Clock.GetSeconds() * 1000000)); + void CreateBox2() + { + var bodyDef = new BodyDef(); + bodyDef.type = BodyType.Dynamic; + bodyDef.position = float2(random.NextFloat(-25f, 25f), 50.0f); + bodyDef.angularVelocity = random.NextFloat() * 40 - 20; + bodyDef.userData = float3(0, 0, 0); + + var body = World.CreateBody(bodyDef); + + var shape = new PolygonShape(); + shape.SetAsBox(0.75f, 0.75f); + + var fixtureDef = new FixtureDef(); + fixtureDef.shape = shape; + fixtureDef.density = 5.0f; + //fixtureDef.friction = 0.75f; + + body.CreateFixture(fixtureDef); + + bodies.Add(body); + } + + private int c = 0; + protected override void OnFixedUpdate() + { + base.OnFixedUpdate(); + + debug_log bodies.Count; + if(c++ % 8 == 0 && bodies.Count < 20) CreateBox2(); + + foreach(var body in bodiesToDelete) + { + World.DestroyBody(body); + bodies.Remove(body); + } + + bodiesToDelete.Clear(); + } + + public class ContactListener : IContactListener + { + private TowerBlock b; + public ContactListener(TowerBlock b) + { + this.b = b; + } + + public void BeginContact(Contact contact) + { + if(contact.GetFixtureA().GetBody() == b.deleteBody) + { + b.bodiesToDelete.Add(contact.GetFixtureB().GetBody()); + } + else if(contact.GetFixtureB().GetBody() == b.deleteBody) + { + b.bodiesToDelete.Add(contact.GetFixtureA().GetBody()); + } + } + + public void EndContact(Contact contact) {} + public void PreSolve(Contact contact, ref Manifold manifold) {} + public void PostSolve(Contact contact, ref ContactImpulse impulse) {} + } + + } +}