Skip to content

Commit

Permalink
implemented basic player health system and combat system. jumps onto …
Browse files Browse the repository at this point in the history
…enemy = kill enemy; runs into enemy = lose health
  • Loading branch information
tommyzty committed Nov 7, 2017
1 parent 6356f15 commit 3df724c
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 1,442 deletions.
848 changes: 61 additions & 787 deletions Assets/_Scenes/sandbox.unity

Large diffs are not rendered by default.

812 changes: 225 additions & 587 deletions Assets/_Scenes/sandbox.unity.orig

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions Assets/_Scripts/Bounds.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Bounds : MonoBehaviour {

Expand All @@ -16,9 +17,12 @@ void Start ()
void OnCollisionStay2D(Collision2D coll)
{
if (coll.gameObject.tag == "Player") {
//Die
player.transform.position = new Vector2 (player.transform.position.x - 5.0f, 0.0f);
//Die
player.transform.position = new Vector2 (player.transform.position.x - 5.0f, 1.0f);
followplayer.setCameraOnPlayer ();
}
} else
{
Destroy(coll.gameObject);
}
}
}
84 changes: 54 additions & 30 deletions Assets/_Scripts/EnemyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,91 @@ public class EnemyController : MonoBehaviour {

public Vector2 walkAmount;

public float wallLeft; // Define wallLeft
public float wallRight; // Define wallRight
float walkingDirection = 1.0f;
public float walkSpeed = 1.0f;
public float moveAmount = 2.5f;
//public float wallLeft;
//public float wallRight;
//float walkingDirection = 1.0f;
//public float walkSpeed = 1.0f;
//public float moveAmount = 2.5f;
public bool stomped;
private bool fall;

public FollowPlayer followplayer;
private GameObject player;
public float speed;
public LayerMask enemyMask;
Rigidbody2D myBody;
Transform myTrans;
float myWidth;
float horizontalRayLength = 0.02f;

private void Start()
{
wallLeft = transform.position.x - moveAmount;
wallRight = transform.position.x + moveAmount;
followplayer = FindObjectOfType(typeof(FollowPlayer)) as FollowPlayer;
player = GameObject.FindGameObjectWithTag("Player");
//wallLeft = transform.position.x - moveAmount;
//wallRight = transform.position.x + moveAmount;
stomped = false;
myTrans = transform;
myBody = GetComponent<Rigidbody2D>();
myWidth = GetComponent<SpriteRenderer>().bounds.extents.x;
}

private void Update()
private void FixedUpdate()
{
if (stomped)
{
Vector3 newScale = transform.localScale;
newScale.y /= 2;
newScale.y = newScale.y / 2;
transform.localScale = newScale;
Vector3 newPosition = transform.position;
newPosition.y -= newScale.y;
transform.position = newPosition;
stomped = false;
Physics2D.IgnoreCollision(GetComponent<Collider2D>(), GameObject.FindGameObjectWithTag("Ground").GetComponent<Collider2D>());
fall = true;
Vector2 myVelocity = myBody.velocity;
myVelocity.y = -1.0f;
myVelocity.x = 0;
myBody.velocity = myVelocity;
}
else if (fall)
{
Vector3 newPosition = transform.position;
newPosition.y -= 0.05f;
transform.position = newPosition;
Vector2 myVelocity = myBody.velocity;
myVelocity.y *= 1.1f;
myBody.velocity = myVelocity;
}
else
{
// check to see if there's ground in front of us before moving forward
Vector2 lineCastPos = myTrans.position - myTrans.right * myWidth / 2;
bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask);
Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
bool isBlocked = Physics2D.Linecast(myTrans.position, lineCastPos, enemyMask);
Debug.DrawLine(myTrans.position, lineCastPos);

walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
if (walkingDirection > 0.0f && transform.position.x >= wallRight)
{
walkingDirection = -1.0f;
}
else if (walkingDirection < 0.0f && transform.position.x <= wallLeft)
// if there's no ground, turn around
if (!isGrounded || isBlocked)
{
walkingDirection = 1.0f;
Vector3 currRotation = myTrans.eulerAngles;
currRotation.y += 180;
myTrans.eulerAngles = currRotation;
}
transform.Translate(walkAmount);

// always move forward
Vector2 myVelocity = myBody.velocity;
myVelocity.x = -myTrans.right.x * speed;
myBody.velocity = myVelocity;
}
}

private void OnTriggerEnter2D(Collider2D collision)
/*
private void walk()
{
if (collision.gameObject.tag == "Player" && !stomped)
walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
if (walkingDirection > 0.0f && transform.position.x >= wallRight)
{
//Die
player.transform.position = new Vector2(player.transform.position.x - 5.0f, 0.0f);
followplayer.setCameraOnPlayer();
walkingDirection = -1.0f;
}
}
else if (walkingDirection < 0.0f && transform.position.x <= wallLeft)
{
walkingDirection = 1.0f;
}
transform.Translate(walkAmount);
}*/
}
10 changes: 10 additions & 0 deletions Assets/_Scripts/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using UnityEngine;
using System.Collections;

