diff --git a/prefab/role/Player.tscn b/prefab/role/Player.tscn index 06f381b..0389b0f 100644 --- a/prefab/role/Player.tscn +++ b/prefab/role/Player.tscn @@ -5,5 +5,9 @@ [ext_resource path="res://prefab/weapon/Gun.tscn" type="PackedScene" id=4] [node name="Player" instance=ExtResource( 1 )] +collision_layer = 8 script = ExtResource( 2 ) GunPrefab = ExtResource( 4 ) + +[node name="AnimatedSprite" parent="." index="0"] +frame = 0 diff --git a/prefab/role/Role.tscn b/prefab/role/Role.tscn index 58b6905..1c7e387 100644 --- a/prefab/role/Role.tscn +++ b/prefab/role/Role.tscn @@ -85,7 +85,7 @@ position = Vector2( 0, -12 ) frames = SubResource( 6 ) animation = "idle" -frame = 1 +frame = 3 playing = true [node name="HitArea" type="Area2D" parent="."] diff --git a/project.godot b/project.godot index f339797..b259443 100644 --- a/project.godot +++ b/project.godot @@ -25,6 +25,7 @@ window/size/test_width=1920 window/size/test_height=1080 window/dpi/allow_hidpi=true +window/vsync/vsync_via_compositor=true window/stretch/aspect="keep_width" window/stretch/shrink=4.0 diff --git a/resource/materlal/Shadow.gdshader b/resource/materlal/Shadow.gdshader new file mode 100644 index 0000000..1803dfd --- /dev/null +++ b/resource/materlal/Shadow.gdshader @@ -0,0 +1,11 @@ +shader_type canvas_item; + +uniform vec4 shadowColor : hint_color = vec4(0.0, 0.0, 0.0, 0.8); + +//将贴图渲染为阴影 + +void fragment() { + float a = texture(TEXTURE, UV).a; + + COLOR = mix(vec4(0.0, 0.0, 0.0, 0.0), shadowColor, a); +} diff --git a/resource/materlal/Shadow.tres b/resource/materlal/Shadow.tres new file mode 100644 index 0000000..4110580 --- /dev/null +++ b/resource/materlal/Shadow.tres @@ -0,0 +1,7 @@ +[gd_resource type="ShaderMaterial" load_steps=2 format=2] + +[ext_resource path="res://resource/materlal/Shadow.gdshader" type="Shader" id=1] + +[resource] +shader = ExtResource( 1 ) +shader_param/shadowColor = Color( 0, 0, 0, 0.8 ) diff --git a/resource/sprite/effect/SmallShadow.png b/resource/sprite/effect/SmallShadow.png new file mode 100644 index 0000000..04d362a --- /dev/null +++ b/resource/sprite/effect/SmallShadow.png Binary files differ diff --git a/resource/sprite/effect/SmallShadow.png.import b/resource/sprite/effect/SmallShadow.png.import new file mode 100644 index 0000000..e9c3943 --- /dev/null +++ b/resource/sprite/effect/SmallShadow.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/SmallShadow.png-8aae30cd42094d71e55e4d7fa6391d71.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://resource/sprite/effect/SmallShadow.png" +dest_files=[ "res://.import/SmallShadow.png-8aae30cd42094d71e55e4d7fa6391d71.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=false +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=false +svg/scale=1.0 diff --git a/resource/sprite/shell/shellCase.aseprite b/resource/sprite/shell/shellCase.aseprite index b24b3c9..fe6c181 100644 --- a/resource/sprite/shell/shellCase.aseprite +++ b/resource/sprite/shell/shellCase.aseprite Binary files differ diff --git a/resource/sprite/shell/shellCase.png b/resource/sprite/shell/shellCase.png index c6db56d..7a926c6 100644 --- a/resource/sprite/shell/shellCase.png +++ b/resource/sprite/shell/shellCase.png Binary files differ diff --git a/scene/Room.tscn b/scene/Room.tscn index 7223c4d..64eb6c2 100644 --- a/scene/Room.tscn +++ b/scene/Room.tscn @@ -11,6 +11,7 @@ UIPath = NodePath("") [node name="MapRoot" type="Node2D" parent="."] +z_index = -10 [node name="dungeon_test" parent="MapRoot" instance=ExtResource( 2 )] diff --git a/src/common/ResourceManager.cs b/src/common/ResourceManager.cs new file mode 100644 index 0000000..eb7eb3d --- /dev/null +++ b/src/common/ResourceManager.cs @@ -0,0 +1,22 @@ +using Godot; + +public static class ResourceManager +{ + + /// + /// 2D阴影的材质 + /// + public static ShaderMaterial ShadowMaterial + { + get + { + if (_shadowMaterial == null) + { + _shadowMaterial = ResourceLoader.Load("res://resource/materlal/Shadow.tres"); + } + return _shadowMaterial; + } + } + private static ShaderMaterial _shadowMaterial; + +} \ No newline at end of file diff --git a/src/common/ThrowNode.cs b/src/common/ThrowNode.cs new file mode 100644 index 0000000..2d22955 --- /dev/null +++ b/src/common/ThrowNode.cs @@ -0,0 +1,183 @@ +using System; +using Godot; + +/// +/// 模拟抛出的物体, 使用时将对象挂载到该节点上即可 +/// +public class ThrowNode : KinematicBody2D +{ + /// + /// 是否已经结束 + /// + /// + public bool IsOver { get; protected set; } = true; + /// + /// 物体大小 + /// + public Vector2 Size { get; protected set; } + /// + /// 起始坐标 + /// + public Vector2 StartPosition { get; protected set; } + /// + /// 移动方向, 0 - 360 + /// + /// + public float Direction { get; protected set; } + /// + /// x速度, 也就是水平速度 + /// + public float XSpeed { get; protected set; } + /// + /// y轴速度, 也就是竖直速度 + /// + /// + public float YSpeed { get; protected set; } + /// + /// 初始x轴组队 + /// + /// + public float StartXSpeed { get; protected set; } + /// + /// 初始y轴速度 + /// + public float StartYSpeed { get; protected set; } + /// + /// 旋转速度 + /// + public float RotateSpeed { get; protected set; } + /// + /// 挂载的对象 + /// + public Node2D Mount { get; protected set; } + /// + /// 碰撞组件 + /// + /// + public CollisionShape2D CollisionShape { get; protected set; } + /// + /// 绘制阴影的精灵 + /// + public Sprite ShadowSprite { get; protected set; } + + public override void _Ready() + { + //只与墙壁碰撞 + CollisionMask = 1; + CollisionLayer = 0; + //创建碰撞器 + CollisionShape = new CollisionShape2D(); + var shape = new RectangleShape2D(); + shape.Extents = Size * 0.5f; + CollisionShape.Shape = shape; + AddChild(CollisionShape); + + + } + + /// + /// 初始化该抛物线对象的基础数据 + /// + /// 抛射的物体所占大小, 用于碰撞检测 + /// 起始点 + /// 起始高度 + /// 角度, 0 - 360 + /// 横轴速度 + /// 纵轴速度 + /// 旋转速度 + /// 需要挂载的节点 + /// 抛射的节点显示的纹理, 用于渲染阴影用 + public void InitThrow(Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate, Node2D mount) + { + IsOver = false; + Size = size; + GlobalPosition = StartPosition = start; + Direction = direction; + XSpeed = xSpeed; + YSpeed = ySpeed; + StartXSpeed = xSpeed; + StartYSpeed = ySpeed; + RotateSpeed = rotate; + + if (mount != null) + { + Mount = mount; + AddChild(mount); + mount.Position = new Vector2(0, -startHeight); + } + } + + /// + /// 初始化该抛物线对象的基础数据, 并且渲染阴影 + /// + /// 抛射的物体所占大小, 用于碰撞检测 + /// 起始点 + /// 起始高度 + /// 角度, 0 - 360 + /// 横轴速度 + /// 纵轴速度 + /// 旋转速度 + /// 需要挂载的节点 + /// 抛射的节点显示的纹理, 用于渲染阴影用 + public void InitThrow(Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate, Node2D mount, Texture texutre) + { + InitThrow(size, start, startHeight, direction, xSpeed, ySpeed, rotate, mount); + if (texutre != null) + { + if (ShadowSprite == null) + { + //阴影 + ShadowSprite = new Sprite(); + ShadowSprite.ZIndex = -1; + ShadowSprite.Material = ResourceManager.ShadowMaterial; + ShadowSprite.Position = new Vector2(0, 1); + AddChild(ShadowSprite); + } + ShadowSprite.Texture = texutre; + } + else if (ShadowSprite != null) + { + ShadowSprite.Texture = null; + } + } + + /// + /// 初始化时调用 + /// + protected virtual void OnInit() + { + + } + + /// + /// 结束的调用 + /// + protected virtual void OnOver() + { + + } + + public override void _Process(float delta) + { + if (!IsOver) + { + MoveAndSlide(new Vector2(XSpeed, 0).Rotated(Direction * Mathf.Pi / 180)); + Mount.Position = new Vector2(0, Mount.Position.y - YSpeed * delta); + var rotate = Mount.GlobalRotationDegrees + RotateSpeed * delta; + Mount.GlobalRotationDegrees = rotate; + if (ShadowSprite != null) + { + ShadowSprite.GlobalRotationDegrees = rotate; + } + YSpeed -= GameConfig.G * delta; + + if (Mount.Position.y >= 0) + { + Mount.Position = new Vector2(0, 0); + IsOver = true; + OnOver(); + } + } + } + +} \ No newline at end of file diff --git a/src/room/RoomManager.cs b/src/room/RoomManager.cs index f27f508..a7433ac 100644 --- a/src/room/RoomManager.cs +++ b/src/room/RoomManager.cs @@ -35,11 +35,6 @@ } - public override void _Draw() - { - DrawLine(new Vector2(100, 100), new Vector2(200, 200), Colors.White); - } - public override void _Process(float delta) { if (Input.IsActionJustPressed("fire")) diff --git a/src/weapon/ThrowNode.cs b/src/weapon/ThrowNode.cs deleted file mode 100644 index bc69dc6..0000000 --- a/src/weapon/ThrowNode.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using Godot; - -/// -/// 模拟抛出的物体, 使用时将对象挂载到该节点上即可 -/// -public class ThrowNode : Node2D -{ - public bool IsOver { get; protected set; } = true; - - public Vector2 StartPosition { get; protected set; } - public float Direction { get; protected set; } - public float XForce { get; protected set; } - public float YForce { get; protected set; } - public float RotateSpeed { get; protected set; } - public Node2D Mount { get; protected set; } - - public void InitThrow(Vector2 start, float startHeight, float direction, float xForce, float yForce, float rotate, Node2D mount) - { - IsOver = false; - GlobalPosition = StartPosition = start; - Direction = direction; - XForce = xForce; - YForce = -yForce; - RotateSpeed = rotate; - - Mount = mount; - AddChild(mount); - mount.Position = new Vector2(0, -startHeight); - } - - protected virtual void OnOver() - { - - } - - public override void _Process(float delta) - { - if (!IsOver) - { - Position += new Vector2(XForce * delta, 0).Rotated(Direction * Mathf.Pi / 180); - Mount.Position = new Vector2(0, Mount.Position.y + YForce * delta); - Mount.GlobalRotationDegrees += RotateSpeed * delta; - YForce += GameConfig.G * delta; - - if (Mount.Position.y >= 0) - { - Mount.Position = new Vector2(0, 0); - IsOver = true; - } - } - } - -} \ No newline at end of file diff --git a/src/weapon/bullet/OrdinaryBullets.cs b/src/weapon/bullet/OrdinaryBullets.cs index cefd904..7d1e121 100644 --- a/src/weapon/bullet/OrdinaryBullets.cs +++ b/src/weapon/bullet/OrdinaryBullets.cs @@ -28,6 +28,8 @@ private RayCast2D RayCast; //子弹的精灵 private Sprite BulletSprite; + //绘制阴影的精灵 + private Sprite ShadowSprite; private int frame = 0; @@ -42,7 +44,15 @@ BulletSprite = GetNode("Bullet"); BulletSprite.Visible = false; RayCast = GetNode("RayCast2D"); - + + //创建阴影 + ShadowSprite = new Sprite(); + ShadowSprite.Visible = false; + ShadowSprite.ZIndex = -1; + ShadowSprite.Texture = BulletSprite.Texture; + ShadowSprite.Offset = BulletSprite.Offset; + ShadowSprite.Material = ResourceManager.ShadowMaterial; + AddChild(ShadowSprite); } public override void _PhysicsProcess(float delta) @@ -50,6 +60,7 @@ if (frame++ == 0) { BulletSprite.Visible = true; + ShadowSprite.Visible = true; } //碰到墙壁 if (RayCast.IsColliding()) @@ -65,6 +76,7 @@ } else //没有碰到, 继续移动 { + ShadowSprite.GlobalPosition = GlobalPosition + new Vector2(0, 5); Position += new Vector2(FlySpeed * delta, 0).Rotated(Rotation); CurrFlyDistance += FlySpeed * delta; diff --git a/src/weapon/gun/Gun.cs b/src/weapon/gun/Gun.cs index 52fd2e7..e0af9e4 100644 --- a/src/weapon/gun/Gun.cs +++ b/src/weapon/gun/Gun.cs @@ -60,6 +60,7 @@ /// public float CurrScatteringRange { get; private set; } = 0; + //是否按下 private bool triggerFlag = false; //扳机计时器 diff --git a/src/weapon/gun/OrdinaryGun.cs b/src/weapon/gun/OrdinaryGun.cs index 0aa25c9..fa39413 100644 --- a/src/weapon/gun/OrdinaryGun.cs +++ b/src/weapon/gun/OrdinaryGun.cs @@ -19,14 +19,15 @@ protected override void Fire() { //创建一个弹壳 - var temp = new ThrowNode(); + var temp = new Shell(); var startPos = GlobalPosition + new Vector2(0, 5); - var startHeight = 16; + var startHeight = 6; var direction = GlobalRotationDegrees + MathUtils.RandRangeInt(-30, 30) + 180; var xf = MathUtils.RandRangeInt(20, 60); var yf = MathUtils.RandRangeInt(60, 120); var rotate = MathUtils.RandRangeInt(-720, 720); - temp.InitThrow(startPos, startHeight, direction, xf, yf, rotate, shell.Instance()); + var sprite = shell.Instance(); + temp.InitThrow(new Vector2(5, 10), startPos, startHeight, direction, xf, yf, rotate, sprite, sprite.Texture); RoomManager.Current.ItemRoot.AddChild(temp); } diff --git a/src/weapon/shell/Shell.cs b/src/weapon/shell/Shell.cs new file mode 100644 index 0000000..fb6afde --- /dev/null +++ b/src/weapon/shell/Shell.cs @@ -0,0 +1,34 @@ +using Godot; + +/// +/// 弹壳 +/// +public class Shell : ThrowNode +{ + protected override void OnInit() + { + + } + + protected override void OnOver() + { + //如果落地高度不够低, 再抛一次 + if (StartYSpeed > 1) + { + InitThrow(Size, GlobalPosition, 0, Direction, XSpeed * 0.8f, StartYSpeed * 0.5f, RotateSpeed * 0.5f, null); + } + else + { + //等待被销毁 + AwaitDestroy(); + } + } + + private async void AwaitDestroy() + { + CollisionShape.Disabled = true; + //20秒后销毁 + await ToSignal(GetTree().CreateTimer(20), "timeout"); + QueueFree(); + } +} \ No newline at end of file