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 3 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
16 changes: 15 additions & 1 deletion Assets/Scripts/Controllers/CharacterSpriteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,24 @@ public void OnCharacterCreated(Character c)
char_go.transform.SetParent(characterParent.transform, true);

SpriteRenderer sr = char_go.AddComponent<SpriteRenderer>();
sr.sprite = SpriteManager.current.GetSprite("Character", "p2_front");
//sr.sprite = SpriteManager.current.GetSprite("Character", "p2_front");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not be commented out code

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
44 changes: 39 additions & 5 deletions Assets/Scripts/Models/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 int Facing;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should use an enum here. It would make things look more clear.


/// Use only for serialization
public Character()
{
Expand Down Expand Up @@ -329,7 +338,7 @@ private bool CheckForJobMaterials()
// Walk towards a tile containing the required goods.
Debug.Log("Walk to the stuff");
Debug.Log(myJob.canTakeFromStockpile);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theres a handful of lines in this file with trailing white space. Could you fix that? :)

// Find the first thing in the Job that isn't satisfied.
Inventory desired = myJob.GetFirstDesiredInventory();

Expand Down Expand Up @@ -445,9 +454,10 @@ private void Update_DoMovement(float deltaTime)
if (CurrTile == DestTile)
{
pathAStar = null;
IsWalking = false;
return; // We're already were we want to be.
}

// currTile = The tile I am currently in (and may be in the process of leaving)
// nextTile = The tile I am currently entering
// destTile = Our final destination -- we never walk here directly, but instead use it for the pathfinding
Expand All @@ -469,15 +479,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)
{
Facing = 1;
}
else if (NextTile.Y > CurrTile.Y)
{
Facing = 0;
}
else if (NextTile.X < CurrTile.X)
{
Facing = 3;
}
else
{
Facing = 2;
}

// 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 @@ -538,12 +569,15 @@ public void Update(float deltaTime)
{
Update_DoJob(deltaTime);

Update_DoMovement(deltaTime);

Update_DoMovement(deltaTime);
if (cbCharacterChanged != null)
{
cbCharacterChanged(this);
cbCharacterChanged(this);
}

animation.Update(deltaTime);

}

private void OnJobStopped(Job j)
Expand Down
116 changes: 116 additions & 0 deletions Assets/Scripts/Models/CharacterAnimation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
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();
}

private void callAnimation()
{
if (character.IsWalking)
{
// character walking
switch (character.Facing)
{
case 0: // walk north
toggleAnimation(5, 6);
renderer.flipX = false;
break;
case 1: // walk east
toggleAnimation(3, 4);
renderer.flipX = false;
break;
case 2: // walk south
toggleAnimation(7, 8);
renderer.flipX = false;
break;
case 3: // walk west
toggleAnimation(3, 4); // FLIP east sprite
renderer.flipX = true;
break;
default:
break;
}
}
else
{
//character idle
switch (character.Facing)
{
case 0: // walk north
showSprite(2);
renderer.flipX = false;
break;
case 1: // walk east
showSprite(1);
renderer.flipX = false;
break;
case 2: // walk south
showSprite(0);
renderer.flipX = false;
break;
case 3: // 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];
}

public void setSprites(Sprite[] s)
{
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>