public static class ExtensionMethods
{
public static Vector2 toVector2(this Vector3 vec3)
{
return new Vector2(vec3.x, vec3.y);
}
}

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

87 changes: 72 additions & 15 deletions Assets/_Scripts/PlayerController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Prime31;

public class PlayerController : MonoBehaviour {

public float speed = 7.0f;
public float gravity = -25f;
public float MinJumpHeight = 0.6f;
Expand Down Expand Up @@ -42,7 +42,14 @@ public class PlayerController : MonoBehaviour {
private RaycastHit2D _lastControllerColliderHit;
private Vector3 _velocity;

void Awake()

// combat
public int health = 3;
public float invincibleTimeAfterHurt = 2;
[HideInInspector]
Collider2D[] myColliders;

void Awake()
{
_controller = GetComponent<CharacterController2D>();
_animator = GetComponent<Animator> ();
Expand All @@ -52,26 +59,76 @@ void Awake()
_controller.onControllerCollidedEvent += onControllerCollider;
_controller.onTriggerEnterEvent += onTriggerEnterEvent;
_controller.onTriggerExitEvent += onTriggerExitEvent;
}

myColliders = GetComponents<Collider2D>();
}

#region Event Listeners

void onControllerCollider( RaycastHit2D hit )
{
// bail out on plain old ground hits cause they arent very interesting
if( hit.normal.y == 1f )
// bail out on plain old ground hits cause they arent very interesting
if ( hit.normal.y == 1f )
return;
if (hit.collider.tag == "Ground")
isCollidingWall = true;
else
isCollidingWall = false;

// logs any collider hits if uncommented. it gets noisy so it is commented out for the demo
//Debug.Log( "flags: " + _controller.collisionState + ", hit.normal: " + hit.normal );
}


void onTriggerEnterEvent( Collider2D col )
GameObject gameObject = hit.collider.gameObject;
if (gameObject.tag == "Enemy")
{
//gameObject.GetComponent<EnemyController>().stomped = true;
}

// logs any collider hits if uncommented. it gets noisy so it is commented out for the demo
//Debug.Log( "flags: " + _controller.collisionState + ", hit.normal: " + hit.normal );
}

IEnumerator Hurt()
{

Debug.Log("hurt");
health--;
if (health <= 0)
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
else
{
int enemyLayer = LayerMask.NameToLayer("Enemy");
int playerLayer = LayerMask.NameToLayer("Player");
Physics2D.IgnoreLayerCollision(enemyLayer, playerLayer);
foreach (Collider2D collider in myColliders)
{
collider.enabled = false;
collider.enabled = true;
}
yield return new WaitForSeconds(invincibleTimeAfterHurt);
Physics2D.IgnoreLayerCollision(enemyLayer, playerLayer, false);
}
}

private void OnCollisionEnter2D(Collision2D collision)
{
EnemyController enemy = collision.collider.GetComponent<EnemyController>();

if (enemy != null)
{
foreach (ContactPoint2D point in collision.contacts)
{
Debug.DrawLine(point.point, point.point + point.normal, Color.red, 10);
if (point.normal.y >= 0.9f)
{
_velocity = new Vector2(_velocity.x, Mathf.Sqrt(2f * MaxJumpHeight * -gravity));
enemy.stomped = true;
}
else {
StartCoroutine(Hurt());
}
}
}
}

void onTriggerEnterEvent( Collider2D col )
{
Debug.Log( "onTriggerEnterEvent: " + col.gameObject.name );
}
Expand Down Expand Up @@ -223,11 +280,11 @@ void Update()
if (isWallSliding)
_velocity.y /= wallsSlideModifier;

_controller.move( _velocity * Time.deltaTime );
_controller.move(_velocity * Time.deltaTime);

// grab our current _velocity to use as a base for all calculations
_velocity = _controller.velocity;
}
// grab our current _velocity to use as a base for all calculations
_velocity = _controller.velocity;
}

void checkWallJump(int sign)
{
Expand Down
16 changes: 0 additions & 16 deletions Assets/_Scripts/StompEnemy.cs

This file was deleted.

10 changes: 10 additions & 0 deletions Assets/noFrictionNoBounce.physicsMaterial2D
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!62 &6200000
PhysicsMaterial2D:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: noFrictionNoBounce
friction: 0
bounciness: 0
10 changes: 10 additions & 0 deletions Assets/noFrictionNoBounce.physicsMaterial2D.meta

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

10 changes: 8 additions & 2 deletions ProjectSettings/TagManager.asset
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ TagManager:
-
- Ground
- Player
-
-
- Enemy
- Bound
-
-
-
Expand All @@ -46,3 +46,9 @@ TagManager:
- name: Default
uniqueID: 0
locked: 0
- name: Enemy
uniqueID: 1943075123
locked: 0
- name: Player
uniqueID: 3909492469
locked: 0

0 comments on commit 3df724c

Please sign in to comment.