Entity uses Attack
Player shoots bullets
This commit is contained in:
parent
f03c0f4469
commit
83e54fb772
14 changed files with 374 additions and 75 deletions
117
Assets/Prefabs/Bullet.prefab
Normal file
117
Assets/Prefabs/Bullet.prefab
Normal file
|
@ -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
|
8
Assets/Prefabs/Bullet.prefab.meta
Normal file
8
Assets/Prefabs/Bullet.prefab.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c6c321ecc6516374781405a54c1fa6a7
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 100100000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
46
Assets/Scripts/Bullet.cs
Normal file
46
Assets/Scripts/Bullet.cs
Normal file
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 582bfd99a32c0ea45a7d09ef4f308d88
|
||||
guid: 0cfff064e4589e34cb5b415dcc016dc1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
|
@ -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<Rigidbody2D>();
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
59
Assets/Scripts/Entities/Enemy.cs
Normal file
59
Assets/Scripts/Entities/Enemy.cs
Normal file
|
@ -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<Rigidbody2D>();
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Entities/Enemy.cs.meta
Normal file
11
Assets/Scripts/Entities/Enemy.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3b0dd37750beb51419e397f295f9b925
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new Mob instance with the given HP.
|
||||
|
@ -14,6 +16,10 @@
|
|||
isDead = false;
|
||||
}
|
||||
|
||||
public void SetAttack(IAttack attack) {
|
||||
this.attack = attack;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inflicts damage to this mob.
|
||||
/// </summary>
|
||||
|
@ -44,22 +50,6 @@
|
|||
currentHP = ( currentHP + healAmount > currentHP ) ? maxHP : currentHP + healAmount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the damage value of a mobs attack.
|
||||
/// </summary>
|
||||
/// <param name="dmg"></param>
|
||||
public void SetDamage(int dmg) {
|
||||
damage = dmg;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the damage value og a mobs attack.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int GetDamage() {
|
||||
return damage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current HP.
|
||||
/// </summary>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Collision checking. Player is going to die on any collision with a wall.
|
||||
/// </summary>
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
12
Assets/Scripts/Entities/Scorpion.cs
Normal file
12
Assets/Scripts/Entities/Scorpion.cs
Normal file
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Entities/Scorpion.cs.meta
Normal file
11
Assets/Scripts/Entities/Scorpion.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0d221474512c53044b3507bc53b9c7a8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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: []
|
||||
|
|
Loading…
Add table
Reference in a new issue