Implemented shooting

Fixed abstract character not depleting energy when reaching zero
Created prefabs for player and enemy bullets
Added physics layers for players and enemies
This commit is contained in:
trotFunky 2019-09-19 12:36:09 +02:00
parent f6529e33da
commit 42706712aa
15 changed files with 458 additions and 29 deletions

View file

@ -0,0 +1,128 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &7819471670070814049
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 7819471670070814054}
- component: {fileID: 7819471670070814053}
- component: {fileID: 7819471670070814052}
- component: {fileID: 7819471670070814051}
- component: {fileID: 7819471670070814050}
m_Layer: 9
m_Name: EnemyBullet
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &7819471670070814054
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7819471670070814049}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 3.11, y: -0.15, 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!212 &7819471670070814053
SpriteRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7819471670070814049}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 1
m_RendererPriority: 0
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_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
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: 3b4cae76f20a7b6238d5eb0109a2b8d4, type: 3}
m_Color: {r: 1, g: 0.14117648, b: 0.23921569, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 1, y: 1}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!58 &7819471670070814052
CircleCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7819471670070814049}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 0
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
serializedVersion: 2
m_Radius: 0.2
--- !u!114 &7819471670070814051
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7819471670070814049}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8d0d4226322cf15c8b3f89217d9b1c66, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &7819471670070814050
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7819471670070814049}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 88e019f69c8f40971acaa53093cfe4dd, type: 3}
m_Name:
m_EditorClassIdentifier:
_damage: 1
_speed: 5
_direction: {x: 1, y: 0}

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d562dc003000623c3a481b16e746d1a5
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -15,7 +15,7 @@ GameObject:
- component: {fileID: 2690590977808455802} - component: {fileID: 2690590977808455802}
- component: {fileID: 2690590977808455803} - component: {fileID: 2690590977808455803}
- component: {fileID: 2690590977808455796} - component: {fileID: 2690590977808455796}
m_Layer: 0 m_Layer: 8
m_Name: Player m_Name: Player
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
@ -113,7 +113,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 35183fd85dde156de82c2af47fea6156, type: 3} m_Script: {fileID: 11500000, guid: 35183fd85dde156de82c2af47fea6156, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
_maxSpeed: 10 _maxSpeed: 7
_maxHealth: 3 _maxHealth: 3
_maxEnergy: 100 _maxEnergy: 100
_energyRegen: 20 _energyRegen: 20
@ -153,3 +153,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 61bf45c54c3d922598ce965db2dc869c, type: 3} m_Script: {fileID: 11500000, guid: 61bf45c54c3d922598ce965db2dc869c, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
_energyCost: 10
_shotsPerSecond: 2
_bullet: {fileID: 4369719273071165554, guid: 5dcbc4e2919ffde8a9d89514fd293e8e, type: 3}
_direction: {x: 1, y: 0}
_offset: {x: 1, y: 0}

View file

@ -0,0 +1,128 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &4369719273071165554
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4369719273071165557}
- component: {fileID: 4369719273071165558}
- component: {fileID: 4369719273071165559}
- component: {fileID: 4369719273071165552}
- component: {fileID: 4369719273071165553}
m_Layer: 8
m_Name: PlayerBullet
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &4369719273071165557
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4369719273071165554}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 3.11, y: -0.15, 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!212 &4369719273071165558
SpriteRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4369719273071165554}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 1
m_RendererPriority: 0
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_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
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: 3b4cae76f20a7b6238d5eb0109a2b8d4, type: 3}
m_Color: {r: 0.16981131, g: 1, b: 0.2012984, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 1, y: 1}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!58 &4369719273071165559
CircleCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4369719273071165554}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 0
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
serializedVersion: 2
m_Radius: 0.2
--- !u!114 &4369719273071165552
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4369719273071165554}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8d0d4226322cf15c8b3f89217d9b1c66, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &4369719273071165553
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4369719273071165554}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 88e019f69c8f40971acaa53093cfe4dd, type: 3}
m_Name:
m_EditorClassIdentifier:
_damage: 1
_speed: 5
_direction: {x: 1, y: 0}

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 5dcbc4e2919ffde8a9d89514fd293e8e
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -270,10 +270,5 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 2690590977808455797, guid: 3ae3c84bd570eb26d86ed9ec4e5fcba0,
type: 3}
propertyPath: _maxSpeed
value: 7
objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 3ae3c84bd570eb26d86ed9ec4e5fcba0, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 3ae3c84bd570eb26d86ed9ec4e5fcba0, type: 3}

View file

@ -78,13 +78,17 @@ public abstract class AbstractCharacter : MonoBehaviour
_isDepleted = false; _isDepleted = false;
} }
} }
if (CurrentEnergy < 0) if (CurrentEnergy <= 0)
{ {
CurrentEnergy = 0; CurrentEnergy = 0;
_isDepleted = true;
} }
// The InputManager is executed before and updates it to true if needed // The InputManager is executed before and updates it to true if needed
_isShooting = false; _isShooting = false;
// FIXME : Debug drawing to show energy level
Debug.DrawLine(new Vector3(-5,4), new Vector3(-5 + 10 * CurrentEnergy/MaxEnergy,4),Color.blue);
} }
public abstract void Move(Vector2 movementDirection); public abstract void Move(Vector2 movementDirection);

View file

@ -1,18 +1,57 @@
using System.Collections; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class AbstractShooter : MonoBehaviour public abstract class AbstractShooter : MonoBehaviour
{ {
// Energy consumed per shot
[SerializeField] protected float _energyCost = 2;
public float EnergyCost => _energyCost;
// Minimum time between two shots
protected float _minDeltaTime = 1f / 5;
[SerializeField] private int _shotsPerSecond = 5;
public int ShotsPerSecond
{
get { return _shotsPerSecond; }
set
{
_shotsPerSecond = value;
_minDeltaTime = 1f / _shotsPerSecond;
}
}
private float _timeLeft;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
_minDeltaTime = 1f / ShotsPerSecond;
_timeLeft = 0;
} }
// Update is called once per frame
void Update() void Update()
{ {
_timeLeft -= Time.deltaTime;
} }
private void OnValidate()
{
_minDeltaTime = 1f / ShotsPerSecond;
}
public bool Shoot(float currentEnergy)
{
if (_timeLeft <= 0 && currentEnergy > 0)
{
InstantiateProjectiles();
_timeLeft = _minDeltaTime;
return true;
}
return false;
}
protected abstract void InstantiateProjectiles();
} }

View file

@ -4,15 +4,26 @@ using UnityEngine;
public class BasicShooter : AbstractShooter public class BasicShooter : AbstractShooter
{ {
// Start is called before the first frame update // Bullet that will be fired
[SerializeField] private GameObject _bullet;
private Transform _parentTransform;
// Direction the bullet will be going
[SerializeField] private Vector2 _direction = Vector2.right;
// Offset between parent and newly created bullet
[SerializeField] private Vector2 _offset = Vector2.zero;
void Start() void Start()
{ {
_parentTransform = GetComponentInParent<Transform>();
} }
// Update is called once per frame protected override void InstantiateProjectiles()
void Update()
{ {
Vector3 newPosition = _parentTransform.position + (Vector3)_offset;
GameObject child = Instantiate(_bullet, newPosition, Quaternion.identity);
child.GetComponent<Bullet>().Direction = _direction;
} }
} }

View file

@ -35,12 +35,6 @@ public class Bullet : MonoBehaviour
mover.MaxSpeed = Speed; mover.MaxSpeed = Speed;
} }
// Update is called once per frame
void Update()
{
}
// Destroy on Stay, collision effects should be handled on Enter // Destroy on Stay, collision effects should be handled on Enter
private void OnCollisionStay2D(Collision2D other) private void OnCollisionStay2D(Collision2D other)
{ {

View file

@ -12,8 +12,16 @@ public class PlayerCharacter : AbstractCharacter
public override void Shoot() public override void Shoot()
{ {
// TODO : Implement _isShooting = true;
throw new System.NotImplementedException(); if (!_isDepleted && _shooter.Shoot(CurrentEnergy))
{
CurrentEnergy -= _shooter.EnergyCost;
}
else if (_isDepleted)
{
// Force regen when depleted even if holding the trigger
_isShooting = false;
}
} }
protected override void Death() protected override void Death()

BIN
Assets/Sprites/Bullet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

View file

@ -0,0 +1,103 @@
fileFormatVersion: 2
guid: 3b4cae76f20a7b6238d5eb0109a2b8d4
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 10
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 0
aniso: -1
mipBias: -100
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 10
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 0
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID: 5e97eb03825dee720800000000000000
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:

View file

@ -53,4 +53,4 @@ Physics2DSettings:
m_ColliderAsleepColor: {r: 0.5686275, g: 0.95686275, b: 0.54509807, a: 0.36078432} m_ColliderAsleepColor: {r: 0.5686275, g: 0.95686275, b: 0.54509807, a: 0.36078432}
m_ColliderContactColor: {r: 1, g: 0, b: 1, a: 0.6862745} m_ColliderContactColor: {r: 1, g: 0, b: 1, a: 0.6862745}
m_ColliderAABBColor: {r: 1, g: 1, b: 0, a: 0.2509804} m_ColliderAABBColor: {r: 1, g: 1, b: 0, a: 0.2509804}
m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffdffcffffffffffffffffffffdffeffffdffdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

View file

@ -13,8 +13,8 @@ TagManager:
- UI - UI
- -
- -
- - Player
- - Enemy
- -
- -
- -