public void DrawBefore2D(GraphicsDevice gd, WorldState state) { foreach (var avatar in Blueprint.Avatars) { avatar.Draw(gd, state); } }
public void DrawAfter2D(GraphicsDevice gd, WorldState state) { gd.RenderState.CullMode = CullMode.CullCounterClockwiseFace; foreach (var avatar in Blueprint.Avatars){ avatar.Draw(gd, state); } }
public void DrawBefore2D(GraphicsDevice gd, WorldState state) { foreach (var avatar in Blueprint.Avatars) { if ((avatar.Position.Z+0.05f)/2.95f < state.Level) avatar.Draw(gd, state); } }
/// <summary> /// Setup anything that needs a GraphicsDevice /// </summary> /// <param name="layer"></param> public override void Initialize(_3DLayer layer) { base.Initialize(layer); /** * Setup world state, this object acts as a facade * to world objects as well as providing various * state settings for the world and helper functions */ State = new WorldState(layer.Device, layer.Device.Viewport.Width, layer.Device.Viewport.Height, this); State._3D = new tso.world.utils._3DWorldBatch(State); State._2D = new tso.world.utils._2DWorldBatch(layer.Device, World2D.NUM_2D_BUFFERS, World2D.BUFFER_SURFACE_FORMATS); base.Camera = State.Camera; HasInitGPU = true; HasInit = HasInitGPU & HasInitBlueprint; _2DWorld.Initialize(layer); }
public void PreDraw(GraphicsDevice gd, WorldState state) { }
public void DrawBefore2D(GraphicsDevice gd, WorldState state) { }
/// <summary> /// Paint to screen /// </summary> /// <param name="gd"></param> /// <param name="state"></param> public void Draw(GraphicsDevice gd, WorldState state) { var _2d = state._2D; /** * Draw static layers */ _2d.OffsetPixel(Vector2.Zero); if (StaticTerrain != null){ _2d.DrawBasic(StaticTerrain, Vector2.Zero); } if (StaticFloor != null){ _2d.DrawBasic(StaticFloor, Vector2.Zero); } if (StaticObjects != null && StaticObjectsDepth != null) { _2d.DrawBasicRestoreDepth(StaticObjects, StaticObjectsDepth, Vector2.Zero); } /** * Draw dynamic objects. If an object has been static for X frames move it back into the static layer */ var occupiedTiles = Blueprint.GetOccupiedTiles(state.Rotation); var pxOffset = state.WorldSpace.GetScreenOffset(); foreach (var tile in occupiedTiles) { var tilePosition = new Vector3(tile.TileX, tile.TileY, 0.0f); _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); /** Objects **/ if ((tile.Type & BlueprintOccupiedTileType.OBJECT) == BlueprintOccupiedTileType.OBJECT) { var objects = Blueprint.GetObjects(tile.TileX, tile.TileY); foreach (var obj in objects.Objects) { var renderInfo = GetRenderInfo(obj); if (renderInfo.Layer == WorldObjectRenderLayer.DYNAMIC) { obj.Draw(gd, state); } } } } /** Center point **/ if (DrawCenterPoint) { _2d.OffsetPixel(Vector2.Zero); _2d.DrawBasic(CenterPixel, new Vector2((gd.Viewport.Width / 2.0f)-2, (gd.Viewport.Height / 2.0f)-2)); } //var pxOffset = new Vector2(512.0f, -512.0f); //var _2d = state._2D; //foreach (var tile in Blueprint.GetOccupiedTiles(state.Rotation)){ // var tilePosition = new Vector3(tile.TileX, tile.TileY, 0.0f); // _2d.OffsetPixel(state.WorldSpace.GetScreenFromTileWithoutScroll(tilePosition) + pxOffset); // _2d.OffsetTile(tilePosition); // /** Do i have a texture for this tile? **/ // var tileInfo = this.TileInfo[(tile.TileY * Blueprint.Width) + tile.TileX]; // if (tileInfo.Pixel != null){ // _2d.DrawBasic(tileInfo.Pixel, Vector2.Zero); // } //} }
/// <summary> /// Gets an object's ID given an object's screen position. /// </summary> /// <param name="x">The object's X position.</param> /// <param name="y">The object's Y position.</param> /// <param name="gd">GraphicsDevice instance.</param> /// <param name="state">WorldState instance.</param> /// <returns>Object's ID if the object was found at the given position.</returns> public short GetObjectIDAtScreenPos(int x, int y, GraphicsDevice gd, WorldState state) { /** Draw all objects to a texture as their IDs **/ var occupiedTiles = Blueprint.GetOccupiedTiles(state.Rotation); var pxOffset = state.WorldSpace.GetScreenOffset(); pxOffset.X -= x; pxOffset.Y -= y; var _2d = state._2D; Promise<Texture2D> bufferTexture = null; state._2D.OBJIDMode = true; using (var buffer = state._2D.WithBuffer(BUFFER_OBJID, ref bufferTexture)) { while (buffer.NextPass()) { foreach (var tile in occupiedTiles) { /** Objects **/ if ((tile.Type & BlueprintOccupiedTileType.OBJECT) == BlueprintOccupiedTileType.OBJECT) { var objects = Blueprint.GetObjects(tile.TileX, tile.TileY); foreach (var obj in objects.Objects) { var tilePosition = obj.Position; _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); _2d.SetObjID(obj.ObjectID); obj.Draw(gd, state); } } } } } state._2D.OBJIDMode = false; var tex = bufferTexture.Get(); Single[] data = new float[1]; tex.GetData<Single>(data); return (short)Math.Round(data[0]*65535f); }
/// <summary> /// Gets an object's ID given an object's screen position. /// </summary> /// <param name="x">The object's X position.</param> /// <param name="y">The object's Y position.</param> /// <param name="gd">GraphicsDevice instance.</param> /// <param name="state">WorldState instance.</param> /// <returns>Object's ID if the object was found at the given position.</returns> public short GetObjectIDAtScreenPos(int x, int y, GraphicsDevice gd, WorldState state) { /** Draw all objects to a texture as their IDs **/ var occupiedTiles = Blueprint.GetOccupiedTiles(state.Rotation); var oldCenter = state.CenterTile; var tileOff = state.WorldSpace.GetTileFromScreen(new Vector2(x, y)); state.CenterTile += tileOff; var pxOffset = state.WorldSpace.GetScreenOffset(); var _2d = state._2D; Promise<Texture2D> bufferTexture = null; state.TempDraw = true; state._2D.OBJIDMode = true; state._3D.OBJIDMode = true; using (var buffer = state._2D.WithBuffer(BUFFER_OBJID, ref bufferTexture)) { while (buffer.NextPass()) { foreach (var tile in occupiedTiles) { /** Objects **/ if ((tile.Type & BlueprintOccupiedTileType.OBJECT) == BlueprintOccupiedTileType.OBJECT) { var objects = Blueprint.GetObjects(tile.TileX, tile.TileY); //TODO: Level foreach (var obj in objects.Objects) { if (obj.Level > state.Level) continue; var tilePosition = obj.Position; _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); _2d.SetObjID(obj.ObjectID); obj.Draw(gd, state); } } } state._3D.Begin(gd); foreach (var avatar in Blueprint.Avatars) { state._3D.SetObjID((short)avatar.ObjectID); avatar.Draw(gd, state); } state._3D.End(); } } state._3D.OBJIDMode = false; state._2D.OBJIDMode = false; state.TempDraw = false; state.CenterTile = oldCenter; var tex = bufferTexture.Get(); Single[] data = new float[1]; tex.GetData<Single>(data); return (short)Math.Round(data[0]*65535f); }
public virtual void OnZoomChanged(WorldState world) { OnWorldChanged(world); }
public virtual void OnWorldChanged(WorldState world) { }
public virtual void OnScrollChanged(WorldState world) { OnWorldChanged(world); }
public virtual void OnRotationChanged(WorldState world) { OnWorldChanged(world); }
public virtual void Initialize(GraphicsDevice device, WorldState world) { OnWorldChanged(world); }
public abstract void Draw(GraphicsDevice device, WorldState world);
/// <summary> /// Creates a new WorldSpace instance. /// </summary> /// <param name="worldPxWidth">Width of world in pixels.</param> /// <param name="worldPxHeight">Height of world in pixels.</param> /// <param name="state">WorldState instance.</param> public WorldSpace(float worldPxWidth, float worldPxHeight, WorldState state) { this.State = state; this.WorldPxWidth = worldPxWidth; this.WorldPxHeight = worldPxHeight; }
/// <summary> /// Paint to screen /// </summary> /// <param name="gd"></param> /// <param name="state"></param> public void Draw(GraphicsDevice gd, WorldState state) { var _2d = state._2D; /** * Draw static layers */ _2d.OffsetPixel(Vector2.Zero); if (StaticTerrain != null){ _2d.DrawBasic(StaticTerrain, Vector2.Zero); } if (StaticFloor != null){ _2d.DrawBasic(StaticFloor, Vector2.Zero); } if (StaticArch != null && StaticArchDepth != null) { _2d.DrawBasicRestoreDepth(StaticArch, StaticArchDepth, Vector2.Zero); } if (StaticObjects != null && StaticObjectsDepth != null) { _2d.DrawBasicRestoreDepth(StaticObjects, StaticObjectsDepth, Vector2.Zero); } /** * Draw dynamic objects. If an object has been static for X frames move it back into the static layer */ var occupiedTiles = Blueprint.GetOccupiedTiles(state.Rotation); var pxOffset = state.WorldSpace.GetScreenOffset(); foreach (var tile in occupiedTiles) { /** Objects **/ if ((tile.Type & BlueprintOccupiedTileType.OBJECT) == BlueprintOccupiedTileType.OBJECT) { var objects = Blueprint.GetObjects(tile.TileX, tile.TileY); foreach (var obj in objects.Objects) { var renderInfo = GetRenderInfo(obj); if (renderInfo.Layer == WorldObjectRenderLayer.DYNAMIC) { var tilePosition = obj.Position; _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); _2d.SetObjID(obj.ObjectID); obj.Draw(gd, state); } } } } }
/// <summary> /// Gets an object group's thumbnail provided an array of objects. /// </summary> /// <param name="objects">The object components to draw.</param> /// <param name="gd">GraphicsDevice instance.</param> /// <param name="state">WorldState instance.</param> /// <returns>Object's ID if the object was found at the given position.</returns> public Texture2D GetObjectThumb(ObjectComponent[] objects, Vector3[] positions, GraphicsDevice gd, WorldState state) { var oldZoom = state.Zoom; var oldRotation = state.Rotation; /** Center average position **/ Vector3 average = new Vector3(); for (int i = 0; i < positions.Length; i++) { average += positions[i]; } average /= positions.Length; state.SilentZoom = WorldZoom.Near; state.SilentRotation = WorldRotation.BottomRight; state.WorldSpace.Invalidate(); state.InvalidateCamera(); state.TempDraw = true; var pxOffset = new Vector2(442, 275) - state.WorldSpace.GetScreenFromTile(average); var _2d = state._2D; Promise<Texture2D> bufferTexture = null; state._2D.OBJIDMode = false; Rectangle bounds = new Rectangle(); using (var buffer = state._2D.WithBuffer(BUFFER_THUMB, ref bufferTexture)) { while (buffer.NextPass()) { for (int i=0; i<objects.Length; i++) { var obj = objects[i]; var tilePosition = positions[i]; //we need to trick the object into believing it is in a set world state. var oldObjRot = obj.Direction; obj.Direction = Direction.NORTH; state.SilentZoom = WorldZoom.Near; state.SilentRotation = WorldRotation.BottomRight; obj.OnRotationChanged(state); obj.OnZoomChanged(state); _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); _2d.SetObjID(obj.ObjectID); obj.Draw(gd, state); //return everything to normal obj.Direction = oldObjRot; state.SilentZoom = oldZoom; state.SilentRotation = oldRotation; obj.OnRotationChanged(state); obj.OnZoomChanged(state); } bounds = _2d.GetSpriteListBounds(); } } bounds.X = Math.Max(0, Math.Min(1023, bounds.X)); bounds.Y = Math.Max(0, Math.Min(1023, bounds.Y)); if (bounds.Width + bounds.X > 1024) bounds.Width = 1024 - bounds.X; if (bounds.Height + bounds.Y > 1024) bounds.Height = 1024 - bounds.Y; //return things to normal state.WorldSpace.Invalidate(); state.InvalidateCamera(); state.TempDraw = false; var tex = bufferTexture.Get(); return TextureUtils.Clip(gd, tex, bounds); }
/// <summary> /// Prep work before screen is painted /// </summary> /// <param name="gd"></param> /// <param name="state"></param> public void PreDraw(GraphicsDevice gd, WorldState state) { var damage = Blueprint.Damage; var _2d = state._2D; /** * Tasks: * If scroll, zoom or rotation has changed, redraw all static layers * If architecture has changed, redraw appropriate static layer * If there is a new object in the static layer, redraw the static layer * If an objects in the static layer has changed, redraw the static layer and move the object to the dynamic layer * If wall visibility has changed, redraw wall layer (should think about how this works with breakthrough wall mode */ var redrawTerrain = StaticTerrain == null; var redrawStaticObjects = false; var redrawFloors = false; var redrawWalls = false; WorldObjectRenderInfo info = null; foreach (var item in damage){ switch (item.Type){ case BlueprintDamageType.ROTATE: case BlueprintDamageType.ZOOM: case BlueprintDamageType.SCROLL: redrawFloors = true; redrawWalls = true; redrawStaticObjects = true; redrawTerrain = true; break; case BlueprintDamageType.OBJECT_MOVE: /** Redraw if its in static layer **/ info = GetRenderInfo(item.Component); if (info.Layer == WorldObjectRenderLayer.STATIC){ redrawStaticObjects = true; } break; case BlueprintDamageType.OBJECT_GRAPHIC_CHANGE: /** Redraw if its in static layer **/ info = GetRenderInfo(item.Component); if (info.Layer == WorldObjectRenderLayer.STATIC){ redrawStaticObjects = true; info.Layer = WorldObjectRenderLayer.DYNAMIC; } break; case BlueprintDamageType.OBJECT_RETURN_TO_STATIC: info = GetRenderInfo(item.Component); if (info.Layer == WorldObjectRenderLayer.DYNAMIC) { redrawStaticObjects = true; info.Layer = WorldObjectRenderLayer.STATIC; } break; case BlueprintDamageType.WALL_CUT_CHANGED: redrawWalls = true; break; case BlueprintDamageType.FLOOR_CHANGED: redrawFloors = true; break; case BlueprintDamageType.WALL_CHANGED: redrawWalls = true; break; } } damage.Clear(); var occupiedTiles = Blueprint.GetOccupiedTiles(state.Rotation); var pxOffset = state.WorldSpace.GetScreenOffset(); if (redrawTerrain) { Promise<Texture2D> bufferTexture = null; using (var buffer = state._2D.WithBuffer(BUFFER_STATIC_TERRAIN, ref bufferTexture)){ while (buffer.NextPass()){ Blueprint.Terrain.Draw(gd, state); } } StaticTerrain = bufferTexture.Get(); } if (redrawFloors){ Promise<Texture2D> bufferTexture = null; using (var buffer = state._2D.WithBuffer(BUFFER_STATIC_FLOOR, ref bufferTexture)) { while (buffer.NextPass()) { foreach (var tile in occupiedTiles) { var tilePosition = new Vector3(tile.TileX, tile.TileY, 0.0f); _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); _2d.SetObjID(0); if ((tile.Type & BlueprintOccupiedTileType.FLOOR) == BlueprintOccupiedTileType.FLOOR) { var floor = Blueprint.GetFloor(tile.TileX, tile.TileY); floor.Draw(gd, state); } } } } StaticFloor = bufferTexture.Get(); //StaticFloor.Save("C:\\floor.png", ImageFileFormat.Png); } if (redrawWalls) { /** Draw archetecture to a texture **/ Promise<Texture2D> bufferTexture = null; Promise<Texture2D> depthTexture = null; using (var buffer = state._2D.WithBuffer(BUFFER_ARCHETECTURE_PIXEL, ref bufferTexture, BUFFER_ARCHETECTURE_DEPTH, ref depthTexture)) { while (buffer.NextPass()) { Blueprint.WallComp.Draw(gd, state); } } StaticArch = bufferTexture.Get(); StaticArchDepth = depthTexture.Get(); } if (redrawStaticObjects){ /** Draw static objects to a texture **/ Promise<Texture2D> bufferTexture = null; Promise<Texture2D> depthTexture = null; using (var buffer = state._2D.WithBuffer(BUFFER_STATIC_OBJECTS_PIXEL, ref bufferTexture, BUFFER_STATIC_OBJECTS_DEPTH, ref depthTexture)) { while (buffer.NextPass()) { foreach (var tile in occupiedTiles) { /** Objects **/ if ((tile.Type & BlueprintOccupiedTileType.OBJECT) == BlueprintOccupiedTileType.OBJECT) { var objects = Blueprint.GetObjects(tile.TileX, tile.TileY); foreach (var obj in objects.Objects) { var renderInfo = GetRenderInfo(obj); if (renderInfo.Layer == WorldObjectRenderLayer.STATIC) { var tilePosition = obj.Position; _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); _2d.SetObjID(obj.ObjectID); obj.Draw(gd, state); } } } } } } StaticObjects = bufferTexture.Get(); StaticObjectsDepth = depthTexture.Get(); //StaticObjects.Save("C:\\static.png", ImageFileFormat.Png); } }
public void DrawAfter2D(GraphicsDevice gd, WorldState state) { //gd.RasterizerState.CullMode = CullMode.CullCounterClockwiseFace; }