1
0
Fork 0

Refactor Room | Objective | Entity | Door Interaction | Code format | clean up

This commit is contained in:
Unknown 2018-04-22 15:59:14 +02:00
parent 7512fef314
commit 5739f3dfb5
19 changed files with 1422 additions and 461 deletions

View file

@ -175,21 +175,6 @@ GameObject:
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 1
--- !u!1 &1528048736566048
GameObject:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
serializedVersion: 5
m_Component:
- component: {fileID: 4871197587417858}
m_Layer: 0
m_Name: Spawn (1)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!1 &1560863483719772 --- !u!1 &1560863483719772
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -278,7 +263,6 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: m_Children:
- {fileID: 4460105983878628} - {fileID: 4460105983878628}
- {fileID: 4871197587417858}
m_Father: {fileID: 4524829645232432} m_Father: {fileID: 4524829645232432}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -316,7 +300,7 @@ Transform:
m_PrefabInternal: {fileID: 100100000} m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1448116197164334} m_GameObject: {fileID: 1448116197164334}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -9, y: -4.8, z: 0} m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 4405955685927858} m_Father: {fileID: 4405955685927858}
@ -365,19 +349,6 @@ Transform:
m_Father: {fileID: 4896748037289080} m_Father: {fileID: 4896748037289080}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!4 &4871197587417858
Transform:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1528048736566048}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 7.7, y: -0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 4405955685927858}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!4 &4896145802546722 --- !u!4 &4896145802546722
Transform: Transform:
m_ObjectHideFlags: 1 m_ObjectHideFlags: 1
@ -688,7 +659,6 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
doorsRootObject: {fileID: 1394898907781064} doorsRootObject: {fileID: 1394898907781064}
spawnpointRootObject: {fileID: 1560863483719772} spawnpointRootObject: {fileID: 1560863483719772}
enemys: []
--- !u!212 &212157662384668488 --- !u!212 &212157662384668488
SpriteRenderer: SpriteRenderer:
m_ObjectHideFlags: 1 m_ObjectHideFlags: 1

File diff suppressed because it is too large Load diff

View file

@ -4,31 +4,28 @@ using UnityEngine;
public class CameraControl : MonoBehaviour { public class CameraControl : MonoBehaviour {
[SerializeField] [SerializeField]
private GameObject followThis; private GameObject followThis;
private Vector3 offset; private Vector3 offset;
void Start() void Start() {
{ if ( followThis == null )
if (followThis == null)
return; return;
offset = transform.position - followThis.transform.position; offset = transform.position - followThis.transform.position;
} }
void LateUpdate() void LateUpdate() {
{ if ( followThis == null )
if (followThis == null)
return; return;
var target = followThis.transform.position + offset; var target = followThis.transform.position + offset;
var targetVec = target - transform.position; var targetVec = target - transform.position;
targetVec.Scale (new Vector3 (0.05f, 0.05f, 0)); targetVec.Scale(new Vector3(0.05f, 0.05f, 0));
transform.position = transform.position + targetVec; transform.position = transform.position + targetVec;
} }
public void SetFollow(GameObject g) public void SetFollow(GameObject g) {
{
followThis = g; followThis = g;
offset = transform.position - followThis.transform.position; offset = transform.position - followThis.transform.position;
} }

View file

@ -1,6 +1,4 @@
using System.Collections; using UnityEngine;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour { public class Door : MonoBehaviour {
private bool locked = false; private bool locked = false;
@ -9,15 +7,16 @@ public class Door : MonoBehaviour {
GameObject graphics; GameObject graphics;
[SerializeField] [SerializeField]
Room parent; Room parent;
BoxCollider2D boundingBox; BoxCollider2D boundingBox;
BoxCollider2D triggerBox; BoxCollider2D triggerBox;
// Use this for initialization
void Start () { // Use this for initialization
void Start() {
BoxCollider2D[] colliders = GetComponents<BoxCollider2D>(); BoxCollider2D[] colliders = GetComponents<BoxCollider2D>();
foreach (BoxCollider2D collider in colliders) { foreach ( BoxCollider2D collider in colliders ) {
if (collider.isTrigger) { if ( collider.isTrigger ) {
triggerBox = collider; triggerBox = collider;
Debug.Log("Found Door trigger"); Debug.Log("Found Door trigger");
} else { } else {
@ -26,35 +25,45 @@ public class Door : MonoBehaviour {
} }
} }
Unlock(); Unlock();
} }
public void Lock() /// <summary>
{ /// Locks the door.
/// </summary>
public void Lock() {
locked = true; locked = true;
boundingBox.enabled = true; boundingBox.enabled = true;
triggerBox.enabled = false; triggerBox.enabled = false;
graphics.GetComponent<SpriteRenderer>().enabled = true; graphics.GetComponent<SpriteRenderer>().enabled = true;
} }
public void Unlock() /// <summary>
{ /// Unlocks the door.
/// </summary>
public void Unlock() {
locked = false; locked = false;
boundingBox.enabled = false; boundingBox.enabled = false;
triggerBox.enabled = true; triggerBox.enabled = true;
graphics.GetComponent<SpriteRenderer>().enabled = false; graphics.GetComponent<SpriteRenderer>().enabled = false;
} }
public bool IsLocked() /// <summary>
{ /// Returns if the door is locked.
/// </summary>
/// <returns></returns>
public bool IsLocked() {
return locked; return locked;
} }
private void OnTriggerExit2D(Collider2D collision) /// <summary>
{ /// Check if a player moved inside of a room and notify the room.
if (collision.tag == "Player") /// </summary>
{ /// <param name="collision"></param>
Debug.Log("Leavin Trigger"); private void OnTriggerExit2D(Collider2D collision) {
parent.OnPlayerEnter(); if ( collision.tag == "Player" ) {
// TODO better checks
Debug.Log("Leaving Trigger");
parent.OnPlayerEnter(collision.gameObject.GetComponent<Player>());
} }
} }
} }

View file

@ -4,41 +4,38 @@ using UnityEngine;
public class EnemyAI : MonoBehaviour { public class EnemyAI : MonoBehaviour {
[SerializeField] [SerializeField]
private GameObject victim; private GameObject victim;
[SerializeField] [SerializeField]
private GameObject agressor; private GameObject agressor;
[SerializeField] [SerializeField]
private float speed = 1; private float speed = 1;
[SerializeField] [SerializeField]
private float rotaionSpeedWIP = 1; private float rotaionSpeedWIP = 1;
/* /*
* Die destructive dumme deutsche Dungeon Diktator Drifter DLC Debakel Distribution Dokumentations - Druck Datei * Die destructive dumme deutsche Dungeon Diktator Drifter DLC Debakel Distribution Dokumentations - Druck Datei
* */ * */
// Use this for initialization // Use this for initialization
void Start () { void Start() {
} }
// Update is called once per frame // Update is called once per frame
void Update () void Update() {
{ if ( victim == null ) {
if(victim == null) //victim = GameController.instance.GetPlayer().gameObject; // TODO testing purpose only!
{
victim = GameController.instance.GetPlayer().gameObject; // TODO testing purpose only!
return; return;
} }
// movement // movement
agressor.transform.Translate (Vector3.Scale ((victim.transform.position - agressor.transform.position).normalized, new Vector2 (speed, speed))); agressor.transform.Translate(Vector3.Scale(( victim.transform.position - agressor.transform.position ).normalized, new Vector2(speed, speed)));
// rotation // rotation
agressor.transform.Rotate(agressor.transform.forward, Vector3.Angle(victim.transform.position - agressor.transform.position, agressor.transform.rotation * new Vector3(1, 1, 1))); agressor.transform.Rotate(agressor.transform.forward, Vector3.Angle(victim.transform.position - agressor.transform.position, agressor.transform.rotation * new Vector3(1, 1, 1)));
} }
public void SetVictim(GameObject g) public void SetVictim(GameObject g) {
{
victim = g; victim = g;
} }
} }

View file

@ -1,8 +1,3 @@
using System.Collections; public class Collectable : Entity {
using System.Collections.Generic; public Collectable() : base() { }
using UnityEngine;
public class Collectable : Entity{
public Collectable (EntityObjective referringObjective) : base(referringObjective) {}
} }

View file

@ -2,19 +2,14 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public abstract class Entity : MonoBehaviour{ public abstract class Entity : MonoBehaviour {
EntityObjective referringObjective; protected EntityObjective objective;
// Constructor /// <summary>
public Entity(EntityObjective referringObjective) /// Sets the Objective this Entity is associated with.
{ /// </summary>
this.referringObjective = referringObjective; /// <param name="obj">Objective</param>
} public void SetObjective(EntityObjective obj) {
objective = obj;
// kills the entity }
public virtual void Kill()
{
if(referringObjective != null)
referringObjective.Remove (this.gameObject);
}
} }

View file

@ -1,54 +1,77 @@
using System.Collections; public abstract class Mob : Entity {
using System.Collections.Generic; readonly int maxHP;
using UnityEngine; int currentHP;
bool isDead;
public abstract class Mob : Entity {
readonly int maxHP;
int currentHP;
bool isDead;
int damage; int damage;
// Constructor /// <summary>
public Mob(EntityObjective referringObjective, int maxHP) : base(referringObjective) /// Creates a new Mob instance with the given HP.
{ /// </summary>
this.maxHP = maxHP; /// <param name="maxHP"></param>
currentHP = maxHP; public Mob(int maxHP) {
this.maxHP = maxHP;
currentHP = maxHP;
isDead = false;
}
isDead = false; /// <summary>
} /// Inflicts damage to this mob.
/// </summary>
/// <param name="damage"></param>
public void InflictDamage(int damage) {
currentHP -= damage;
if ( !isDead && currentHP <= 0 ) {
isDead = true;
Death();
}
}
// inflicts damage to this mob /// <summary>
public void InflictDamage(int damage) /// This is called when a mob dies.
{ /// </summary>
currentHP -= damage; protected virtual void Death() {
if (!isDead && currentHP <= 0) if ( objective != null )
{ objective.RemoveEntity(this);
base.Kill (); Destroy(gameObject);
isDead = true; }
}
}
// Heals the mob /// <summary>
public void Heal(int healAmount) /// Heals the mob.
{ /// </summary>
if (!isDead) /// <param name="healAmount"></param>
currentHP = (currentHP + healAmount > currentHP) ? maxHP : currentHP + healAmount; public void Heal(int healAmount) {
} if ( !isDead )
currentHP = ( currentHP + healAmount > currentHP ) ? maxHP : currentHP + healAmount;
}
public void SetDamage(int dmg) /// <summary>
{ /// Sets the damage value of a mobs attack.
/// </summary>
/// <param name="dmg"></param>
public void SetDamage(int dmg) {
damage = dmg; damage = dmg;
} }
public int GetDamage() /// <summary>
{ /// Gets the damage value og a mobs attack.
/// </summary>
/// <returns></returns>
public int GetDamage() {
return damage; return damage;
} }
/// <summary>
/// Gets the current HP.
/// </summary>
/// <returns></returns>
public int getHealth() { public int getHealth() {
return currentHP; return currentHP;
} }
/// <summary>
/// Gets the maximum HP.
/// </summary>
/// <returns></returns>
public int getMaxHealth() { public int getMaxHealth() {
return maxHP; return maxHP;
} }

View file

@ -4,38 +4,30 @@ using UnityEngine;
public class Player : Mob { public class Player : Mob {
public Player() : base(null, 100 ) public Player() : base(100) { }
{
}
private void OnCollisionEnter2D(Collision2D collision) /// <summary>
{ /// Collision checking. Player is going to die on any collision with a wall.
/// </summary>
/// <param name="collision"></param>
private void OnCollisionEnter2D(Collision2D collision) {
Debug.Log("Collision"); Debug.Log("Collision");
if (collision.collider.tag == "wall") { if ( collision.collider.tag == "wall" ) {
Kill(); Death();
} else if (collision.collider.tag == "enemy") } else if ( collision.collider.tag == "enemy" ) {
{
Mob m = collision.collider.GetComponent(typeof(Mob)) as Mob; Mob m = collision.collider.GetComponent(typeof(Mob)) as Mob;
if(m != null) if ( m != null ) {
{
InflictDamage(m.GetDamage()); // TODO think about Mob attac mechanic InflictDamage(m.GetDamage()); // TODO think about Mob attac mechanic
} }
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "door") {
//Debug.Log("Open door");
} }
} }
public override void Kill() /// <summary>
{ /// This is called when a Player died.
base.Kill(); /// </summary>
protected override void Death() {
Destroy(this.gameObject); Destroy(this.gameObject);
GameController.instance.ChangeState(GameController.GameState.ENDED); GameController.instance.ChangeState(GameController.GameState.ENDED);
} }
} }

View file

@ -20,8 +20,7 @@ public class GameController : MonoBehaviour {
private bool engineInitDone; private bool engineInitDone;
private Player player; private Player player;
public static GameController instance; public static GameController instance;
public GameController() public GameController() {
{
instance = this; instance = this;
} }
@ -29,15 +28,14 @@ public class GameController : MonoBehaviour {
private GameState state = GameState.UNSET; private GameState state = GameState.UNSET;
// Use this for initialization // Use this for initialization
void Start () { void Start() {
} }
// Update is called once per frame // Update is called once per frame
void Update () { void Update() {
if (!engineInitDone) if ( !engineInitDone ) {
{
engineInitDone = true; engineInitDone = true;
Debug.Log("First Frame"); Debug.Log("First Frame");
ChangeState(GameState.INIT); ChangeState(GameState.INIT);
@ -45,15 +43,14 @@ public class GameController : MonoBehaviour {
} }
public void ChangeState(GameState nextState) { public void ChangeState(GameState nextState) {
if(nextState != state) { if ( nextState != state ) {
state = nextState; state = nextState;
StateLogic(nextState); StateLogic(nextState);
} }
} }
void StateLogic(GameState nstate) { void StateLogic(GameState nstate) {
switch (nstate) switch ( nstate ) {
{
case GameState.INIT: case GameState.INIT:
Init(); Init();
ChangeState(GameState.STARTING); ChangeState(GameState.STARTING);
@ -75,34 +72,26 @@ public class GameController : MonoBehaviour {
} }
private void Init() private void Init() {
{ StartObjective goal = new StartObjective(start, playerPrefab);
List<GameObject> tmp = new List<GameObject> start.SetObjective(goal);
{ start.OnPlayerEnter(player);
playerPrefab player = goal.GetPlayer();
};
start.SetObjective(new EntityObjective(start, tmp));
start.OnPlayerEnter();
player = ((EntityObjective) start.GetObjective()).GetEntities()[0].GetComponent<Player>();
cam.GetComponent<CameraControl>().SetFollow(player.gameObject); cam.GetComponent<CameraControl>().SetFollow(player.gameObject);
((EntityObjective)start.GetObjective()).Remove(player.gameObject);
} }
private void Starting() private void Starting() {
{
} }
private void Running() private void Running() {
{
} }
private void Ended() private void Ended() {
{
Debug.Log("Game ended"); Debug.Log("Game ended");
//Time.timeScale = 0; //Time.timeScale = 0;
if (ui != null) { if ( ui != null ) {
Debug.Log("show gameover UI"); Debug.Log("show gameover UI");
ui.GetComponent<UIController>().ShowGameOverUI(); ui.GetComponent<UIController>().ShowGameOverUI();
} else { } else {
@ -110,8 +99,4 @@ public class GameController : MonoBehaviour {
} }
} }
public Player GetPlayer() {
return player;
}
} }

View file

@ -1,23 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HP : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void SetHP(){
}
public void RotateHpBar(){
}
}

View file

@ -8,23 +8,16 @@ public class HealthbarController : MonoBehaviour {
float maxRotation; float maxRotation;
private Player player; private Player player;
// Use this for initialization // Update is called once per frame
void Start () { void Update() {
UpdatePlayer();
}
// Update is called once per frame
void Update () {
// if player alive and spawned // if player alive and spawned
if (player != null) { if ( player != null ) {
UpdatePointer(player.getHealth()); UpdatePointer(player.getHealth());
} else { } else if (currentRotation != 0) {
//if player dead or not spawned //if player dead or not spawned
UpdatePointer(0); UpdatePointer(0);
UpdatePlayer();
} }
} }
private void UpdatePointer(float playerLife) { private void UpdatePointer(float playerLife) {
float offset = playerLife - currentRotation; float offset = playerLife - currentRotation;
@ -32,11 +25,7 @@ public class HealthbarController : MonoBehaviour {
currentRotation += offset; currentRotation += offset;
} }
private void UpdatePlayer() { public void SetPlayer(Player ply) {
player = GameController.instance.GetPlayer(); player = ply;
if (player != null) {
maxRotation = player.getMaxHealth();
currentRotation = player.getHealth();
}
} }
} }

View file

@ -2,45 +2,57 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
/// <summary>
/// Objective, where the goal is to "remove" all Entitys, how is defined by the Entity itself.
/// </summary>
public class EntityObjective : Objective { public class EntityObjective : Objective {
List<GameObject> prefabList; List<GameObject> prefabList;
List<GameObject> entityList; List<GameObject> entityList;
List<Transform> spawnPointList;
// Constructor /// <summary>
public EntityObjective(Room objectiveCaller, List<GameObject> prefabList) : base(objectiveCaller) /// Creates a new instance of an EntityObjective.
{ /// </summary>
/// <param name="caller"></param>
/// <param name="prefabList"></param>
public EntityObjective(Room caller, List<GameObject> prefabList) : base(caller) {
this.entityList = new List<GameObject>(); this.entityList = new List<GameObject>();
this.prefabList = prefabList; this.prefabList = prefabList;
spawnPointList = objectiveCaller.GetSpawnpoints (); }
}
// Activates the objective to start progresstracking /// <summary>
public override void Activate() /// Handle activation code for a goal.
{ /// </summary>
Random newRand = new Random (); /// <param name="ply">Player</param>
Debug.Log("Activate"); public override void ActivateGoal(Player ply) {
foreach (GameObject i in prefabList) base.ActivateGoal(ply);
{ foreach ( GameObject i in prefabList ) {
Debug.Log("Instantiating Prefab"); Debug.Log("[ROOMS] Spawning Entity...");
GameObject tempObject = GameObject.Instantiate (i); GameObject tempObject = GameObject.Instantiate(i);
tempObject.transform.position = spawnPointList [Random.Range (0, spawnPointList.Count)].position; List<Transform> spawnPointList = room.GetSpawnpoints();
tempObject.transform.position = spawnPointList[Random.Range(0, spawnPointList.Count)].position;
entityList.Add(tempObject); entityList.Add(tempObject);
} }
objectiveCaller.Lock(); room.Lock();
} }
// Removes the entity from the list and completes the objective, if the list is empty /// <summary>
public void Remove(GameObject inputEntity) /// Removes an Entity from the list. And checks if the goal was reached.
{ /// </summary>
entityList.Remove (inputEntity); /// <param name="e">Entity</param>
if (entityList.Count == 0) public void RemoveEntity(Entity e) {
objectiveCaller.Unlock (); if ( e == null )
} return;
entityList.Remove(e.gameObject);
UpdateGoal();
}
public List<GameObject> GetEntities() /// <summary>
{ /// Tracks / Updates the progess of a goal.
return entityList; /// </summary>
public override void UpdateGoal() {
// Goal:
if ( entityList.Count == 0 )
ReachedGoal();
} }
} }

View file

@ -1,16 +1,48 @@
using System.Collections; public abstract class Objective {
using System.Collections.Generic; protected Room room;
using UnityEngine; protected Player player;
bool activated;
bool finished;
public abstract class Objective { /// <summary>
protected Room objectiveCaller; /// Constructs a new Objective instance.
/// </summary>
/// <param name="caller"></param>
public Objective(Room caller) {
this.room = caller;
}
/// <summary>
/// Handle activation code for a goal.
/// </summary>
/// <param name="ply">Player</param>
public virtual void ActivateGoal(Player ply) {
if ( activated )
return;
activated = true;
player = ply;
}
/// <summary>
/// Tracks / Updates the progess of a goal.
/// </summary>
public abstract void UpdateGoal();
/// <summary>
/// Code executed if the goal is reached eg. opening doors.
/// </summary>
protected virtual void ReachedGoal() {
finished = true;
room.Unlock();
}
/// <summary>
/// Returns wether the goal was reached or not.
/// </summary>
/// <returns></returns>
public bool GetFinished() {
return finished;
}
// Constructor
public Objective(Room objectiveCaller)
{
this.objectiveCaller = objectiveCaller;
}
// Activates the objective to start progresstracking
public virtual void Activate(){}
} }

View file

@ -0,0 +1,45 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StartObjective : Objective {
GameObject playerPrefab;
/// <summary>
/// Creates a new StartObjective instance.
/// </summary>
/// <param name="room"></param>
/// <param name="playerPrefab"></param>
public StartObjective(Room room, GameObject playerPrefab) : base(room) {
this.playerPrefab = playerPrefab;
}
// TODO not as good as possible. Should Objectives ActivateGoal method require a player obj?
/// <summary>
/// Handle activation code for a goal.
/// </summary>
/// <param name="ply">Player is ignored </param>
public override void ActivateGoal(Player player=null) {
if ( room.GetSpawnpoints().Count > 0 ) {
GameObject ply = GameObject.Instantiate(playerPrefab);
ply.transform.position = room.GetSpawnpoints()[0].position;
player = ply.GetComponent<Player>();
base.ActivateGoal(player);
}
}
/// <summary>
/// Returns the created Player object. Call this after <see cref="ActivateGoal(Player)"/> !
/// </summary>
/// <returns>Player</returns>
public Player GetPlayer() {
return player;
}
/// <summary>
/// Code executed if the goal is reached eg. opening doors.
/// </summary>
public override void UpdateGoal() {
ReachedGoal();
}
}

View file

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 605b34431fb4e20408494377178f8ecb guid: cc1002426c801284f84b577c992409bc
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View file

@ -1,65 +1,63 @@
using UnityEngine; using UnityEngine;
public class PlayerMovement : MonoBehaviour { public class PlayerMovement : MonoBehaviour {
private bool firstKeyPressed; private bool firstKeyPressed;
[SerializeField] [SerializeField]
public float acceleration = 3; public float acceleration = 3;
[SerializeField] [SerializeField]
public float friction = 0.1f; public float friction = 0.1f;
[SerializeField] [SerializeField]
public float turnSpeed = 2; public float turnSpeed = 2;
[SerializeField] [SerializeField]
public float drift = 1f; public float drift = 1f;
[SerializeField] [SerializeField]
public float brake = 2f; public float brake = 2f;
Rigidbody2D rigidbody2D; Rigidbody2D rb;
// Use this for initialization // Use this for initialization
void Start() { void Start() {
rigidbody2D = GetComponent<Rigidbody2D>(); rb = GetComponent<Rigidbody2D>();
} }
void Update() void Update() {
{ if ( !firstKeyPressed && Input.anyKey ) {
if(!firstKeyPressed && Input.anyKey)
{
firstKeyPressed = true; firstKeyPressed = true;
} }
} }
void FixedUpdate() { void FixedUpdate() {
if (!firstKeyPressed) if ( !firstKeyPressed )
return; return;
Vector3 speedVec = new Vector3(rigidbody2D.velocity.x, rigidbody2D.velocity.y, 0); Vector3 speedVec = new Vector3(rb.velocity.x, rb.velocity.y, 0);
float speed = speedVec.magnitude; float speed = speedVec.magnitude;
{ // Forward { // Forward
rigidbody2D.AddForce(transform.up * acceleration); rb.AddForce(transform.up * acceleration);
} }
{// Drag {// Drag
Vector3 drag = speedVec.normalized * speed * speed * friction * -1; Vector3 drag = speedVec.normalized * speed * speed * friction * -1;
if (Input.GetKey(KeyCode.S)) if ( Input.GetKey(KeyCode.S) )
drag *= brake; drag *= brake;
rigidbody2D.AddForce(drag); rb.AddForce(drag);
Debug.DrawLine(transform.position, transform.position + drag, Color.cyan, 0.01f, false); Debug.DrawLine(transform.position, transform.position + drag, Color.cyan, 0.01f, false);
} }
{ // Drift { // Drift
Vector3 forwardNorm = (transform.localRotation * Vector3.up).normalized * speedVec.magnitude; Vector3 forwardNorm = ( transform.localRotation * Vector3.up ).normalized * speedVec.magnitude;
Vector3 br = forwardNorm - speedVec; Vector3 br = forwardNorm - speedVec;
br *= drift; br *= drift;
rigidbody2D.AddForce(br); rb.AddForce(br);
//Debug.Log(br); //Debug.Log(br);
Debug.DrawLine(transform.position, transform.position + br, Color.red, 0.01f, false); Debug.DrawLine(transform.position, transform.position + br, Color.red, 0.01f, false);
} }
if (Input.GetKey(KeyCode.A)) if ( Input.GetKey(KeyCode.A) )
rigidbody2D.MoveRotation(rigidbody2D.rotation + turnSpeed); rb.MoveRotation(rb.rotation + turnSpeed);
//transform.Rotate(Vector3.forward * turnSpeed); //transform.Rotate(Vector3.forward * turnSpeed);
if (Input.GetKey(KeyCode.D)) if ( Input.GetKey(KeyCode.D) )
rigidbody2D.MoveRotation(rigidbody2D.rotation - turnSpeed); rb.MoveRotation(rb.rotation - turnSpeed);
//transform.Rotate(Vector3.forward * -turnSpeed); //transform.Rotate(Vector3.forward * -turnSpeed);
// Debug lines // Debug lines
Debug.DrawLine(transform.position, transform.position + speedVec, Color.magenta, 0.01f, false); Debug.DrawLine(transform.position, transform.position + speedVec, Color.magenta, 0.01f, false);
Debug.DrawLine(transform.position, transform.position + transform.localRotation * Vector3.up, Color.yellow, 0.01f, false); Debug.DrawLine(transform.position, transform.position + transform.localRotation * Vector3.up, Color.yellow, 0.01f, false);

View file

@ -4,8 +4,6 @@ using UnityEngine;
public class Room : MonoBehaviour { public class Room : MonoBehaviour {
private bool activated;
List<Door> doors; List<Door> doors;
List<Transform> spawnpoints; List<Transform> spawnpoints;
@ -15,81 +13,73 @@ public class Room : MonoBehaviour {
[SerializeField] [SerializeField]
GameObject spawnpointRootObject; GameObject spawnpointRootObject;
[SerializeField] private Objective objective;
private Objective objective;
enum ObjectiveType { EntityObjective } // Use this for initialization
// Params for testing void Start() {
[SerializeField]
GameObject[] enemys;
// Use this for initialization
void Start () {
doors = new List<Door>(); doors = new List<Door>();
foreach (Door d in doorsRootObject.GetComponentsInChildren<Door>()) foreach ( Door d in doorsRootObject.GetComponentsInChildren<Door>() ) {
{
doors.Add(d); doors.Add(d);
} }
Debug.Log("Doors in Room: " + doors.Count); Debug.Log("[ROOMS] Doors: " + doors.Count);
spawnpoints = new List<Transform>(); spawnpoints = new List<Transform>();
foreach (Transform t in spawnpointRootObject.GetComponentsInChildren<Transform>()) foreach ( Transform t in spawnpointRootObject.GetComponentsInChildren<Transform>() ) {
{ if ( t.gameObject != spawnpointRootObject ) {
if( t.gameObject != spawnpointRootObject)
{
spawnpoints.Add(t); spawnpoints.Add(t);
} }
} }
Debug.Log("Spawnpoints in Room: " + spawnpoints.Count); Debug.Log("[ROOMS] Spawnpoints: " + spawnpoints.Count);
if (enemys.Length != 0)
objective = new EntityObjective(this, new List<GameObject> (enemys));
//Unlock();
}
public void SetObjective(Objective o)
{
objective = o;
} }
public void Lock() /// <summary>
{ /// Locks all doors associated with this room.
foreach (Door d in doors) /// </summary>
{ public void Lock() {
foreach ( Door d in doors ) {
d.Lock(); d.Lock();
} }
Debug.Log("[ROOMS] Locked all doors...");
} }
public void Unlock() /// <summary>
{ /// Unlocks all doors associated with this room.
foreach (Door d in doors) /// </summary>
{ public void Unlock() {
foreach ( Door d in doors ) {
d.Unlock(); d.Unlock();
} }
Debug.Log("[ROOMS] Unlocked all doors...");
}
/// <summary>
/// Sets the rooms Objective.
/// </summary>
/// <param name="obj">Objective</param>
public void SetObjective(Objective obj) {
objective = obj;
}
/// <summary>
/// Informs the objective / activates it and ensures that a cleared room is not going to be activated again.
/// </summary>
/// <param name="player"></param>
public void OnPlayerEnter(Player player) {
if ( objective != null && objective.GetFinished() ) {
Debug.Log("[ROOMS] This room has been cleared already.");
return;
}
if ( objective != null ) {
Debug.Log("[ROOMS] Player activated Objective");
objective.ActivateGoal(player);
}
} }
public Objective GetObjective() /// <summary>
{ /// Returns the Spawnpoints a room has.
return objective; /// </summary>
} /// <returns></returns>
public List<Transform> GetSpawnpoints() {
public void OnPlayerEnter()
{
if (activated)
return;
if(objective != null)
objective.Activate();
activated = true;
}
public List<Transform> GetSpawnpoints()
{
return spawnpoints; return spawnpoints;
} }
public bool GetActivated()
{
return activated;
}
} }

View file

@ -3,49 +3,42 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class UIController : MonoBehaviour { public class UIController : MonoBehaviour {
private GameObject score; private GameObject score;
private GameObject pauseMenu; private GameObject pauseMenu;
[SerializeField] [SerializeField]
private GameObject gameOverPanel; private GameObject gameOverPanel;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public static UIController instance;
public void ShowPauseMenu() [SerializeField]
{ private HealthbarController healthcontroller;
public void ShowPauseMenu() {
pauseMenu.SetActive(true); pauseMenu.SetActive(true);
} }
public void ClosePauseMenu()
{ public void ClosePauseMenu() {
pauseMenu.SetActive(false); pauseMenu.SetActive(false);
} }
public void LoadSceneByIndex(int index) public void LoadSceneByIndex(int index) {
{
Debug.Log("Loaded scene " + index); Debug.Log("Loaded scene " + index);
UnityEngine.SceneManagement.SceneManager.LoadScene(index); UnityEngine.SceneManagement.SceneManager.LoadScene(index);
} }
public void QuitGame() public void QuitGame() {
{
Debug.Log("Quit game"); Debug.Log("Quit game");
Application.Quit(); Application.Quit();
} }
public void ShowGameOverUI() { public void ShowGameOverUI() {
if (gameOverPanel != null) { if ( gameOverPanel != null ) {
Canvas gameOverCanvas = gameOverPanel.GetComponent<Canvas>(); Canvas gameOverCanvas = gameOverPanel.GetComponent<Canvas>();
Debug.Log("Loading Canvas"); Debug.Log("Loading Canvas");
if (gameOverCanvas != null) { if ( gameOverCanvas != null ) {
Debug.Log("Loaded Canvas"); Debug.Log("Loaded Canvas");
gameOverCanvas.enabled = true; gameOverCanvas.enabled = true;
} else { } else {
Debug.Log("Gameover panel has no Canvas"); Debug.Log("Gameover panel has no Canvas");
} }
@ -53,5 +46,9 @@ public class UIController : MonoBehaviour {
Debug.Log("No game over panel assigned"); Debug.Log("No game over panel assigned");
} }
} }
public void InitHealthController(Player ply) {
healthcontroller.SetPlayer(ply);
}
} }