-
Notifications
You must be signed in to change notification settings - Fork 277
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
Character Animation #669
Changes from 3 commits
9f0efd8
c2e1773
cafbd91
95d32d8
6fcc5b9
4deec1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
{ | ||
|
@@ -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); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
||
|
@@ -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 | ||
|
@@ -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? | ||
|
@@ -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) | ||
|
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; | ||
} | ||
|
||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
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> |
There was a problem hiding this comment.
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