From 32216802997d6dd52bb2ce41972d59a8b5322ca2 Mon Sep 17 00:00:00 2001 From: lucas8485 <1443937075@qq.com> Date: Thu, 21 Jul 2022 12:05:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A1=80=E7=93=B6=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Animations/HealthPotionObject.controller | 72 ++++++ .../HealthPotionObject.controller.meta | 8 + Assets/Animations/healthPotion.anim | 77 ++++++ Assets/Animations/healthPotion.anim.meta | 8 + Assets/Prefabs/HealthPotionObject.prefab | 170 ++++++++++++++ Assets/Prefabs/HealthPotionObject.prefab.meta | 7 + Assets/Scenes/MainScene.unity | 21 +- Assets/Scenes/StartupScene.unity | 2 +- Assets/Scripts/GameController.cs | 5 - Assets/Scripts/HealthController.cs | 16 +- Assets/Scripts/HealthPotionController.cs | 57 +++++ Assets/Scripts/HealthPotionController.cs.meta | 11 + Assets/Scripts/HealthPotionGenerator.cs | 80 +++++++ Assets/Scripts/HealthPotionGenerator.cs.meta | 11 + Assets/SpriteSheet/lifepotion.png | Bin 0 -> 337 bytes Assets/SpriteSheet/lifepotion.png.meta | 222 ++++++++++++++++++ ProjectSettings/ProjectSettings.asset | 2 +- ProjectSettings/TagManager.asset | 1 + 18 files changed, 761 insertions(+), 9 deletions(-) create mode 100644 Assets/Animations/HealthPotionObject.controller create mode 100644 Assets/Animations/HealthPotionObject.controller.meta create mode 100644 Assets/Animations/healthPotion.anim create mode 100644 Assets/Animations/healthPotion.anim.meta create mode 100644 Assets/Prefabs/HealthPotionObject.prefab create mode 100644 Assets/Prefabs/HealthPotionObject.prefab.meta create mode 100644 Assets/Scripts/HealthPotionController.cs create mode 100644 Assets/Scripts/HealthPotionController.cs.meta create mode 100644 Assets/Scripts/HealthPotionGenerator.cs create mode 100644 Assets/Scripts/HealthPotionGenerator.cs.meta create mode 100644 Assets/SpriteSheet/lifepotion.png create mode 100644 Assets/SpriteSheet/lifepotion.png.meta diff --git a/Assets/Animations/HealthPotionObject.controller b/Assets/Animations/HealthPotionObject.controller new file mode 100644 index 0000000..eb91add --- /dev/null +++ b/Assets/Animations/HealthPotionObject.controller @@ -0,0 +1,72 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1107 &-1046805334037432806 +AnimatorStateMachine: + serializedVersion: 6 + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: Base Layer + m_ChildStates: + - serializedVersion: 1 + m_State: {fileID: 3868620684800939641} + m_Position: {x: 220, y: 260, z: 0} + m_ChildStateMachines: [] + m_AnyStateTransitions: [] + m_EntryTransitions: [] + m_StateMachineTransitions: {} + m_StateMachineBehaviours: [] + m_AnyStatePosition: {x: 50, y: 20, z: 0} + m_EntryPosition: {x: 50, y: 120, z: 0} + m_ExitPosition: {x: 800, y: 120, z: 0} + m_ParentStateMachinePosition: {x: 800, y: 20, z: 0} + m_DefaultState: {fileID: 3868620684800939641} +--- !u!91 &9100000 +AnimatorController: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: HealthPotionObject + serializedVersion: 5 + m_AnimatorParameters: [] + m_AnimatorLayers: + - serializedVersion: 5 + m_Name: Base Layer + m_StateMachine: {fileID: -1046805334037432806} + m_Mask: {fileID: 0} + m_Motions: [] + m_Behaviours: [] + m_BlendingMode: 0 + m_SyncedLayerIndex: -1 + m_DefaultWeight: 0 + m_IKPass: 0 + m_SyncedLayerAffectsTiming: 0 + m_Controller: {fileID: 9100000} +--- !u!1102 &3868620684800939641 +AnimatorState: + serializedVersion: 6 + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: healthPotion + m_Speed: 0.2 + m_CycleOffset: 0 + m_Transitions: [] + m_StateMachineBehaviours: [] + m_Position: {x: 50, y: 50, z: 0} + m_IKOnFeet: 0 + m_WriteDefaultValues: 1 + m_Mirror: 0 + m_SpeedParameterActive: 0 + m_MirrorParameterActive: 0 + m_CycleOffsetParameterActive: 0 + m_TimeParameterActive: 0 + m_Motion: {fileID: 7400000, guid: 462d63b4fd5e4aa45a4e4b4a3eff0368, type: 2} + m_Tag: + m_SpeedParameter: + m_MirrorParameter: + m_CycleOffsetParameter: + m_TimeParameter: diff --git a/Assets/Animations/HealthPotionObject.controller.meta b/Assets/Animations/HealthPotionObject.controller.meta new file mode 100644 index 0000000..facad5e --- /dev/null +++ b/Assets/Animations/HealthPotionObject.controller.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 65421b7b09d951649997902a13db2b95 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 9100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Animations/healthPotion.anim b/Assets/Animations/healthPotion.anim new file mode 100644 index 0000000..80726e0 --- /dev/null +++ b/Assets/Animations/healthPotion.anim @@ -0,0 +1,77 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!74 &7400000 +AnimationClip: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: healthPotion + serializedVersion: 7 + m_Legacy: 0 + m_Compressed: 0 + m_UseHighQualityCurve: 1 + m_RotationCurves: [] + m_CompressedRotationCurves: [] + m_EulerCurves: [] + m_PositionCurves: [] + m_ScaleCurves: [] + m_FloatCurves: [] + m_PPtrCurves: + - curve: + - time: 0 + value: {fileID: 1866539434, guid: 166d1b9250d8abd4f81557cd6c833c08, type: 3} + - time: 0.083333336 + value: {fileID: 2137247547, guid: 166d1b9250d8abd4f81557cd6c833c08, type: 3} + - time: 0.16666667 + value: {fileID: -1244690013, guid: 166d1b9250d8abd4f81557cd6c833c08, type: 3} + - time: 0.25 + value: {fileID: -175261417, guid: 166d1b9250d8abd4f81557cd6c833c08, type: 3} + attribute: m_Sprite + path: + classID: 212 + script: {fileID: 0} + m_SampleRate: 12 + m_WrapMode: 0 + m_Bounds: + m_Center: {x: 0, y: 0, z: 0} + m_Extent: {x: 0, y: 0, z: 0} + m_ClipBindingConstant: + genericBindings: + - serializedVersion: 2 + path: 0 + attribute: 0 + script: {fileID: 0} + typeID: 212 + customType: 23 + isPPtrCurve: 1 + pptrCurveMapping: + - {fileID: 1866539434, guid: 166d1b9250d8abd4f81557cd6c833c08, type: 3} + - {fileID: 2137247547, guid: 166d1b9250d8abd4f81557cd6c833c08, type: 3} + - {fileID: -1244690013, guid: 166d1b9250d8abd4f81557cd6c833c08, type: 3} + - {fileID: -175261417, guid: 166d1b9250d8abd4f81557cd6c833c08, type: 3} + m_AnimationClipSettings: + serializedVersion: 2 + m_AdditiveReferencePoseClip: {fileID: 0} + m_AdditiveReferencePoseTime: 0 + m_StartTime: 0 + m_StopTime: 0.33333334 + m_OrientationOffsetY: 0 + m_Level: 0 + m_CycleOffset: 0 + m_HasAdditiveReferencePose: 0 + m_LoopTime: 1 + m_LoopBlend: 0 + m_LoopBlendOrientation: 0 + m_LoopBlendPositionY: 0 + m_LoopBlendPositionXZ: 0 + m_KeepOriginalOrientation: 0 + m_KeepOriginalPositionY: 1 + m_KeepOriginalPositionXZ: 0 + m_HeightFromFeet: 0 + m_Mirror: 0 + m_EditorCurves: [] + m_EulerEditorCurves: [] + m_HasGenericRootTransform: 0 + m_HasMotionFloatCurves: 0 + m_Events: [] diff --git a/Assets/Animations/healthPotion.anim.meta b/Assets/Animations/healthPotion.anim.meta new file mode 100644 index 0000000..dbca331 --- /dev/null +++ b/Assets/Animations/healthPotion.anim.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 462d63b4fd5e4aa45a4e4b4a3eff0368 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 7400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/HealthPotionObject.prefab b/Assets/Prefabs/HealthPotionObject.prefab new file mode 100644 index 0000000..e2a3a63 --- /dev/null +++ b/Assets/Prefabs/HealthPotionObject.prefab @@ -0,0 +1,170 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &937080767928470955 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 937080767928470958} + - component: {fileID: 937080767928470957} + - component: {fileID: 937080767928470956} + - component: {fileID: 3322718677524556424} + - component: {fileID: 6324490572315656674} + - component: {fileID: 8620234402615681426} + m_Layer: 6 + m_Name: HealthPotionObject + m_TagString: HealthPotion + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &937080767928470958 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 937080767928470955} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -0.709, y: -4.5, z: 3.8876367} + m_LocalScale: {x: 5, y: 5, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!212 &937080767928470957 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 937080767928470955} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + 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: -875790119 + m_SortingLayer: 2 + m_SortingOrder: 0 + m_Sprite: {fileID: 1866539434, guid: 166d1b9250d8abd4f81557cd6c833c08, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 0.12, y: 0.16} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 +--- !u!95 &937080767928470956 +Animator: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 937080767928470955} + m_Enabled: 1 + m_Avatar: {fileID: 0} + m_Controller: {fileID: 9100000, guid: 65421b7b09d951649997902a13db2b95, type: 2} + m_CullingMode: 0 + m_UpdateMode: 0 + m_ApplyRootMotion: 0 + m_LinearVelocityBlending: 0 + m_StabilizeFeet: 0 + m_WarningMessage: + m_HasTransformHierarchy: 1 + m_AllowConstantClipSamplingOptimization: 1 + m_KeepAnimatorControllerStateOnDisable: 0 +--- !u!50 &3322718677524556424 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 937080767928470955} + m_BodyType: 1 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 0 + m_Mass: 1 + m_LinearDrag: 0 + m_AngularDrag: 0.05 + m_GravityScale: 1 + m_Material: {fileID: 0} + m_Interpolate: 0 + m_SleepingMode: 1 + m_CollisionDetection: 0 + m_Constraints: 4 +--- !u!61 &6324490572315656674 +BoxCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 937080767928470955} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 1 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: -0.06, y: 0.08} + m_SpriteTilingProperty: + border: {x: 0, y: 0, z: 0, w: 0} + pivot: {x: 1, y: 0} + oldSize: {x: 0.12, y: 0.16} + newSize: {x: 0.12, y: 0.16} + adaptiveTilingThreshold: 0.5 + drawMode: 0 + adaptiveTiling: 0 + m_AutoTiling: 0 + serializedVersion: 2 + m_Size: {x: 0.12, y: 0.16} + m_EdgeRadius: 0 +--- !u!114 &8620234402615681426 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 937080767928470955} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 2aec4a80b48fde647b0de0ff6893a188, type: 3} + m_Name: + m_EditorClassIdentifier: + xSpeed: -4.85 diff --git a/Assets/Prefabs/HealthPotionObject.prefab.meta b/Assets/Prefabs/HealthPotionObject.prefab.meta new file mode 100644 index 0000000..c2ff91d --- /dev/null +++ b/Assets/Prefabs/HealthPotionObject.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 9bc9828de06ec594088c2a1de215576f +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index bada86f..015e160 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -386,10 +386,11 @@ GameObject: - component: {fileID: 1715724608} - component: {fileID: 1715724610} - component: {fileID: 1715724612} + - component: {fileID: 1715724613} - component: {fileID: 1715724611} m_Layer: 0 m_Name: GameController - m_TagString: Untagged + m_TagString: GameController m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 @@ -551,6 +552,24 @@ MonoBehaviour: initialPosition: 3 fixedDistance: 5 floatingDistance: 5.5 +--- !u!114 &1715724613 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1715724607} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: dba099aca731d96499f66fdb77e94438, type: 3} + m_Name: + m_EditorClassIdentifier: + generationProbability: 0.1 + initialDistance: 10 + potionXSpeed: -4.85 + potionYOffset: 0 + healthPotionPrefab: {fileID: 937080767928470955, guid: 9bc9828de06ec594088c2a1de215576f, type: 3} + spawnTimeInterval: 6 --- !u!1 &2138531883 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/StartupScene.unity b/Assets/Scenes/StartupScene.unity index 5e86977..89fa158 100644 --- a/Assets/Scenes/StartupScene.unity +++ b/Assets/Scenes/StartupScene.unity @@ -807,7 +807,7 @@ MonoBehaviour: m_HorizontalOverflow: 0 m_VerticalOverflow: 0 m_LineSpacing: 1 - m_Text: Version 3.0 By Lucas Created With Unity + m_Text: Version 3.1 By Lucas Created With Unity --- !u!222 &1714768157 CanvasRenderer: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/GameController.cs b/Assets/Scripts/GameController.cs index 6d4398f..8ad26c2 100644 --- a/Assets/Scripts/GameController.cs +++ b/Assets/Scripts/GameController.cs @@ -33,11 +33,6 @@ public class GameController : MonoBehaviour private Text distanceText; private float initialPlayerPosition; - void Awake() - { - Screen.orientation = ScreenOrientation.LandscapeRight; - } - // Start is called before the first frame update void Start() { diff --git a/Assets/Scripts/HealthController.cs b/Assets/Scripts/HealthController.cs index ad34f97..51181fc 100644 --- a/Assets/Scripts/HealthController.cs +++ b/Assets/Scripts/HealthController.cs @@ -46,8 +46,14 @@ public class HealthController : MonoBehaviour public void IncreaseHealth() { - if (health < initialHealth) + if (health < initialHealth) { controller.SetHealth(++health); + if (health == initialHealth) + { + GameObject gameController = GameObject.FindGameObjectWithTag("GameController"); + gameController.GetComponent().DisNotifyGenerate(); + } + } } public void DecreaseHealth() @@ -56,6 +62,14 @@ public class HealthController : MonoBehaviour if (health <= 0) { SceneManager.LoadScene("StartupScene"); + return; } + GameObject gameController = GameObject.FindGameObjectWithTag("GameController"); + gameController.GetComponent().NotifyGenerate(gameObject); + } + + public bool NeedPotion + { + get => health < initialHealth; } } diff --git a/Assets/Scripts/HealthPotionController.cs b/Assets/Scripts/HealthPotionController.cs new file mode 100644 index 0000000..1b6bee8 --- /dev/null +++ b/Assets/Scripts/HealthPotionController.cs @@ -0,0 +1,57 @@ +/* + zlib License + + 跳出一方天地 (C) 2022 Lucas + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1.The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class HealthPotionController : MonoBehaviour +{ + private float leftBound; + public float xSpeed; + Rigidbody2D rb2d; + // Start is called before the first frame update + void Start() + { + rb2d = GetComponent(); + rb2d.velocity = new Vector2(xSpeed, 0); + leftBound = Camera.main.ViewportToWorldPoint(new Vector2(0, 1)).x; + } + + // Update is called once per frame + void Update() + { + if (rb2d.transform.position.x < leftBound) + { + gameObject.SetActive(false); + } + } + + private void OnTriggerEnter2D(Collider2D collision) + { + if (collision.gameObject.CompareTag("Player")) + { + HealthController healthController = collision.gameObject.GetComponent(); + healthController.IncreaseHealth(); + gameObject.SetActive(false); + } + } +} diff --git a/Assets/Scripts/HealthPotionController.cs.meta b/Assets/Scripts/HealthPotionController.cs.meta new file mode 100644 index 0000000..db2a474 --- /dev/null +++ b/Assets/Scripts/HealthPotionController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2aec4a80b48fde647b0de0ff6893a188 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/HealthPotionGenerator.cs b/Assets/Scripts/HealthPotionGenerator.cs new file mode 100644 index 0000000..4ae34c6 --- /dev/null +++ b/Assets/Scripts/HealthPotionGenerator.cs @@ -0,0 +1,80 @@ +/* + zlib License + + 跳出一方天地 (C) 2022 Lucas + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1.The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class HealthPotionGenerator : MonoBehaviour +{ + private float rightBound; + public float generationProbability; + public float initialDistance; + public float potionXSpeed; + public float potionYOffset; + public GameObject healthPotionPrefab; + private GameObject healthPotion; + Coroutine generatePotionCoroutine; + public float spawnTimeInterval; + // Start is called before the first frame update + void Start() + { + healthPotion = Instantiate(healthPotionPrefab); + healthPotion.SetActive(false); + rightBound = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)).x; + } + + public void NotifyGenerate(GameObject player) + { + HealthController healthController = player.GetComponent(); + if (healthController.NeedPotion) + { + if (generatePotionCoroutine == null) + generatePotionCoroutine = StartCoroutine(ConstantGenerate()); + } + } + + public void DisNotifyGenerate() + { + if (generatePotionCoroutine != null) + { + StopCoroutine(generatePotionCoroutine); + generatePotionCoroutine = null; + } + healthPotion.SetActive(false); + } + + IEnumerator ConstantGenerate() + { + while (true) + { + bool needGenerate = Random.value < generationProbability; + if (needGenerate && !healthPotion.activeSelf) + { + healthPotion.transform.position = new Vector3(rightBound + initialDistance, + potionYOffset); + healthPotion.SetActive(true); + healthPotion.GetComponent().velocity = new Vector2(potionXSpeed, 0); + } + yield return new WaitForSeconds(spawnTimeInterval); + } + } +} diff --git a/Assets/Scripts/HealthPotionGenerator.cs.meta b/Assets/Scripts/HealthPotionGenerator.cs.meta new file mode 100644 index 0000000..204451d --- /dev/null +++ b/Assets/Scripts/HealthPotionGenerator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dba099aca731d96499f66fdb77e94438 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/SpriteSheet/lifepotion.png b/Assets/SpriteSheet/lifepotion.png new file mode 100644 index 0000000000000000000000000000000000000000..b064a3de65be9cdb6c5d43f9b9cd7f1e737e1d82 GIT binary patch literal 337 zcmV-X0j~auP)tUy zP|6P^{dk!}%?zx80rP~tz7p88iCLu#qz1@PG=mvEYLPv0@ZvqVw4tdr!~gH^8ZKVF z(FhmB!5FG$pacdkPr&?4On?k!GvI9pkSFZjI${3COD6;{F)UoU0j?LOc&M9!wakEd zZrIQ-XCl0U%tnZUStBDb#M2SpMrI>K2YF!Nia}iBXdVM|ajC^2K6q0Gt{B86jx8{7 zsl_1<4-8O8?BK3)Y(}Dp;VL{qiZIMTjsYb0U^RoJ8UddtkV6b#tumO+zz7VGr*O44 ju~nzYfq={&ie>-+eF!+9n)hQr00000NkvXXu0mjfd