public void SetEntity(Entity entity) { CurrentEntity = entity; if (CurrentEntity == null) { CurrentEntityHead = null; } else { // Map other Components CurrentController = entity.Components.GetComponent <ControllableComponent>(); CurrentEntityHead = CurrentEntity.Components.GetComponent <HeadComponent>(); if (CurrentEntityHead == null) { CurrentEntityHead = new HeadComponent(); } Inventory = CurrentEntity.Components.GetComponent <InventoryComponent>(); if (Inventory == null) { Inventory = new InventoryComponent(); } Toolbar = CurrentEntity.Components.GetComponent <ToolBarComponent>(); if (Toolbar == null) { Toolbar = new ToolBarComponent(); } Position = CurrentEntity.Components.GetComponent <PositionComponent>(); if (Position == null) { Position = new PositionComponent() { Position = new Coordinate(0, new Index3(0, 0, 0), new Vector3(0, 0, 0)) } } ; } }
public void Draw(Matrix view, Matrix projection, Index3 chunkOffset, Index2 planetSize) { effect.Projection = projection; effect.View = view; effect.TextureEnabled = true; graphicsDevice.RasterizerState = RasterizerState.CullClockwise; using (var writer = File.AppendText(Path.Combine(".", "render.log"))) foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); i++; foreach (var entity in Entities) { if (!entity.Components.ContainsComponent <RenderComponent>()) { continue; } var rendercomp = entity.Components.GetComponent <RenderComponent>(); if (!models.TryGetValue(rendercomp.Name, out ModelInfo modelinfo)) { modelinfo = new ModelInfo() { render = true, model = Game.Content.Load <Model>(rendercomp.ModelName), texture = Game.Content.Load <Texture2D>(rendercomp.TextureName), }; } if (!modelinfo.render) { continue; } var positioncomp = entity.Components.GetComponent <PositionComponent>(); var position = positioncomp.Position; var body = entity.Components.GetComponent <BodyComponent>(); HeadComponent head = new HeadComponent(); if (entity.Components.ContainsComponent <HeadComponent>()) { head = entity.Components.GetComponent <HeadComponent>(); } Index3 shift = chunkOffset.ShortestDistanceXY( position.ChunkIndex, planetSize); var rotation = MathHelper.WrapAngle(positioncomp.Direction + MathHelper.ToRadians(rendercomp.BaseZRotation)); Matrix world = Matrix.CreateTranslation( shift.X * Chunk.CHUNKSIZE_X + position.LocalPosition.X, shift.Y * Chunk.CHUNKSIZE_Y + position.LocalPosition.Y, shift.Z * Chunk.CHUNKSIZE_Z + position.LocalPosition.Z) * Matrix.CreateScaling(body.Radius * 2, body.Radius * 2, body.Height) * Matrix.CreateRotationZ(rotation); effect.World = world; modelinfo.model.Transform = world; modelinfo.model.Draw(effect, modelinfo.texture); } } }
public override void Update(GameTime gameTime) { if (!Enabled) { return; } if (player == null || player.CurrentEntity == null) { return; } Entity entity = player.CurrentEntity; HeadComponent head = player.CurrentEntityHead; PositionComponent position = player.Position; CameraChunk = position.Position.ChunkIndex; CameraPosition = position.Position.LocalPosition + head.Offset; CameraUpVector = new Vector3(0, 0, 1f); float height = (float)Math.Sin(head.Tilt); float distance = (float)Math.Cos(head.Tilt); float lookX = (float)Math.Cos(head.Angle) * distance; float lookY = -(float)Math.Sin(head.Angle) * distance; float strafeX = (float)Math.Cos(head.Angle + MathHelper.PiOver2); float strafeY = -(float)Math.Sin(head.Angle + MathHelper.PiOver2); CameraUpVector = Vector3.Cross(new Vector3(strafeX, strafeY, 0), new Vector3(lookX, lookY, height)); View = Matrix.CreateLookAt( CameraPosition, new Vector3( CameraPosition.X + lookX, CameraPosition.Y + lookY, CameraPosition.Z + height), CameraUpVector); MinimapView = Matrix.CreateLookAt( new Vector3(CameraPosition.X, CameraPosition.Y, 100), new Vector3( position.Position.LocalPosition.X, position.Position.LocalPosition.Y, 0f), new Vector3( (float)Math.Cos(head.Angle), (float)Math.Sin(-head.Angle), 0f)); float centerX = GraphicsDevice.Viewport.Width / 2; float centerY = GraphicsDevice.Viewport.Height / 2; Vector3 nearPoint = GraphicsDevice.Viewport.Unproject(new Vector3(centerX, centerY, 0f), Projection, View, Matrix.Identity); Vector3 farPoint = GraphicsDevice.Viewport.Unproject(new Vector3(centerX, centerY, 1f), Projection, View, Matrix.Identity); Vector3 direction = farPoint - nearPoint; direction.Normalize(); PickRay = new Ray(nearPoint, direction); Frustum = new BoundingFrustum(Projection * View); }