Player health

This commit is contained in:
jventalon 2015-11-19 21:24:31 -05:00
parent 1daff51b46
commit 0819f51529
3 changed files with 37 additions and 16 deletions

Binary file not shown.

View File

@ -11,42 +11,62 @@ public class Health : MonoBehaviour
private ArrayList hearts = new ArrayList();
// Width (X) and height (Y) of an heart image with additional blank space
private float spaceX;
private float spaceY;
void Start ()
{
RectTransform r = heartImage.rectTransform;
spaceX = r.rect.width;
spaceY = r.rect.height;
Debug.Log("spaceX : " + spaceX);
Debug.Log("spaceY : " + spaceY);
spaceX = 2.3f;
spaceY = 2.3f;
AddHearts (startingHealth);
}
void AddHearts (int n)
public void AddHearts (int n)
{
for (int i = 0; i < n; i++)
{
Transform heart = ((Image)Instantiate(heartImage)).transform;
heart.SetParent(GameObject.Find("HeartCanvas").transform, false);
// Add heart image to the canvas
heart.SetParent(GameObject.Find("Canvas").transform, false);
// Calculate x and y position of the new heart (in number of hearts)
int y = Mathf.FloorToInt(hearts.Count/maxHeartsPerRow);
int x = hearts.Count - y * maxHeartsPerRow;
Debug.Log("X : " + x);
Debug.Log("Y : " + y);
Vector2 pos = heart.position;
pos.x += x * spaceX;
pos.y += y * spaceY;
Debug.Log("posX : " + pos.x);
Debug.Log("posY : " + pos.y);
pos.y += -y * spaceY;
heart.position = pos;
// Add heart
hearts.Add(heart);
}
}
public void LoseHearts(int n)
{
for (int i = 0; i < n && i < hearts.Count; i++)
{
int lastIndex = hearts.Count - 1;
// Destroy the image of the last heart
Image heartImage = (Image)((Transform)hearts[lastIndex]).GetComponent("Image");
Destroy(heartImage);
// Remove the last heart
hearts.RemoveAt(lastIndex);
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if( collision.gameObject.tag == "Ennemy" )
{
LoseHearts(1);
}
}
}

View File

@ -1,10 +1,11 @@
fileFormatVersion: 2
guid: 496d8c4c25eff1443b8789ad17b173c5
timeCreated: 1447945584
timeCreated: 1447986069
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
defaultReferences:
- heartImage: {fileID: 11427978, guid: 5c6327fc8da56da4e8a5949457d56e76, type: 2}
executionOrder: 0
icon: {instanceID: 0}
userData: