Created
July 19, 2022 06:24
-
-
Save slimshader/bf1f14b4256c19fcad7db22ec557128e to your computer and use it in GitHub Desktop.
Taking .NET game development in Stride Code first demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Stride.Core.Mathematics; | |
| using Stride.Engine; | |
| using Stride.GameDefaults.ProceduralModels; | |
| using Stride.GameDefaults.Extensions; | |
| using Stride.Physics; | |
| using Stride.Input; | |
| using (var game = new Game()) | |
| { | |
| game.Run(start: Start); | |
| void Start(Scene rootScene) | |
| { | |
| game.SetupBase3DScene(); | |
| game.AddProfiler(); | |
| var entity = game.AddBoxToScene(); | |
| entity.Add(new MoveComponentScript()); | |
| var boxGenerator = new Entity(); | |
| boxGenerator.Scene = rootScene; | |
| boxGenerator.Add(new BoxGeneratorScript()); | |
| var hitComponent = new Entity(); | |
| hitComponent.Scene = rootScene; | |
| hitComponent.Add(new HitComponentScript()); | |
| } | |
| } | |
| public class BoxGeneratorScript : SyncScript | |
| { | |
| private CameraComponent? _camera; | |
| private Simulation? _simulation; | |
| public override void Start() | |
| { | |
| _camera = Entity.Scene.Entities.FirstOrDefault(x => x.Get<CameraComponent>() != null)?.Get<CameraComponent>(); | |
| _simulation = SceneSystem.SceneInstance.GetProcessor<PhysicsProcessor>()?.Simulation; | |
| } | |
| public override void Update() | |
| { | |
| if (_camera is null || _simulation is null) return; | |
| if (Input.HasMouse && Input.IsMouseButtonPressed(MouseButton.Left)) | |
| { | |
| var ray = _camera.ScreenPointToRay(Input.MousePosition); | |
| var hit = _simulation.Raycast(ray.VectorNear.XYZ(), ray.VectorFar.XYZ()); | |
| if (hit.Succeeded) return; | |
| var entity = ((Game)Game).AddBoxToScene(); | |
| } | |
| } | |
| } | |
| public class MoveComponentScript : SyncScript | |
| { | |
| private RigidbodyComponent? _rigidbody; | |
| private float _force = 4500f; | |
| public override void Start() | |
| { | |
| _rigidbody = Entity.Get<RigidbodyComponent>(); | |
| _rigidbody.Mass = 1000; | |
| } | |
| public override void Update() | |
| { | |
| if (_rigidbody is null) return; | |
| if (Math.Round(_rigidbody.LinearVelocity.Length(), 1) == 0) | |
| { | |
| _rigidbody.ApplyImpulse(new Vector3(0, 0, _force)); | |
| _force *= -1; | |
| } | |
| } | |
| } | |
| public class HitComponentScript : SyncScript | |
| { | |
| private CameraComponent? _camera; | |
| private Simulation? _simulation; | |
| public override void Start() | |
| { | |
| _camera = Entity.Scene.Entities.FirstOrDefault(x => x.Get<CameraComponent>() != null)?.Get<CameraComponent>(); | |
| _simulation = SceneSystem.SceneInstance.GetProcessor<PhysicsProcessor>()?.Simulation; | |
| } | |
| public override void Update() | |
| { | |
| if (Vector3.Distance(Vector3.Zero, Entity.Transform.Position) > 30) | |
| { | |
| Entity.Scene = null; | |
| } | |
| if (_camera is null || _simulation is null) return; | |
| if (Input.HasMouse && Input.IsMouseButtonPressed(MouseButton.Left)) | |
| { | |
| var ray = _camera.ScreenPointToRay(Input.MousePosition); | |
| var hit = _simulation.Raycast(ray.VectorNear.XYZ(), ray.VectorFar.XYZ()); | |
| if (hit.Succeeded) | |
| { | |
| var rigidbody = hit.Collider.Entity.Get<RigidbodyComponent>(); | |
| if (rigidbody is null) return; | |
| rigidbody.ClearForces(); | |
| rigidbody.ApplyImpulse(new Vector3(-28, 0, 28)); | |
| } | |
| } | |
| } | |
| } | |
| public static class GameExtensions | |
| { | |
| public static Entity AddBoxToScene(this Game game) | |
| { | |
| var entity = game.CreatePrimitive(PrimitiveModelType.Cube, material: game.NewDefaultMaterial(Color.DarkGoldenrod)); | |
| entity.Transform.Position = new Vector3(0, 2.5f, 0); | |
| entity.Scene = game.SceneSystem.SceneInstance.RootScene; | |
| return entity; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment