From 83e54fb772a3cf66b3906857ef08590465f6796d Mon Sep 17 00:00:00 2001 From: Saibotk Date: Sun, 22 Apr 2018 23:50:39 +0200 Subject: [PATCH] Entity uses Attack Player shoots bullets --- Assets/Prefabs/Bullet.prefab | 117 ++++++++++++++++++ Assets/Prefabs/Bullet.prefab.meta | 8 ++ Assets/Prefabs/Player.prefab | 35 +++++- Assets/Scenes/CodeDEV.unity | 2 +- Assets/Scripts/Bullet.cs | 46 +++++++ .../{EnemyAI.cs.meta => Bullet.cs.meta} | 2 +- Assets/Scripts/EnemyAI.cs | 46 ------- Assets/Scripts/Entities/Enemy.cs | 59 +++++++++ Assets/Scripts/Entities/Enemy.cs.meta | 11 ++ Assets/Scripts/Entities/Mob.cs | 32 ++--- Assets/Scripts/Entities/Player.cs | 33 ++++- Assets/Scripts/Entities/Scorpion.cs | 12 ++ Assets/Scripts/Entities/Scorpion.cs.meta | 11 ++ .../textures/Entities/Bullet/bullet.png.meta | 35 +++++- 14 files changed, 374 insertions(+), 75 deletions(-) create mode 100644 Assets/Prefabs/Bullet.prefab create mode 100644 Assets/Prefabs/Bullet.prefab.meta create mode 100644 Assets/Scripts/Bullet.cs rename Assets/Scripts/{EnemyAI.cs.meta => Bullet.cs.meta} (83%) delete mode 100644 Assets/Scripts/EnemyAI.cs create mode 100644 Assets/Scripts/Entities/Enemy.cs create mode 100644 Assets/Scripts/Entities/Enemy.cs.meta create mode 100644 Assets/Scripts/Entities/Scorpion.cs create mode 100644 Assets/Scripts/Entities/Scorpion.cs.meta diff --git a/Assets/Prefabs/Bullet.prefab b/Assets/Prefabs/Bullet.prefab new file mode 100644 index 0000000..83f6d32 --- /dev/null +++ b/Assets/Prefabs/Bullet.prefab @@ -0,0 +1,117 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1001 &100100000 +Prefab: + m_ObjectHideFlags: 1 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: [] + m_RemovedComponents: [] + m_ParentPrefab: {fileID: 0} + m_RootGameObject: {fileID: 1379543051791382} + m_IsPrefabParent: 1 +--- !u!1 &1379543051791382 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 100100000} + serializedVersion: 5 + m_Component: + - component: {fileID: 4781595464190240} + - component: {fileID: 58664560518688016} + - component: {fileID: 212185122411066660} + - component: {fileID: 114075590146335024} + m_Layer: 0 + m_Name: Bullet + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &4781595464190240 +Transform: + m_ObjectHideFlags: 1 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 100100000} + m_GameObject: {fileID: 1379543051791382} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: -0.625, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!58 &58664560518688016 +CircleCollider2D: + m_ObjectHideFlags: 1 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 100100000} + m_GameObject: {fileID: 1379543051791382} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: -0.0030702911, y: 0.0782015} + serializedVersion: 2 + m_Radius: 0.05788156 +--- !u!114 &114075590146335024 +MonoBehaviour: + m_ObjectHideFlags: 1 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 100100000} + m_GameObject: {fileID: 1379543051791382} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 0cfff064e4589e34cb5b415dcc016dc1, type: 3} + m_Name: + m_EditorClassIdentifier: + speed: 10 + damage: 0 +--- !u!212 &212185122411066660 +SpriteRenderer: + m_ObjectHideFlags: 1 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 100100000} + m_GameObject: {fileID: 1379543051791382} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RenderingLayerMask: 4294967295 + m_Materials: + - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 0 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Sprite: {fileID: 21300000, guid: f0a1f29b04961544d939bb5bd18e56c2, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 10, y: 10} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 diff --git a/Assets/Prefabs/Bullet.prefab.meta b/Assets/Prefabs/Bullet.prefab.meta new file mode 100644 index 0000000..bbe56db --- /dev/null +++ b/Assets/Prefabs/Bullet.prefab.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c6c321ecc6516374781405a54c1fa6a7 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 100100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index ad2e7ee..74b4ff7 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -11,6 +11,21 @@ Prefab: m_ParentPrefab: {fileID: 0} m_RootGameObject: {fileID: 1679161327080020} m_IsPrefabParent: 1 +--- !u!1 &1136532900431144 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 100100000} + serializedVersion: 5 + m_Component: + - component: {fileID: 4676354101874032} + m_Layer: 0 + m_Name: BulletSpawn + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 --- !u!1 &1281416296700930 GameObject: m_ObjectHideFlags: 0 @@ -89,6 +104,7 @@ Transform: m_LocalScale: {x: 0.25, y: 0.25, z: 0.25} m_Children: - {fileID: 4830909199357604} + - {fileID: 4676354101874032} m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -118,6 +134,19 @@ Transform: m_Father: {fileID: 4830909199357604} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!4 &4676354101874032 +Transform: + m_ObjectHideFlags: 1 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 100100000} + m_GameObject: {fileID: 1136532900431144} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 2.23, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 4079161515917916} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!4 &4830909199357604 Transform: m_ObjectHideFlags: 1 @@ -146,7 +175,7 @@ Rigidbody2D: m_UseAutoMass: 0 m_Mass: 1 m_LinearDrag: 0 - m_AngularDrag: 0 + m_AngularDrag: 1.1 m_GravityScale: 0 m_Material: {fileID: 0} m_Interpolate: 1 @@ -207,6 +236,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 4a1319c82afce6e47b9ec0980f156e39, type: 3} m_Name: m_EditorClassIdentifier: + bulletPrefab: {fileID: 1379543051791382, guid: c6c321ecc6516374781405a54c1fa6a7, + type: 2} + bulletSpawn: {fileID: 4676354101874032} + carDamage: 5 --- !u!114 &114874769993434856 MonoBehaviour: m_ObjectHideFlags: 1 diff --git a/Assets/Scenes/CodeDEV.unity b/Assets/Scenes/CodeDEV.unity index cc4450e..f861d02 100644 --- a/Assets/Scenes/CodeDEV.unity +++ b/Assets/Scenes/CodeDEV.unity @@ -1085,7 +1085,7 @@ Camera: far clip plane: 1000 field of view: 60 orthographic: 1 - orthographic size: 10 + orthographic size: 5 m_Depth: -1 m_CullingMask: serializedVersion: 2 diff --git a/Assets/Scripts/Bullet.cs b/Assets/Scripts/Bullet.cs new file mode 100644 index 0000000..7fd2078 --- /dev/null +++ b/Assets/Scripts/Bullet.cs @@ -0,0 +1,46 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bullet : MonoBehaviour { + [SerializeField] + protected float speed = 1; + [SerializeField] + int damage = 0; + + GameObject owner; + // Use this for initialization + void Start () { + + } + + public void SetDamage(int dmg) { + damage = dmg; + } + + public void SetOwner(GameObject ow) { + owner = ow; + } + + // Update is called once per frame + void Update () { + if ( owner == null ) + return; + transform.position = transform.position + transform.localRotation * Vector3.up * speed * Time.deltaTime; + } + + void OnCollisionEnter2D(Collision2D collision) { + if ( owner == null ) + return; + Debug.Log("Collided"); + if ( collision.collider.gameObject.tag != owner.tag ) { + Debug.Log("calc"); + Mob m = collision.collider.gameObject.GetComponent(typeof(Mob)) as Mob; + if (m != null) { + m.InflictDamage(damage); + } + Destroy(this.gameObject); + } + + } +} diff --git a/Assets/Scripts/EnemyAI.cs.meta b/Assets/Scripts/Bullet.cs.meta similarity index 83% rename from Assets/Scripts/EnemyAI.cs.meta rename to Assets/Scripts/Bullet.cs.meta index 694ed9a..a0c82ee 100644 --- a/Assets/Scripts/EnemyAI.cs.meta +++ b/Assets/Scripts/Bullet.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 582bfd99a32c0ea45a7d09ef4f308d88 +guid: 0cfff064e4589e34cb5b415dcc016dc1 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scripts/EnemyAI.cs b/Assets/Scripts/EnemyAI.cs deleted file mode 100644 index 657ac60..0000000 --- a/Assets/Scripts/EnemyAI.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class EnemyAI : MonoBehaviour { - - private GameObject victim; - Rigidbody2D body; - [SerializeField] - private float speed = 1; - [SerializeField] - private float rotationSpeed = 1; - - /* - * Die destructive dumme deutsche Dungeon Diktator Drifter DLC Debakel Distribution Dokumentations - Druck Datei - */ - - // Use this for initialization - void Start () { - victim = null; - body = gameObject.GetComponent(); - } - - // Update is called once per frame - void Update () - { - if(victim == null) { - //victim = GameController.instance.GetPlayer().gameObject; // TODO testing purpose only! - return; - } - - Vector3 distanceToEnemy = victim.transform.position - gameObject.transform.position; - - // movement - body.velocity = new Vector2(distanceToEnemy.normalized.x, distanceToEnemy.normalized.y) * speed; - - //rotation - Vector3 localRotation = gameObject.transform.localRotation * Vector3.up; - float angleToRotate = Mathf.Round(Vector3.SignedAngle(localRotation, distanceToEnemy.normalized, Vector3.forward)); - gameObject.transform.Rotate(0, 0, angleToRotate * rotationSpeed); - } - - public void SetVictim(GameObject g) { - victim = g; - } -} diff --git a/Assets/Scripts/Entities/Enemy.cs b/Assets/Scripts/Entities/Enemy.cs new file mode 100644 index 0000000..c09ec00 --- /dev/null +++ b/Assets/Scripts/Entities/Enemy.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using Assets.Scripts.Entities.Attack; + +namespace Assets.Scripts.Entities { + class Enemy : Mob { + + [SerializeField] + private float speed = 1; + [SerializeField] + private float rotationSpeed = 1; + + protected GameObject victim; + private Rigidbody2D body; + + public Enemy(int mHP) : base(mHP) { + + } + + private void Start() { + body = gameObject.GetComponent(); + + } + + private float nextAttackTime; + void Update() { + if ( victim == null || attack == null ) { + return; + } + + if ( Time.timeSinceLevelLoad >= nextAttackTime ) { + RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.position - victim.transform.position, attack.GetRange()); + if ( hit.collider.gameObject == victim ) { + attack.Attack(); + nextAttackTime = Time.timeSinceLevelLoad + attack.GetCooldownTime(); + } + } + + Vector3 distanceToEnemy = victim.transform.position - gameObject.transform.position; + + if(distanceToEnemy.magnitude < attack.GetRange()) { + return; + } + + // movement + body.velocity = new Vector2(distanceToEnemy.normalized.x, distanceToEnemy.normalized.y) * speed; + + //rotation + Vector3 localRotation = gameObject.transform.localRotation * Vector3.up; + float angleToRotate = Mathf.Round(Vector3.SignedAngle(localRotation, distanceToEnemy.normalized, Vector3.forward)); + gameObject.transform.Rotate(0, 0, angleToRotate * rotationSpeed); + } + + public void SetVictim(GameObject g) { + victim = g; + } + } +} diff --git a/Assets/Scripts/Entities/Enemy.cs.meta b/Assets/Scripts/Entities/Enemy.cs.meta new file mode 100644 index 0000000..e712b1b --- /dev/null +++ b/Assets/Scripts/Entities/Enemy.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3b0dd37750beb51419e397f295f9b925 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Entities/Mob.cs b/Assets/Scripts/Entities/Mob.cs index 80752c3..e008430 100644 --- a/Assets/Scripts/Entities/Mob.cs +++ b/Assets/Scripts/Entities/Mob.cs @@ -1,8 +1,10 @@ -public abstract class Mob : Entity { - readonly int maxHP; - int currentHP; - bool isDead; - int damage; +using Assets.Scripts.Entities.Attack; + +public abstract class Mob : Entity { + protected readonly int maxHP; + protected int currentHP; + protected bool isDead; + protected IAttack attack; /// /// Creates a new Mob instance with the given HP. @@ -14,6 +16,10 @@ isDead = false; } + public void SetAttack(IAttack attack) { + this.attack = attack; + } + /// /// Inflicts damage to this mob. /// @@ -44,22 +50,6 @@ currentHP = ( currentHP + healAmount > currentHP ) ? maxHP : currentHP + healAmount; } - /// - /// Sets the damage value of a mobs attack. - /// - /// - public void SetDamage(int dmg) { - damage = dmg; - } - - /// - /// Gets the damage value og a mobs attack. - /// - /// - public int GetDamage() { - return damage; - } - /// /// Gets the current HP. /// diff --git a/Assets/Scripts/Entities/Player.cs b/Assets/Scripts/Entities/Player.cs index f551ed0..0cbaffc 100644 --- a/Assets/Scripts/Entities/Player.cs +++ b/Assets/Scripts/Entities/Player.cs @@ -1,11 +1,37 @@ -using System.Collections; -using System.Collections.Generic; +using Assets.Scripts.Entities.Attack; using UnityEngine; public class Player : Mob { + + [SerializeField] + private GameObject bulletPrefab; + [SerializeField] + Transform bulletSpawn; + [SerializeField] + private int carDamage = 5; + + private float nextAttackTime; public Player() : base(100) { } + private void Start() { + SingleShot s = new SingleShot(this.gameObject); + s.SetPrefab(bulletPrefab); + s.SetSpawn(bulletSpawn); + SetAttack(s); + } + + void Update() { + if ( Time.timeSinceLevelLoad >= nextAttackTime && attack != null) { + if ( Input.GetKey(KeyCode.Space) ) { + Debug.Log("Attack pressed!"); + attack.Attack(); + nextAttackTime = Time.timeSinceLevelLoad + attack.GetCooldownTime(); + } + } + } + + /// /// Collision checking. Player is going to die on any collision with a wall. /// @@ -17,9 +43,8 @@ public class Player : Mob { } else if ( collision.collider.tag == "enemy" ) { Mob m = collision.collider.GetComponent(typeof(Mob)) as Mob; if ( m != null ) { - InflictDamage(m.GetDamage()); // TODO think about Mob attac mechanic + InflictDamage(carDamage); } - } } diff --git a/Assets/Scripts/Entities/Scorpion.cs b/Assets/Scripts/Entities/Scorpion.cs new file mode 100644 index 0000000..daf3532 --- /dev/null +++ b/Assets/Scripts/Entities/Scorpion.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using Assets.Scripts.Entities.Attack; + +namespace Assets.Scripts.Entities { + class Scorpion : Enemy { + public Scorpion() : base(30) { + SetAttack(new MeleeAttack(this.gameObject)); + } + } +} diff --git a/Assets/Scripts/Entities/Scorpion.cs.meta b/Assets/Scripts/Entities/Scorpion.cs.meta new file mode 100644 index 0000000..459abd0 --- /dev/null +++ b/Assets/Scripts/Entities/Scorpion.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0d221474512c53044b3507bc53b9c7a8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/graphics/textures/Entities/Bullet/bullet.png.meta b/Assets/graphics/textures/Entities/Bullet/bullet.png.meta index e534af9..8fe73b5 100644 --- a/Assets/graphics/textures/Entities/Bullet/bullet.png.meta +++ b/Assets/graphics/textures/Entities/Bullet/bullet.png.meta @@ -43,7 +43,7 @@ TextureImporter: spriteMeshType: 1 alignment: 0 spritePivot: {x: 0.5, y: 0.5} - spritePixelsToUnits: 100 + spritePixelsToUnits: 1000 spriteBorder: {x: 0, y: 0, z: 0, w: 0} spriteGenerateFallbackPhysicsShape: 1 alphaUsage: 1 @@ -67,6 +67,39 @@ TextureImporter: allowsAlphaSplitting: 0 overridden: 0 androidETC2FallbackOverride: 0 + - serializedVersion: 2 + buildTarget: Standalone + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + - serializedVersion: 2 + buildTarget: Android + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + - serializedVersion: 2 + buildTarget: WebGL + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 spriteSheet: serializedVersion: 2 sprites: []