コード例 #1
0
        protected override void Update(GameTime gameTime)
        {
            _mapRenderer.Update(gameTime);

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(_movementController.ExitGame))
            {
                Exit();
            }

            // TODO: Add your update logic here
            _movementController.Update(gameTime);

            if (_player != null)
            {
                // We want to clamp camera coordinates so that when the player gets to < CamWidth / 2 or World.MaxX - CamWidth / 2
                // the camera doesnt change in x. Do the same for y.
                var x = _player.Position.X;
                var y = _player.Position.Y;

                _cameraTarget.X = MathHelper.Clamp(x, _minCameraX, _maxCameraX);
                _cameraTarget.Y = MathHelper.Clamp(y, _minCameraY, _maxCameraY);

                _camera.LookAt(_cameraTarget);
            }

            _entityManager.Update(gameTime);

            base.Update(gameTime);
        }
コード例 #2
0
 private void LookAtPlayer()
 {
     if (GameProcess.Player is not null)
     {
         camera.LookAt(GameProcess.Player.Position.RenderVector);
     }
 }
コード例 #3
0
ファイル: Main.cs プロジェクト: Solargale/CollectorRust
        protected override void Initialize()
        {
            _gui = new Gui(_desktop);

            base.Initialize();

            foreach (Blocks name in Enum.GetValues(typeof(Blocks)))
            {
                Materials.Add(name, Content.Load <Texture2D>(name.ToString()));
            }

            _virtualWidth  = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
            _virtualHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;

            var viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, _virtualWidth, _virtualHeight);

            _world  = new World();
            Player1 = new Player(0, 0, _world);
            _cam    = new OrthographicCamera(viewportAdapter);
            _cam.LookAt(new Vector2(Player.X, Player.Y));
            _cam.Zoom = IRestrictions.Zoom;

            _spriteBatch     = new SpriteBatch(GraphicsDevice);
            _playerMouse     = new PlayerMouse(Content, _spriteBatch, _cam);
            _inputController = new InputController(_cam, _spriteBatch, Content, this, _world, Player1, _playerMouse);
            WorldRenderer    = new WorldRenderer(_playerMouse, _inputController, _spriteBatch, this, _world);
        }
コード例 #4
0
 protected override void LoadContent()
 {
     _spriteBatch  = new SpriteBatch(GraphicsDevice);
     player.sprite = Content.Load <Texture2D>("Textures/Player");
     camPos        = new Vector2(100, 100);
     cam.LookAt(camPos);
     map         = Content.Load <TiledMap>("map");
     mapRenderer = new TiledMapRenderer(GraphicsDevice, map);
 }
コード例 #5
0
        protected override void Update(GameTime gameTime)
        {
            var deltaTime     = gameTime.GetElapsedSeconds();
            var keyboardState = Keyboard.GetState();
            var mouseState    = Mouse.GetState();

            if (keyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (_player != null && !_player.IsDestroyed)
            {
                const float acceleration = 5f;

                if (keyboardState.IsKeyDown(Keys.W) || keyboardState.IsKeyDown(Keys.Up))
                {
                    _player.Accelerate(acceleration);
                }

                if (keyboardState.IsKeyDown(Keys.S) || keyboardState.IsKeyDown(Keys.Down))
                {
                    _player.Accelerate(-acceleration);
                }

                if (keyboardState.IsKeyDown(Keys.A) || keyboardState.IsKeyDown(Keys.Left))
                {
                    _player.Rotation += deltaTime * 3f;
                }

                if (keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Right))
                {
                    _player.Rotation -= deltaTime * 3f;
                }

                if (keyboardState.IsKeyDown(Keys.Space) || mouseState.LeftButton == ButtonState.Pressed)
                {
                    _player.Fire();
                }

                if (_previousMouseState.X != mouseState.X || _previousMouseState.Y != mouseState.Y)
                {
                    _player.LookAt(_camera.ScreenToWorld(new Vector2(mouseState.X, mouseState.Y)));
                }

                _camera.LookAt(_player.Position + _player.Velocity * 0.2f);
                _camera.Zoom = 1.0f - _player.Velocity.Length() / 500f;
            }

            _entityManager.Update(gameTime);

            CheckCollisions();

            _previousMouseState = mouseState;

            base.Update(gameTime);
        }
コード例 #6
0
        protected override void Update(GameTime gameTime)
        {
            var elapsedSeconds = gameTime.GetElapsedSeconds();
            var keyboardState  = Keyboard.GetState();
            var direction      = Vector2.Zero;

            if (keyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A))
            {
                direction.X          -= 1;
                _player.Sprite.Effect = SpriteEffects.FlipHorizontally;
                _player.Sprite.Play("walk");
            }

            if (keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D))
            {
                direction.X          += 1;
                _player.Sprite.Effect = SpriteEffects.None;
                _player.Sprite.Play("walk");
            }

            if (keyboardState.IsKeyDown(Keys.Up) || keyboardState.IsKeyDown(Keys.W))
            {
                direction.Y -= 1;
            }

            if (keyboardState.IsKeyDown(Keys.Down) || keyboardState.IsKeyDown(Keys.S))
            {
                direction.Y += 1;
            }

            if (keyboardState.IsKeyDown(Keys.R))
            {
                _camera.ZoomIn(elapsedSeconds);
            }

            if (keyboardState.IsKeyDown(Keys.F))
            {
                _camera.ZoomOut(elapsedSeconds);
            }


            _player.Position += direction * _player.Speed * elapsedSeconds;

            var lookAtPosition = Vector2.Lerp(_camera.Position + _camera.Origin, _player.Position, 0.05f);

            _camera.LookAt(lookAtPosition);

            _player.Update(elapsedSeconds);

            base.Update(gameTime);
        }
コード例 #7
0
        public override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _backgroundTexture = Content.Load <Texture2D>("checkered-dark");
            _spriteFont        = Content.Load <SpriteFont>("default");

            Camera = new OrthographicCamera(GraphicsDevice);
            Camera.LookAt(Vector2.Zero);
        }
コード例 #8
0
        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            _bitmapFont  = Content.Load <BitmapFont>("Fonts/montserrat-32");

            _availableMaps = new Queue <string>(new[] { "level01", "level02", "level03", "level04", "level05", "level06", "level07", "level08" });

            _map = LoadNextMap();
            _camera.LookAt(new Vector2(_map.WidthInPixels, _map.HeightInPixels) * 0.5f);
            _camera.Position = new Vector2(-104, -92);

            var effect = new CustomEffect(GraphicsDevice)
            {
                Alpha              = 0.5f,
                TextureEnabled     = true,
                VertexColorEnabled = false
            };

            _customEffect = effect;
        }
コード例 #9
0
 protected override void Draw(GameTime gameTime)
 {
     GraphicsDevice.Clear(Color.CornflowerBlue);
     _camera.LookAt(_playerPosition);
     // TODO: Add your drawing code here
     _spriteBatch.Begin(transformMatrix: _camera.GetViewMatrix(), samplerState: SamplerState.PointClamp);
     _tiledMapRenderer.Draw(_camera.GetViewMatrix());
     _spriteBatch.Draw(_playerTexture, _playerPosition, Color.White);
     _spriteBatch.End();
     base.Draw(gameTime);
 }
コード例 #10
0
        private void HandleViewportSizeChange()
        {
            Size2 graphicsDeviceSize = new Size2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);

            if (viewportSize != graphicsDeviceSize)
            {
                Vector2 cameraCenter = camera.Center;
                camera.Origin = new Vector2(graphicsDeviceSize.Width / 2, graphicsDeviceSize.Height / 2);
                camera.LookAt(cameraCenter);

                viewportSize = graphicsDeviceSize;
            }
        }
コード例 #11
0
        public override void Process(GameTime gameTime, int entityId)
        {
            foreach (var entity in ActiveEntities)
            {
                var focus     = focusableMapper.Get(entity);
                var transform = transformMapper.Get(entity);

                //There can only be one object focused
                if (focus.IsFocused)
                {
                    orthographicCamera.LookAt(new Vector2(transform.Position.X, transform.Position.Y / 2));
                }
            }
        }
コード例 #12
0
        protected override void Update(GameTime gameTime)
        {
            _tiledMapRenderer.Update(gameTime);

            DualStick.Update(gameTime);
            var relativePostion = new
            {
                Left  = DualStick.LeftStick.GetRelativeVector(DualStick.aliveZoneSize),
                Right = DualStick.RightStick.GetRelativeVector(DualStick.aliveZoneSize)
            };

            Ball.Move(relativePostion.Left, gameTime);
            _camera.LookAt(Ball.Position + relativePostion.Right);

            base.Update(gameTime);
        }
コード例 #13
0
        public override void Initialize()
        {
            Console.WriteLine("Initializing!");

            _imGuiRenderer = new ImGuiRenderer(Game);
            _imGuiRenderer.RebuildFontAtlas();

            _camera = new OrthographicCamera(GraphicsDevice);
            _camera.LookAt(new Vector2(GraphicsDevice.Viewport.Width / 2.0f, GraphicsDevice.Viewport.Height / 2.0f));
            _camera.Zoom = 0.5f;

            _playerTurnsContainer = new PlayerTurnsContainer(_field, _gameStateManager);
            SetUpCommands();

            base.Initialize();
        }
コード例 #14
0
        /// <summary>
        /// HandleViewportSizeChange()
        /// Handles recentering the Camera and resizing the MonoGame
        /// viewport when the window size is changed.
        /// </summary>
        private void HandleViewportSizeChange()
        {
            // Get current size of the window.
            Size2 graphicsDeviceSize = new Size2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);

            // if window size has changed recenter camera
            if (viewportSize != graphicsDeviceSize)
            {
                Vector2 cameraCenter = camera.Center; // Get camera center
                // Set camera origin to center of graphics device
                camera.Origin = new Vector2(graphicsDeviceSize.Width / 2, graphicsDeviceSize.Height / 2);
                camera.LookAt(cameraCenter); // Refocus camera on new center

                // Set the MonoGame viewport to the graphics device's size.
                viewportSize = graphicsDeviceSize;
            }
        }
コード例 #15
0
ファイル: Game1.cs プロジェクト: a17fellu/ShootThem
        protected override void Initialize()
        {
            // TODO: Add your initialization logic
            _graphics.PreferredBackBufferWidth  = 1280;
            _graphics.PreferredBackBufferHeight = 720;
            _graphics.ApplyChanges();

            map          = Content.Load <TiledMap>("test");
            _mapRenderer = new TiledMapRenderer(GraphicsDevice, map);

            var viewportadapter = new BoxingViewportAdapter(Window, GraphicsDevice, 400, 400);

            camera = new OrthographicCamera(viewportadapter);
            camera.LookAt(new Vector2(0, 0));
            _cameraMovement = new CameraMovement();

            base.Initialize();
        }
コード例 #16
0
        protected override void Update(GameTime gameTime)
        {
            var keyStates = Keyboard.GetState();

            if (keyStates.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            var movementDirection = Vector2.Zero;

            if (keyStates.IsKeyDown(Keys.S))
            {
                movementDirection += Vector2.UnitY;
            }
            if (keyStates.IsKeyDown(Keys.W))
            {
                movementDirection -= Vector2.UnitY;
            }
            if (keyStates.IsKeyDown(Keys.A))
            {
                movementDirection -= Vector2.UnitX;
            }
            if (keyStates.IsKeyDown(Keys.D))
            {
                movementDirection += Vector2.UnitX;
            }
            // Can't normalize the zero vector so test for it before normalizing
            if (movementDirection != Vector2.Zero)
            {
                movementDirection.Normalize();
                camPos += 200f * movementDirection * gameTime.GetElapsedSeconds();
            }

            // TODO: Add your update logic here

            viewportAdapter = new ScalingViewportAdapter(GraphicsDevice, Window.ClientBounds.Width, Window.ClientBounds.Height);
            cam             = new OrthographicCamera(viewportAdapter);

            cam.LookAt(camPos);

            mapRenderer.Update(gameTime);
            base.Update(gameTime);
        }
コード例 #17
0
        /// <summary>
        ///
        /// </summary>
        public override void Render()
        {
            theta += 0.1f;

            camera.Position.X = radius * (float)System.Math.Sin(Mat.DegToRad(theta));
            camera.Position.Y = radius * (float)System.Math.Sin(Mat.DegToRad(theta));
            camera.Position.Z = radius * (float)System.Math.Cos(Mat.DegToRad(theta));
            camera.LookAt(scene.Position);

            // find intersections
            var vector    = new Vector3(mouse.X, mouse.Y, -1).Unproject(camera);
            var direction = new Vector3(0, 0, -1).TransformDirection(camera.MatrixWorld);

            raycaster = new Raycaster(vector, direction);

            var intersects = raycaster.IntersectObjects(scene.Children);

            if (intersects.Count > 0)
            {
                if (INTERSECTED != intersects[0].Object3D)
                {
                    if (INTERSECTED != null)
                    {
                        ((MeshLambertMaterial)INTERSECTED.Material).Emissive = currentHex;
                    }

                    INTERSECTED = intersects[0].Object3D;
                    currentHex  = ((MeshLambertMaterial)INTERSECTED.Material).Emissive;
                    ((MeshLambertMaterial)INTERSECTED.Material).Emissive = Color.Red;
                }
            }
            else
            {
                if (INTERSECTED != null)
                {
                    ((MeshLambertMaterial)INTERSECTED.Material).Emissive = currentHex;
                }
                INTERSECTED = null;
            }

            renderer.Render(scene, camera);
        }
コード例 #18
0
ファイル: Game1.cs プロジェクト: educrod/rpg
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            if (player.Health > 0)
            {
                player.Update(gameTime, myMap.WidthInPixels, myMap.HeightInPixels);
            }

            float tempX = player.Position.X;
            float tempY = player.Position.Y;
            int   camW  = _graphics.PreferredBackBufferWidth;
            int   camH  = _graphics.PreferredBackBufferHeight;

            int mapW = myMap.WidthInPixels;
            int mapH = myMap.HeightInPixels;

            if (tempX < camW / 2)
            {
                tempX = camW / 2;
            }

            if (tempY < camH / 2)
            {
                tempY = camH / 2;
            }

            if (tempX > (mapW - (camW / 2)))
            {
                tempX = (mapW - (camW / 2));
            }
            if (tempY > (mapH - (camH / 2)))
            {
                tempY = (mapH - (camH / 2));
            }

            cam.LookAt(new Vector2(tempX, tempY));

            foreach (Projectile proj in Projectile.projectiles)
            {
                proj.Update(gameTime);
            }
            foreach (Enemy en in Enemy.enemies)
            {
                en.Update(gameTime, player.Position);
            }
            foreach (Projectile proj in Projectile.projectiles)
            {
                foreach (Enemy en in Enemy.enemies)
                {
                    int sum = proj.Radius + en.Radius;
                    if (Vector2.Distance(proj.Position, en.Position) < sum)
                    {
                        proj.Collided = true;
                        en.Health--;
                    }
                }
                if (Obstacle.didCollide(proj.Position, proj.Radius))
                {
                    proj.Collided = true;
                }
            }
            foreach (Enemy en in Enemy.enemies)
            {
                int sum = player.Radius + en.Radius;
                if (Vector2.Distance(player.Position, en.Position) < sum && player.HealthTimer <= 0)
                {
                    player.Health--;
                    player.HealthTimer = 1.5f;
                }
            }
            Projectile.projectiles.RemoveAll(p => p.Collided);
            Enemy.enemies.RemoveAll(e => e.Health <= 0);

            mapRenderer.Update(gameTime);
            base.Update(gameTime);
        }
コード例 #19
0
        private void Move(float deltaSeconds)
        {
            KeyboardState kstate = Keyboard.GetState();

            _movement = new Vector2(0f, 0f);

            if (kstate.IsKeyDown(Keys.W))
            {
                _movement.Y -= 1f;
            }
            if (kstate.IsKeyDown(Keys.S))
            {
                _movement.Y += 1f;
            }
            if (kstate.IsKeyDown(Keys.A))
            {
                _movement.X -= 1f;
            }
            if (kstate.IsKeyDown(Keys.D))
            {
                _movement.X += 1f;
            }

            if (_movement.Length() > 0f)
            {
                _movement.Normalize();
            }

            // If Gravity is On then player should move as normal;
            // Else velocity should be cumulative to give a 0G feel.
            if (_gravityStatus)
            {
                _velocity = new Vector2(0f, 0f);

                if (_movement.X != 0f)
                {
                    _velocity.X += _movement.X * _movementSpeed * deltaSeconds;
                }
                if (_movement.Y != 0f)
                {
                    _velocity.Y += _movement.Y * _movementSpeed * deltaSeconds;
                }
            }

            else
            {
                if (_movement.X != 0f)
                {
                    _velocity.X += _movement.X * _noGravityMovementSpeed * deltaSeconds;
                }
                if (_movement.Y != 0f)
                {
                    _velocity.Y += _movement.Y * _noGravityMovementSpeed * deltaSeconds;
                }
            }

            // Moves player, collision box and Progress Bar
            _position       += _velocity;
            Bounds.Position += _velocity;
            Vector2 progressBarPos = _position;

            progressBarPos.Y -= _sprite.TextureRegion.Height;
            _fixProgress.SetPosition(progressBarPos);

            UpdateAnimation();

            _sprite.Play(_currentAnimation);
            _sprite.Update(deltaSeconds);

            // Updates 'OrthographicCamera' position to players current position.
            _camera.LookAt(_position);
        }
コード例 #20
0
        static void Main(string[] args)
        {
            var mediaPath    = Path.GetFullPath("../../../../../js/r68/examples/");
            var texturesPath = Path.Combine(mediaPath, "textures");

            var renderer = new Renderer();

            var scene = new Scene()
            {
                //Fog = new FogExp2(Color.Blue, 0.24f)
            };

            var camera = new OrthographicCamera(renderer, -1000, 1000)
            {
                Position = new Vector3(0, 0, 2)
            };

            //// create a point light
            scene.Add(new DirectionalLight(Color.White)
            {
                Target = Vector3.UnitX
            });


            var geometry = new BoxGeometry(20, 20, 20);

            for (var i = 0; i < 2000; i++)
            {
                var o = new Mesh(geometry, new MeshLambertMaterial(renderer)
                {
                    Diffuse = Color.Random()
                });
                o.Position = new Vector3(Mathf.RandomF(-400, 400), Mathf.RandomF(-400, 400), Mathf.RandomF(-400, 400));
                o.Rotation = new Euler(Mathf.Tau * Mathf.RandomF(), Mathf.Tau * Mathf.RandomF(), Mathf.Tau * Mathf.RandomF());
                o.Scale    = new Vector3(Mathf.RandomF(0.5f, 1.5f), Mathf.RandomF(0.5f, 1.5f), Mathf.RandomF(0.5f, 1.5f));
                scene.Add(o);
            }

            var      raycaster     = new Raycaster();
            Object3D INTERSECTED   = null;
            Color    previousColor = Color.White;

            var radius       = 100;
            var previousTime = 0f;
            var stopwatch    = Stopwatch.StartNew();

            while (!renderer.Done)
            {
                var now       = (float)stopwatch.Elapsed.TotalSeconds;
                var deltaTime = now - previousTime;
                previousTime = now;

                var offset = now / 4;
                var sin    = Mathf.Sin(offset) * radius;
                var cos    = Mathf.Cos(offset) * radius;
                camera.Position = new Vector3(sin, sin, cos);
                camera.LookAt(Vector3.Zero);

                #region FindIntersections
                var vector    = Projector.UnprojectVector(new Vector3(renderer.MousePositionNormalized, -1), camera.projectionMatrix, camera.matrixWorld);
                var direction = new Vector3(0, 0, -1);
                direction.TransformDirection(camera.matrixWorld);

                raycaster.Set(vector, direction);

                var intersects = raycaster.IntersectObjects(scene.Children);

                if (intersects != null && intersects.Count > 0)
                {
                    var first = intersects[0];

                    if (INTERSECTED != first.Object)
                    {
                        if (INTERSECTED != null)
                        {
                            var basic = INTERSECTED.Material as MeshLambertMaterial;
                            basic.Emissive = previousColor;
                        }

                        var firstMat = first.Object.Material as MeshLambertMaterial;

                        INTERSECTED       = first.Object;
                        previousColor     = firstMat.Emissive;
                        firstMat.Emissive = Color.Red;
                    }
                }
                else
                {
                    if (INTERSECTED != null)
                    {
                        (INTERSECTED.Material as MeshLambertMaterial).Emissive = previousColor;
                    }

                    INTERSECTED = null;
                }
                #endregion

                renderer.RenderFrame(scene, camera);
            }
        }