Skip to content

Commit

Permalink
Start on edit mode
Browse files Browse the repository at this point in the history
Well, more like stepthrough preview mode. Requires a fighter named Morrigan in bin\data\fighters, but bin is ignored so nyeh. That file will be watched for changes.
  • Loading branch information
Kawa-oneechan committed May 9, 2019
1 parent b60ea5e commit 2a18379
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ bin/
*.user

desktop.ini
kafe.zip
82 changes: 81 additions & 1 deletion Background.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Kawa.Json;

/*
Expand Down Expand Up @@ -97,10 +98,89 @@ public override void Draw(GameTime gameTime)
base.Draw(gameTime);
var batch = Kafe.SpriteBatch;
batch.Begin();

foreach (var c in Characters)
c.Draw(batch);
batch.End();
}
}

class Editor : Background
{
public Character Subject { get; set; }
private System.IO.FileSystemWatcher watcher;
private string topMessage = string.Empty;

public Editor(string file, string charFile) : base(file)
{
try
{
var path = System.IO.Path.Combine("data", "fighters", charFile);
if (System.IO.File.Exists(path))
{
watcher = new System.IO.FileSystemWatcher(System.IO.Path.GetDirectoryName(path), charFile);
watcher.NotifyFilter = System.IO.NotifyFilters.LastWrite;
watcher.Changed += (sender, e) =>
{
tryAgain:
try
{
if (Subject == null)
Subject = new Character(charFile, 0);
else
Subject.Reload(charFile, 0, true);
SetupSubject();
}
catch (System.IO.IOException)
{
goto tryAgain;
}
catch (JsonException jEx)
{
topMessage = jEx.Message;
Subject = null;
}
};
watcher.EnableRaisingEvents = true;
}
Subject = new Character(charFile, 0);
SetupSubject();
}
catch (JsonException jEx)
{
topMessage = jEx.Message;
Subject = null;
}
}

private void SetupSubject()
{
if (Subject == null)
return;
Subject.Position = new Vector2(Kafe.ScreenWidth / 2, Kafe.Ground);
Subject.EditMode = true;
Subject.ShowBoxes = true;
topMessage = string.Empty;
}

public override void Update(GameTime gameTime)
{
if (Input.WasJustReleased(Keys.S))
Subject.Update();
base.Update(gameTime);
}

public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
var batch = Kafe.SpriteBatch;
batch.Begin();
if (Subject != null)
Subject.Draw(batch);
Text.Draw(batch, 1, "Edit mode", 4, Kafe.ScreenHeight - 4 - 24);
Text.Draw(batch, 0, "(S)tep anim", 4, Kafe.ScreenHeight - 4 - 8);
if (!string.IsNullOrWhiteSpace(topMessage))
Text.Draw(batch, 0, topMessage, 4, 4, Color.Red);
batch.End();
}
}
}
163 changes: 109 additions & 54 deletions Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Character
private const int MaxPalettes = 4;

private Texture2D sheet;

private Texture2D[] sheets;

private JsonObj json;
Expand All @@ -37,7 +36,7 @@ class Character

//private string currentAnimName;

private static Texture2D shadow;
private static Texture2D shadow, square;

public Rectangle Image { get; set; }
public Vector2 Position { get; set; }
Expand All @@ -46,12 +45,25 @@ class Character
public bool FacingLeft { get; set; }
public Character Opponent { get; set; }

public bool EditMode { get; set; }
public bool ShowBoxes { get; set; }

public Character(string jsonFile, int palIndex)
{
if (shadow == null)
shadow = Mix.GetTexture("shadow");
if (square == null)
square = Mix.GetTexture("square");

Reload(jsonFile, palIndex, false);
}

public void Reload(string jsonFile, int palIndex, bool refresh)
{
if (palIndex >= MaxPalettes)
palIndex = MaxPalettes - 1;

json = Mix.GetJson("fighters\\" + jsonFile) as JsonObj;
json = Mix.GetJson("fighters\\" + jsonFile, false) as JsonObj;
var baseName = json["base"] as string;
//sheet = Mix.GetTexture("fighters\\" + baseName);

Expand Down Expand Up @@ -90,19 +102,23 @@ public Character(string jsonFile, int palIndex)
animations = (JsonObj)json["animations"];
var idx = 0;
foreach (var a in animations)
{
((JsonObj)a.Value)["index"] = idx++;
animation = animations.ElementAt(0).Value as JsonObj;
((JsonObj)a.Value)["name"] = a.Key;
}
if (!refresh)
animation = animations.ElementAt(0).Value as JsonObj;
else
animation = animations[animation["name"] as string] as JsonObj;
frames = ((List<object>)animation["frames"]).Cast<JsonObj>().ToList();
totalFrames = frames.Count;
currentFrame = 0;
if (!refresh || currentFrame >= totalFrames)
currentFrame = 0;
Position = new Vector2(160, 160);
var image = ((List<object>)frames[currentFrame]["image"]).Select(x => (int)(double)x).ToArray();
var offset = ((List<object>)frames[currentFrame]["offset"]).Select(x => (int)(double)x).ToArray();
Image = new Rectangle(image[0], image[1], image[2], image[3]);
CelOffset = new Vector2(offset[0], offset[1]);

if (shadow == null)
shadow = Mix.GetTexture("shadow");
}

private void SwitchTo(object anim)
Expand Down Expand Up @@ -136,53 +152,64 @@ public void Update()
if (currentFrame >= totalFrames)
{
var fallTos = ((List<object>)animation["fallTo"]);
if (Input.A && fallTos[1] != null)
SwitchTo(fallTos[1]);
else if (Input.B && fallTos[2] != null)
SwitchTo(fallTos[2]);
else if (Input.C && fallTos[3] != null)
SwitchTo(fallTos[3]);
else if (Input.D && fallTos[4] != null)
SwitchTo(fallTos[4]);
else if (Input.E && fallTos[5] != null)
SwitchTo(fallTos[5]);
else if (Input.F && fallTos[6] != null)
SwitchTo(fallTos[6]);
else if (advance && fallTos[7] != null)
SwitchTo(fallTos[7]);
else if (retreat && fallTos[8] != null)
SwitchTo(fallTos[8]);
else if (Input.Up && fallTos[9] != null)
SwitchTo(fallTos[10]);
else if (Input.Down && fallTos[9] != null)
SwitchTo(fallTos[10]);
else if (fallTos[0] != null)
SwitchTo(fallTos[0]);
if (EditMode)
{
if (fallTos[0] != null)
SwitchTo(fallTos[0]);
}
else
{
if (Input.A && fallTos[1] != null)
SwitchTo(fallTos[1]);
else if (Input.B && fallTos[2] != null)
SwitchTo(fallTos[2]);
else if (Input.C && fallTos[3] != null)
SwitchTo(fallTos[3]);
else if (Input.D && fallTos[4] != null)
SwitchTo(fallTos[4]);
else if (Input.E && fallTos[5] != null)
SwitchTo(fallTos[5]);
else if (Input.F && fallTos[6] != null)
SwitchTo(fallTos[6]);
else if (advance && fallTos[7] != null)
SwitchTo(fallTos[7]);
else if (retreat && fallTos[8] != null)
SwitchTo(fallTos[8]);
else if (Input.Up && fallTos[9] != null)
SwitchTo(fallTos[10]);
else if (Input.Down && fallTos[9] != null)
SwitchTo(fallTos[10]);
else if (fallTos[0] != null)
SwitchTo(fallTos[0]);
}
}

var cancelTos = ((List<object>)animation["cancelTo"]);
if (Input.A && cancelTos[1] != null)
SwitchTo(cancelTos[1]);
else if (Input.B && cancelTos[2] != null)
SwitchTo(cancelTos[2]);
else if (Input.C && cancelTos[3] != null)
SwitchTo(cancelTos[3]);
else if (Input.D && cancelTos[4] != null)
SwitchTo(cancelTos[4]);
else if (Input.E && cancelTos[5] != null)
SwitchTo(cancelTos[5]);
else if (Input.F && cancelTos[6] != null)
SwitchTo(cancelTos[6]);
else if (advance && cancelTos[7] != null)
SwitchTo(cancelTos[7]);
else if (retreat && cancelTos[8] != null)
SwitchTo(cancelTos[8]);
else if (Input.Up && cancelTos[9] != null)
SwitchTo(cancelTos[9]);
else if (Input.Down && cancelTos[10] != null)
SwitchTo(cancelTos[10]);
else if (!Input.Anything && cancelTos[0] != null)
SwitchTo(cancelTos[0]);
if (!EditMode)
{
var cancelTos = ((List<object>)animation["cancelTo"]);
if (Input.A && cancelTos[1] != null)
SwitchTo(cancelTos[1]);
else if (Input.B && cancelTos[2] != null)
SwitchTo(cancelTos[2]);
else if (Input.C && cancelTos[3] != null)
SwitchTo(cancelTos[3]);
else if (Input.D && cancelTos[4] != null)
SwitchTo(cancelTos[4]);
else if (Input.E && cancelTos[5] != null)
SwitchTo(cancelTos[5]);
else if (Input.F && cancelTos[6] != null)
SwitchTo(cancelTos[6]);
else if (advance && cancelTos[7] != null)
SwitchTo(cancelTos[7]);
else if (retreat && cancelTos[8] != null)
SwitchTo(cancelTos[8]);
else if (Input.Up && cancelTos[9] != null)
SwitchTo(cancelTos[9]);
else if (Input.Down && cancelTos[10] != null)
SwitchTo(cancelTos[10]);
else if (!Input.Anything && cancelTos[0] != null)
SwitchTo(cancelTos[0]);
}

if (currentFrame >= frames.Count)
currentFrame = 0;
Expand All @@ -200,7 +227,8 @@ public void Update()
if ((int)animation["index"] == 0)
{
Velocity = new Vector2(0, Velocity.Y); //grind to a halt
FacingLeft = (Position.X > Opponent.Position.X);
if (Opponent != null && !EditMode)
FacingLeft = (Position.X > Opponent.Position.X);
}

Position = new Vector2(Position.X, Position.Y + Velocity.Y);
Expand Down Expand Up @@ -252,6 +280,33 @@ public void Draw(SpriteBatch batch)
}

batch.Draw(sheet, new Vector2(posX, posY), Image, Color.White, 0.0f, Vector2.Zero, 1.0f, fx, 0.0f);

if (ShowBoxes && frames[currentFrame].ContainsKey("boxes"))
{
var src = new Rectangle(0, 0, 32, 32);
foreach (var box in ((List<object>)frames[currentFrame]["boxes"]).Cast<JsonObj>())
{
var rect = ((List<object>)box["rect"]).Select(x => (int)(double)x).ToArray();
batch.Draw(square, new Rectangle(rect[0] + posX, rect[1] + posY, rect[2], rect[3]), src, ((bool)box["hit"]) ? Color.Blue : Color.Red);
}
}

if (EditMode)
{
var fallTo0 = ((List<object>)animation["fallTo"])[0];
var fallToA = default(JsonObj);
if (fallTo0 == null)
fallTo0 = 0.0;
if (fallTo0 is double)
fallToA = animations.ElementAt((int)(double)fallTo0).Value as JsonObj;
else
fallToA = animations[fallTo0 as string] as JsonObj;
var fallTo = string.Format("{0} \"{1}\"", fallToA["index"], fallToA["name"]);
Text.Draw(batch, 0,
string.Format("anim {0} \"{1}\"\nframe {2} of {3}\nfall to {4}",
animation["index"], animation["name"], currentFrame, totalFrames, fallTo),
2, 2);
}
}
}
}
5 changes: 3 additions & 2 deletions Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Kafe() : base()
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = CrtWidth;
graphics.PreferredBackBufferHeight = CrtHeight;
IsMouseVisible = false;
IsMouseVisible = true;
}

protected override void Initialize()
Expand All @@ -58,7 +58,8 @@ protected override void LoadContent()
var felicia = new Character("felicia.json", 0);
var sakura = new Character("felicia.json", 1);

var arena = new Arena("locales\\vegas.json", felicia, sakura);
//var arena = new Arena("locales\\vegas.json", felicia, sakura);
var arena = new Editor("locales\\vegas.json", "morrigan.json");
Components.Add(arena);
}

Expand Down
5 changes: 5 additions & 0 deletions Kafe.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Compile Include="json\Kawa.Json.Schema.cs" />
<Compile Include="Mix.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="OpenTK">
Expand All @@ -60,13 +61,17 @@
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Content Include="data\font1.png" />
<Content Include="data\font2.png" />
<Content Include="data\font3.png" />
<Content Include="Icon.ico" />
<Content Include="SDL.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="data\fonts.json" />
<None Include="data\keys.json" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
6 changes: 3 additions & 3 deletions Mix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ public static object GetJson(string fileName, bool useCache = true)
{
if (!fileName.Contains("."))
fileName += ".json";
if (cache.ContainsKey(fileName))
return cache[fileName];
if (useCache && cache.ContainsKey(fileName + "-asObj"))
return cache[fileName + "-asObj"];
var ret = Json5.Parse(GetString(fileName));
if (ret is Dictionary<string, object>)
{
Expand All @@ -194,7 +194,7 @@ public static object GetJson(string fileName, bool useCache = true)
Kawa.Json.Patch.JsonPatch.Apply(ret, patch);
}
}
cache[fileName] = ret;
cache[fileName + "-asObj"] = ret;
return ret;
}

Expand Down
Loading

0 comments on commit 2a18379

Please sign in to comment.