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

Character Animation #669

Merged
merged 6 commits into from
Aug 23, 2016
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
17 changes: 15 additions & 2 deletions Assets/Scripts/Controllers/CharacterSpriteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,24 @@ public void OnCharacterCreated(Character c)
char_go.transform.position = new Vector3(c.X, c.Y, 0);
char_go.transform.SetParent(characterParent.transform, true);

SpriteRenderer sr = char_go.AddComponent<SpriteRenderer>();
sr.sprite = SpriteManager.current.GetSprite("Character", "p2_front");
SpriteRenderer sr = char_go.AddComponent<SpriteRenderer>();
sr.sortingLayerName = "Characters";
sr.color = c.GetCharacterColor();

c.animation = new CharacterAnimation(c, sr);
Sprite[] sprites = {
SpriteManager.current.GetSprite("Character", "p1_nh_idle_south"),
SpriteManager.current.GetSprite("Character", "p1_nh_idle_east"),
SpriteManager.current.GetSprite("Character", "p1_nh_idle_north"),
SpriteManager.current.GetSprite("Character", "p1_nh_walk_east_01"),
SpriteManager.current.GetSprite("Character", "p1_nh_walk_east_02"),
SpriteManager.current.GetSprite("Character", "p1_nh_walk_north_01"),
SpriteManager.current.GetSprite("Character", "p1_nh_walk_north_02"),
SpriteManager.current.GetSprite("Character", "p1_nh_walk_south_01"),
SpriteManager.current.GetSprite("Character", "p1_nh_walk_south_02")
};
c.animation.SetSprites(sprites);

// Add the inventory sprite onto the character
GameObject inv_go = new GameObject("Inventory");
SpriteRenderer inv_sr = inv_go.AddComponent<SpriteRenderer>();
Expand Down
42 changes: 42 additions & 0 deletions Assets/Scripts/Models/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
using MoonSharp.Interpreter;
using UnityEngine;

public enum Facing
{
NORTH,
EAST,
SOUTH,
WEST
}

/// <summary>
/// A Character is an entity on the map that can move between tiles and,
/// for now, grabs jobs from the work queue and performs this.
Expand Down Expand Up @@ -138,6 +146,15 @@ public Tile JobTile
// The item we are carrying (not gear/equipment)
public Inventory inventory;

// holds all character animations
public CharacterAnimation animation;

// is the character walking or idle
public bool IsWalking;

// 0=north, 1=east, 2=south, 3=west
public Facing CharFacing;

/// Use only for serialization
public Character()
{
Expand Down Expand Up @@ -445,6 +462,7 @@ private void Update_DoMovement(float deltaTime)
if (CurrTile == DestTile)
{
pathAStar = null;
IsWalking = false;
return; // We're already were we want to be.
}

Expand All @@ -469,15 +487,36 @@ private void Update_DoMovement(float deltaTime)
NextTile = pathAStar.Dequeue();
}

IsWalking = true;

// Grab the next waypoint from the pathing system!
NextTile = pathAStar.Dequeue();

if (NextTile == CurrTile)
{
IsWalking = false;
// Debug.LogError("Update_DoMovement - nextTile is currTile?");
}
}

// Find character facing
if (NextTile.X > CurrTile.X)
{
CharFacing = Facing.EAST;
}
else if (NextTile.X < CurrTile.X)
{
CharFacing = Facing.WEST;
}
else if (NextTile.Y > CurrTile.Y)
{
CharFacing = Facing.NORTH;
}
else
{
CharFacing = Facing.SOUTH;
}

// At this point we should have a valid nextTile to move to.

// What's the total distance from point A to point B?
Expand Down Expand Up @@ -544,6 +583,9 @@ public void Update(float deltaTime)
{
cbCharacterChanged(this);
}

animation.Update(deltaTime);

}

private void OnJobStopped(Job j)
Expand Down
124 changes: 124 additions & 0 deletions Assets/Scripts/Models/CharacterAnimation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#region License
// ====================================================
// Project Porcupine Copyright(C) 2016 Team Porcupine
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
#endregion
using System;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// CharacterAnimation gets reference to character, and should be able to
/// figure out which animation to play, by looking at character Facing and IsMoving.
/// </summary>
public class CharacterAnimation
{
private Character character;
private SpriteRenderer renderer;

// currentframe is incremented each update... fix to run on time instead
private int currentFrame = 0;

// frames before loop. halfway through the next frame is triggered
private int animationLength = 40;

// TODO: should be more flexible ....
private Sprite[] sprites = new Sprite[9];

public CharacterAnimation(Character c, SpriteRenderer r)
{
character = c;
renderer = r;
}

public void Update(float deltaTime)
{
if (currentFrame >= animationLength)
{
currentFrame = 0;
}

currentFrame++;

CallAnimation();
}

public void SetSprites(Sprite[] s)
{
sprites = s;
}

private void CallAnimation()
{
if (character.IsWalking)
{
// character walking
switch (character.CharFacing)
{
case Facing.NORTH: // walk north
ToggleAnimation(5, 6);
renderer.flipX = false;
break;
case Facing.EAST: // walk east
ToggleAnimation(3, 4);
renderer.flipX = false;
break;
case Facing.SOUTH: // walk south
ToggleAnimation(7, 8);
renderer.flipX = false;
break;
case Facing.WEST: // walk west
ToggleAnimation(3, 4); // FLIP east sprite
renderer.flipX = true;
break;
default:
break;
}
}
else
{
// character idle
switch (character.CharFacing)
{
case Facing.NORTH: // walk north
ShowSprite(2);
renderer.flipX = false;
break;
case Facing.EAST: // walk east
ShowSprite(1);
renderer.flipX = false;
break;
case Facing.SOUTH: // walk south
ShowSprite(0);
renderer.flipX = false;
break;
case Facing.WEST: // walk west
ShowSprite(1); // FLIP east sprite
renderer.flipX = true;
break;
default:
break;
}
}
}

private void ToggleAnimation(int s1, int s2)
{
if (currentFrame == 1)
{
renderer.sprite = sprites[s1];
}
else if (currentFrame == animationLength / 2)
{
renderer.sprite = sprites[s2];
}
}

private void ShowSprite(int s)
{
renderer.sprite = sprites[s];
}
}
12 changes: 12 additions & 0 deletions Assets/Scripts/Models/CharacterAnimation.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Assets/StreamingAssets/Images/Character/p1_animated.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Sprites>
<Sprite name="p1_idle_south" x="0" y="420" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_idle_east" x="66" y="420" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_idle_north" x="132" y="420" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_walk_east_01" x="0" y="328" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_walk_east_02" x="66" y="328" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_walk_north_01" x="0" y="236" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_walk_north_02" x="66" y="236" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_walk_south_01" x="0" y="144" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_walk_south_02" x="66" y="144" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
</Sprites>
8 changes: 8 additions & 0 deletions Assets/StreamingAssets/Images/Character/p1_animated.xml.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/StreamingAssets/Images/Character/p1_animated_nohelmet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions Assets/StreamingAssets/Images/Character/p1_animated_nohelmet.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Sprites>
<Sprite name="p1_nh_idle_south" x="0" y="420" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_nh_idle_east" x="66" y="420" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_nh_idle_north" x="132" y="420" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_nh_walk_east_01" x="0" y="328" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_nh_walk_east_02" x="66" y="328" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_nh_walk_north_01" x="0" y="236" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_nh_walk_north_02" x="66" y="236" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_nh_walk_south_01" x="0" y="144" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
<Sprite name="p1_nh_walk_south_02" x="66" y="144" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
</Sprites>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/StreamingAssets/Images/Character/p2_helmet.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Sprites>
<Sprite name="p2_helmet" x="0" y="0" w="66" h="92" pixelPerUnit="66" />
<Sprite name="p2_helmet" x="0" y="0" w="66" h="92" pixelPerUnit="66" pivotY="0.3" />
</Sprites>