Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Items/Ammo/Rewards Refactored and added to Conscripts #187

Merged
merged 2 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ZeldaOracle/ConscriptDesigner/Control/DesignerControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using ZeldaOracle.Common.Scripts;
using ZeldaOracle.Common.Util;
using ZeldaOracle.Game;
using ZeldaOracle.Game.Items;
using ZeldaOracle.Game.Items.Rewards;
using ZeldaOracle.Game.Tiles;
using ZeldaOracle.Game.Worlds;
Expand Down Expand Up @@ -424,7 +425,8 @@ private static ScriptReaderException RunConscriptsTask() {
UpdateContentFolder(project);

Resources.Initialize(spriteBatch, graphicsDevice, contentManager);
rewardManager = new RewardManager(null);
Inventory inventory = new Inventory();
rewardManager = new RewardManager(inventory);
GameData.Initialize(false, rewardManager);

//Console.WriteLine("Loading Rewards");
Expand Down
47 changes: 47 additions & 0 deletions ZeldaOracle/ConscriptDesigner/Themes/ConscriptHighlighting.xshd
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,53 @@
<Word>PALETTE</Word>
<Word>SETSTYLE</Word>
</Keywords>

<!--ItemSR-->
<Keywords color="Commands">
<Word>ITEM</Word>
<Word>AMMO</Word>
<Word>END</Word>
<Word>MAXLEVEL</Word>
<Word>NAME</Word>
<Word>DESCRIPTION</Word>
<Word>MESSAGE</Word>
<Word>SPRITE</Word>
<Word>PRICE</Word>
<Word>AMMO</Word>
<Word>MAXAMMO</Word>
<Word>HOLDTYPE</Word>
<Word>EQUIPSPRITE</Word>
<Word>SECONDSLOT</Word>
<Word>ESSENCESLOT</Word>
<Word>LEVELUPAMMO</Word>

<Word>NAME</Word>
<Word>DESCRIPTION</Word>
<Word>OBTAIN</Word>
<Word>CANTCOLLECT</Word>
<Word>SPRITE</Word>
<Word>AMOUNTBASED</Word>
<Word>MAXAMOUNT</Word>
</Keywords>

<!--RewardSR-->
<Keywords color="Commands">
<Word>REWARD</Word>
<Word>CLONE</Word>
<Word>END</Word>
<Word>MESSAGE</Word>
<Word>OBTAIN</Word>
<Word>CANTCOLLECT</Word>
<Word>HOLDINCHEST</Word>
<Word>HOLDTYPE</Word>
<Word>PICKUPMESSAGE</Word>
<Word>WEAPONINTERACT</Word>
<Word>BOUNCESOUND</Word>

<Word>AMOUNT</Word>
<Word>FULL</Word>
<Word>AMMO</Word>
</Keywords>
</RuleSet>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Rectangle = System.Drawing.Rectangle;
using Bitmap = System.Drawing.Bitmap;
using BitmapData = System.Drawing.Imaging.BitmapData;
using ImageLockMode = System.Drawing.Imaging.ImageLockMode;
using PixelFormat = System.Drawing.Imaging.PixelFormat;
using Rectangle = System.Drawing.Rectangle;
using Bitmap = System.Drawing.Bitmap;
using BitmapData = System.Drawing.Imaging.BitmapData;
using ImageLockMode = System.Drawing.Imaging.ImageLockMode;
using PixelFormat = System.Drawing.Imaging.PixelFormat;
using ZeldaOracle.Common.Graphics;
using XnaColor = Microsoft.Xna.Framework.Color;

namespace ZeldaOracle.Common.Content {
/// <summary>A specialized Texture2D loader used to fix
/// Texture2D.FromStream's broken nature.</summary>
public static class Texture2DLoader {
public static class ImageLoader {

/// <summary>Loads the texture from the stream with the specified file size.</summary>
public static Texture2D FromFile(string filePath) {
public static Image FromFile(string filePath) {
using (FileStream stream = File.Open(filePath, FileMode.Open))
return FromStream(stream);
}

/// <summary>Loads the texture from the stream with the specified file size.</summary>
public static Texture2D FromStream(Stream stream, int fileSize) {
public static Image FromStream(Stream stream, int fileSize) {
BinaryReader reader = new BinaryReader(stream);
using (Stream memory = new MemoryStream(reader.ReadBytes(fileSize)))
return FromStream(memory);
}

/// <summary>Loads the texture from the stream.</summary>
public static unsafe Texture2D FromStream(Stream stream) {
public static unsafe Image FromStream(Stream stream) {
// Load through GDI Bitmap because it doesn't cause issues with alpha
using (Bitmap bitmap = (Bitmap) Bitmap.FromStream(stream)) {
// Create a texture and array to output the bitmap to
Texture2D texture = new Texture2D(Resources.GraphicsDevice,
bitmap.Width, bitmap.Height, false, SurfaceFormat.Color);
Color[] data = new Color[bitmap.Width * bitmap.Height];
Image image = new Image(Resources.GraphicsDevice,
bitmap.Width, bitmap.Height, SurfaceFormat.Color);
//Texture2D texture = new Texture2D(Resources.GraphicsDevice,
// bitmap.Width, bitmap.Height, false, SurfaceFormat.Color);
XnaColor[] data = new XnaColor[bitmap.Width * bitmap.Height];

// Get the pixels from the bitmap
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
Expand All @@ -44,19 +48,19 @@ public static unsafe Texture2D FromStream(Stream stream) {
byte* ptr = (byte*) bmpData.Scan0;
for (int i = 0; i < data.Length; i++) {
// Go through every color and reverse red and blue channels
data[i] = new Color(ptr[2], ptr[1], ptr[0], ptr[3]);
data[i] = new XnaColor(ptr[2], ptr[1], ptr[0], ptr[3]);
ptr += 4;
}

bitmap.UnlockBits(bmpData);

// Assign the data to the texture
texture.SetData<Color>(data);
image.Texture.SetData<XnaColor>(data);

// Fun fact: All this extra work is actually 50% faster than
// Texture2D.FromStream! It's not only broken, but slow as well.

return texture;
return image;
}
}
}
Expand Down
35 changes: 32 additions & 3 deletions ZeldaOracle/Game/Common/Content/Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
using ZeldaOracle.Common.Scripts;
using ZeldaOracle.Common.Scripts.CustomReaders;
using ZeldaOracle.Common.Translation;
using ZeldaOracle.Game.Items;
using ZeldaOracle.Game.Tiles;
using ZeldaOracle.Game.Tiles.ActionTiles;
using ZeldaOracle.Game.Worlds;
using Song = ZeldaOracle.Common.Audio.Song;
using XnaSong = Microsoft.Xna.Framework.Media.Song;
using ZeldaOracle.Common.Graphics.Sprites;
using ZeldaOracle.Game.Items.Rewards;

namespace ZeldaOracle.Common.Content {

Expand Down Expand Up @@ -117,6 +119,9 @@ public class Resources {
private static Dictionary<string, Tileset> tilesets;
private static Dictionary<string, Zone> zones;
private static Dictionary<string, PropertyAction> propertyActions;
private static Dictionary<string, Item> items;
private static Dictionary<string, Ammo> ammos;
private static Dictionary<string, Reward> rewards;

// SOUNDS:
/// <summary>The collection of loaded sound effects.</summary>
Expand Down Expand Up @@ -165,6 +170,10 @@ public class Resources {
/// <summary>The directory for storing languages.</summary>
public const string LanguageDirectory = "Languages/";

// Items
/// <summary>The directory for storing items, ammos, and rewards.</summary>
public const string ItemDirectory = "Items/";


//-----------------------------------------------------------------------------
// Initialization
Expand Down Expand Up @@ -204,6 +213,10 @@ public static void Initialize(SpriteBatch spriteBatch, GraphicsDevice graphicsDe
tilesets = new Dictionary<string, Tileset>();
zones = new Dictionary<string, Zone>();

items = new Dictionary<string, Item>();
ammos = new Dictionary<string, Ammo>();
rewards = new Dictionary<string, Reward>();

propertyActions = new Dictionary<string, PropertyAction>();

// Settings
Expand All @@ -226,12 +239,15 @@ public static void Initialize(SpriteBatch spriteBatch, GraphicsDevice graphicsDe
resourceDictionaries[typeof(CollisionModel)] = collisionModels;
resourceDictionaries[typeof(BaseTileData)] = baseTileData;
resourceDictionaries[typeof(TileData)] = tileData;
resourceDictionaries[typeof(ActionTileData)] = actionTileData;
resourceDictionaries[typeof(ActionTileData)] = actionTileData;
resourceDictionaries[typeof(Tileset)] = tilesets;
resourceDictionaries[typeof(Zone)] = zones;
resourceDictionaries[typeof(PropertyAction)] = propertyActions;
resourceDictionaries[typeof(PaletteDictionary)] = paletteDictionaries;
resourceDictionaries[typeof(Palette)] = palettes;
resourceDictionaries[typeof(PaletteDictionary)] = paletteDictionaries;
resourceDictionaries[typeof(Palette)] = palettes;
resourceDictionaries[typeof(Item)] = items;
resourceDictionaries[typeof(Ammo)] = ammos;
resourceDictionaries[typeof(Reward)] = rewards;
}

public static void Uninitialize() {
Expand All @@ -255,6 +271,9 @@ public static void Uninitialize() {
actionTileData = null;
tilesets = null;
zones = null;
items = null;
ammos = null;
rewards = null;
propertyActions = null;
paletteDictionaries = null;
foreach (var pair in palettes) {
Expand Down Expand Up @@ -609,6 +628,16 @@ public static void LoadTiles(string assetName) {
LoadScript(assetName, new TileDataSR());
}

/// <summary>Loads/compiles items and ammos from a script file.</summary>
public static void LoadItems(string assetName) {
LoadScript(assetName, new ItemSR());
}

/// <summary>Loads/compiles items and rewards from a script file.</summary>
public static void LoadRewards(string assetName) {
LoadScript(assetName, new RewardSR());
}

/// <summary>Loads/compiles zones from a script file.</summary>
/// <param name="postTileData">Set to false when loading zones right after palettes in-case of an error
/// in order to continue previewing sprites with a specific zone.</param>
Expand Down
42 changes: 40 additions & 2 deletions ZeldaOracle/Game/Common/Graphics/GameFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
using System.Linq;
using System.Text;
using ZeldaOracle.Common.Geometry;
using ZeldaOracle.Common.Graphics.Sprites;
using ZeldaOracle.Common.Translation;
using ZeldaOracle.Game;
using ZeldaOracle.Game.API;

namespace ZeldaOracle.Common.Graphics {

/// <summary>A monospaced sprite font.</summary>
public class GameFont {

Expand Down Expand Up @@ -45,7 +46,7 @@ public GameFont(SpriteSheet spriteSheet, int charactersPerRow, int characterSpac


//-----------------------------------------------------------------------------
// Character Cell
// Characters
//-----------------------------------------------------------------------------

/// <summary>A helper method to get the source rect for the specified character.</summary>
Expand All @@ -55,6 +56,39 @@ public Rectangle2I GetCharacterCell(char character) {
return spriteSheet.GetSourceRect(index % charactersPerRow, index / charactersPerRow);
return spriteSheet.GetSourceRect(Point2I.Zero);
}


//-----------------------------------------------------------------------------
// Sprites
//-----------------------------------------------------------------------------

/// <summary>Gets the sprite for the specified character.</summary>
public BasicSprite GetSprite(char character) {
int index = (int) character;
if (index < charactersPerRow * spriteSheet.Height)
return spriteSheet.GetSprite(index % charactersPerRow, index / charactersPerRow);
return spriteSheet.GetSprite(Point2I.Zero);
}

/// <summary>Gets the colored sprite for the specified character.</summary>
public CanvasSprite GetSprite(char character, ColorOrPalette color) {
return GetSprite("" + character, color);
}

/// <summary>Gets the colored sprite for the specified string.</summary>
public CanvasSprite GetSprite(DrawableString text, ColorOrPalette color) {
CanvasSprite canvas = new CanvasSprite(MeasureString(text));
Graphics2D g = canvas.Begin(GameSettings.DRAW_MODE_DEFAULT);
g.DrawString(this, text, Vector2F.Zero, color);
canvas.End(g);
return canvas;
}

/// <summary>Gets the colored sprite for the specified formatted string.</summary>
public CanvasSprite GetFormattedSprite(string text, ColorOrPalette color) {
LetterString letterStr = FormatCodes.FormatString(text, null);
return GetSprite(letterStr, color);
}


//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -238,6 +272,10 @@ public WrappedLetterString WrapString(string text, int width, int caretPosition,
caretLine = lines.Count - 1;
}

// Make a dummy line to prevent issues with the text reader.
//if (lines.Count == 0)
// lines.Add(new LetterString());

WrappedLetterString wrappedString = new WrappedLetterString();
wrappedString.Lines = lines.ToArray();
wrappedString.LineLengths = lineLengths.ToArray();
Expand Down
81 changes: 81 additions & 0 deletions ZeldaOracle/Game/Common/Graphics/RenderTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework.Graphics;
using ZeldaOracle.Common.Geometry;

namespace ZeldaOracle.Common.Graphics {
/// <summary>A subclass of image that confirms the texture is a render target.</summary>
public class RenderTarget : Image {

//-----------------------------------------------------------------------------
// Constructors
//-----------------------------------------------------------------------------

/// <summary>Constructs an unassigned render target.</summary>
public RenderTarget() : base() { }

/// <summary>Constructs an render target with the specified render target.</summary>
public RenderTarget(RenderTarget2D renderTarget) : base(renderTarget) { }

/// <summary>Constructs an new render target with the specified size.</summary>
public RenderTarget(GraphicsDevice graphicsDevice, int width, int height)
: base(new RenderTarget2D(graphicsDevice, width, height)) { }

/// <summary>Constructs an new render target with the specified size.</summary>
public RenderTarget(GraphicsDevice graphicsDevice, Point2I size)
: base(new RenderTarget2D(graphicsDevice, size.X, size.Y)) { }

/// <summary>Constructs an new render target with the specified texture
/// information.</summary>
public RenderTarget(GraphicsDevice graphicsDevice, int width, int height,
SurfaceFormat format)
: base(new RenderTarget2D(graphicsDevice, width, height, false, format,
DepthFormat.None)) { }

/// <summary>Constructs an new render target with the specified texture
/// information.</summary>
public RenderTarget(GraphicsDevice graphicsDevice, Point2I size,
SurfaceFormat format)
: base(new RenderTarget2D(graphicsDevice, size.X, size.Y, false, format,
DepthFormat.None)) { }

/// <summary>Constructs an new render target with the specified texture
/// information.</summary>
public RenderTarget(GraphicsDevice graphicsDevice, int width, int height,
SurfaceFormat format, RenderTargetUsage usage)
: base(new RenderTarget2D(graphicsDevice, width, height, false, format,
DepthFormat.None, 0, usage)) { }

/// <summary>Constructs an new render target with the specified texture
/// information.</summary>
public RenderTarget(GraphicsDevice graphicsDevice, Point2I size,
SurfaceFormat format, RenderTargetUsage usage)
: base(new RenderTarget2D(graphicsDevice, size.X, size.Y, false, format,
DepthFormat.None, 0, usage)) { }


//-----------------------------------------------------------------------------
// Operators
//-----------------------------------------------------------------------------

/// <summary>Used to auto-convert RenderTarget into XNA RenderTarget2Ds.</summary>
public static implicit operator RenderTarget2D(RenderTarget image) {
return (RenderTarget2D) image.Texture;
}


//-----------------------------------------------------------------------------
// Properties
//-----------------------------------------------------------------------------

// Information ----------------------------------------------------------------

/// <summary>Gets the XNA render target of the render target.</summary>
public RenderTarget2D RenderTarget2D {
get { return (RenderTarget2D) Texture; }
}
}
}
Loading