龙腾猫跃 4 лет назад
Родитель
Сommit
d38d1754d5

+ 1 - 1
Assets/Resources/Entities/EnemyController.cs

@@ -5,7 +5,7 @@ using UnityEngine;
 public class EnemyController : EntityBase {
     
     internal override bool isPressingCrouch() => false;
-    internal override bool isPressingJump() => false;
+    internal override bool isPressDownJump() => false;
     internal override bool isPressingLeft() => false;
     internal override bool isPressingRight() => true;
 

+ 19 - 10
Assets/Resources/Entities/EntityBase.cs

@@ -15,8 +15,10 @@ public abstract class EntityBase : MonoBehaviour {
         coll = GetComponent<BoxCollider2D>();
     }
 
+    public bool canCrouch = false;
     public float movementSpeed = 10f, jumpForce = 20f, horizontalF = 0.9f, horizontalA = 0.3f, jumpThreshold = 0.1f, downA = 0.3f, fallA = 0.1f;
     public float jumpBufferTime = 0.2f, jumpWolfTime = 0.2f; internal float jumpBuffer = 0f, jumpWolf = 0f, jumping = 0f;
+    private bool towardsRight = true;
     void Update() {
         // 获取当前情况
         float speedX = rb.velocity.x, speedY = rb.velocity.y;
@@ -27,7 +29,7 @@ public abstract class EntityBase : MonoBehaviour {
             (isPressingLeft() ? -1 : 1)); // 获取方向
         // 判断跳跃
         jumpBuffer -= Time.deltaTime; jumpWolf -= Time.deltaTime; jumping -= Time.deltaTime;
-        if (isPressingJump()) jumpBuffer = jumpBufferTime;
+        if (isPressDownJump()) jumpBuffer = jumpBufferTime;
         if (isLand) jumpWolf = jumpWolfTime;
         if (!isCrouch && jumpBuffer > 0 && jumpWolf > 0 && jumping < 0) { // 在地面、没有蹲下
             speedY = jumpForce;
@@ -45,21 +47,28 @@ public abstract class EntityBase : MonoBehaviour {
         }
         speedX *= horizontalF;
         // 蹲下
-        if (isCrouch) {
-            sprite.sprite = spriteDown;
-            coll.size = new Vector2(coll.size.x, 0.4f);
-            coll.offset = new Vector2(0, 0.3f);
-        } else {
-            sprite.sprite = spriteNormal;
-            coll.size = new Vector2(coll.size.x, 1.2f);
-            coll.offset = new Vector2(0, 0.7f);
+        if (canCrouch) {
+            if (isCrouch) {
+                sprite.sprite = spriteDown;
+                coll.size = new Vector2(coll.size.x, 0.4f);
+                coll.offset = new Vector2(0, 0.3f);
+            } else {
+                sprite.sprite = spriteNormal;
+                coll.size = new Vector2(coll.size.x, 1.2f);
+                coll.offset = new Vector2(0, 0.7f);
+            }
         }
         // 设置
         rb.velocity = new Vector2(speedX, speedY);
+        if (towardsRight != (Mathf.Abs(speedX) > 0.01 ? speedX > 0 : towardsRight)) {
+            // 反向
+            towardsRight = !towardsRight;
+            transform.DOScaleX(towardsRight ? 1 : -1, 0.1f);
+        }
     }
     internal abstract bool isPressingLeft();
     internal abstract bool isPressingRight();
-    internal abstract bool isPressingJump();
+    internal abstract bool isPressDownJump();
     internal abstract bool isPressingCrouch();
 
     // 碰撞检测

BIN
Assets/Resources/Entities/Lamp.png


+ 2 - 1
Assets/Resources/Entities/PlayerController.cs

@@ -8,7 +8,7 @@ public class PlayerController : EntityBase {
 
     public string keyLeft = "a", keyRight = "d", keyJump = "w", keyDown = "s";
     internal override bool isPressingCrouch() => Input.GetKey(keyDown);
-    internal override bool isPressingJump() => Input.GetKey(keyJump);
+    internal override bool isPressDownJump() => Input.GetKeyDown(keyJump);
     internal override bool isPressingLeft() => Input.GetKey(keyLeft);
     internal override bool isPressingRight() => Input.GetKey(keyRight);
 
@@ -24,6 +24,7 @@ public class PlayerController : EntityBase {
     }
     public void Hurt() {
         rb.velocity = Vector2.zero;
+        Camera.current.DOShakePosition(0.3f, 0.3f, 20);
     }
 
 }

BIN
Assets/Resources/Map/Background.png


+ 98 - 0
Assets/Resources/Map/Background.png.meta

@@ -0,0 +1,98 @@
+fileFormatVersion: 2
+guid: 3cc65945c8c7b234180eb44480465276
+TextureImporter:
+  internalIDToNameTable: []
+  externalObjects: {}
+  serializedVersion: 11
+  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
+  vTOnly: 0
+  ignoreMasterTextureLimit: 0
+  grayScaleToAlpha: 0
+  generateCubemap: 6
+  cubemapConvolution: 0
+  seamlessCubemap: 0
+  textureFormat: 1
+  maxTextureSize: 2048
+  textureSettings:
+    serializedVersion: 2
+    filterMode: 1
+    aniso: 1
+    mipBias: 0
+    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: 100
+  spriteBorder: {x: 0, y: 0, z: 0, w: 0}
+  spriteGenerateFallbackPhysicsShape: 1
+  alphaUsage: 1
+  alphaIsTransparency: 1
+  spriteTessellationDetail: -1
+  textureType: 8
+  textureShape: 1
+  singleChannelComponent: 0
+  flipbookRows: 1
+  flipbookColumns: 1
+  maxTextureSizeSet: 0
+  compressionQualitySet: 0
+  textureFormatSet: 0
+  ignorePngGamma: 0
+  applyGammaDecoding: 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
+  spriteSheet:
+    serializedVersion: 2
+    sprites: []
+    outline: []
+    physicsShape: []
+    bones: []
+    spriteID: 5e97eb03825dee720800000000000000
+    internalID: 0
+    vertices: []
+    indices: 
+    edges: []
+    weights: []
+    secondaryTextures: []
+    nameFileIdTable: {}
+  spritePackingTag: 
+  pSDRemoveMatte: 0
+  pSDShowRemoveMatteOption: 0
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 84 - 0
Assets/Scenes/SceneMain.unity

@@ -123,6 +123,90 @@ NavMeshSettings:
     debug:
       m_Flags: 0
   m_NavMeshData: {fileID: 0}
+--- !u!1 &252020815
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 252020817}
+  - component: {fileID: 252020816}
+  m_Layer: 0
+  m_Name: Background
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 1
+--- !u!212 &252020816
+SpriteRenderer:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 252020815}
+  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: 0
+  m_SortingLayer: 0
+  m_SortingOrder: -1000
+  m_Sprite: {fileID: 21300000, guid: 3cc65945c8c7b234180eb44480465276, 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.24, y: 5.12}
+  m_AdaptiveModeThreshold: 0.5
+  m_SpriteTileMode: 0
+  m_WasSpriteAssigned: 1
+  m_MaskInteraction: 0
+  m_SpriteSortPoint: 0
+--- !u!4 &252020817
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 252020815}
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
+  m_LocalScale: {x: 4.5, y: 4.5, z: 1}
+  m_ConstrainProportionsScale: 0
+  m_Children: []
+  m_Father: {fileID: 0}
+  m_RootOrder: 5
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
 --- !u!1 &557375725
 GameObject:
   m_ObjectHideFlags: 0