首页 > 编程语言 >[GodotDL C# D2]从空格控制转弯到撞墙检测

[GodotDL C# D2]从空格控制转弯到撞墙检测

时间:2024-06-23 23:11:10浏览次数:30  
标签:last turn C# linetail Lineinfo var delta GodotDL D2

书接上回:https://www.cnblogs.com/meny233/p/18250399
新增功能:
空格控制转弯
导出变量
撞墙检测
音乐播放
前戏
image

点击查看代码
using Godot;
using System;

public partial class Line : CharacterBody3D
{
	[Export]
	public float Speed = 10f;
	[Export]
	public bool turn = false;
	[Export]
	public bool alive = true;
	[Export]
	public bool start = false;
	[Export]
	public MeshInstance3D tail;
	[Export]
	public AudioStreamPlayer song;
	[Export]
	public AudioStreamPlayer die;

	public override void _PhysicsProcess(double delta)
	{
		Vector3 velocity = Velocity;
		if (alive)
		{
			if (!IsOnFloor())
			{
				velocity += GetGravity() * (float)delta;
			}
			if (Input.IsActionJustPressed("ui_accept"))
			{
				if (!start)
				{
					start = true;
					song.Play();
				}
				turn = !turn;
			}
			if (start)
			{
				if (turn)
				{
					velocity.X = 0;
					velocity.Z = Speed;
				}
				else
				{
					velocity.Z = 0;
					velocity.X = Speed;
				}
			}
			if (IsOnWall())
			{
				alive = false;
				die.Play();
				song.Stop();
			}
		}
		Velocity = velocity;
		MoveAndSlide();
		MakeTail();
	}
	public void MakeTail()
	{
		//Soon
	}
}

参考
https://space.bilibili.com/508557497

点击查看代码
extends CharacterBody3D

var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var lastonfloor:bool = true
var song_time:float = 0

@onready var last_linetail:MeshInstance3D = $MeshInstance3D2
@onready var fallpar:CPUParticles3D = $CPUParticles3D
@onready var body:MeshInstance3D = $MeshInstance3D
@onready var song:AudioStreamPlayer = $AudioStreamPlayer
@onready var playing_ui = $CanvasLayer/playing
@onready var end_ui = $CanvasLayer/end
@onready var start = $CanvasLayer/start
#@onready var start_ui = $start
signal go_dead
var turn:String = ''
var timeLine:Array
#@export_file('*.json') var chart_file:String

func _ready():
	body.scale = Lineinfo.scale
	last_linetail.scale = Lineinfo.scale
	playing_ui.visible = false
	end_ui.visible = false
	start.connect("startgame", Callable(self, "_start"))
	Lineinfo.connect('turn_main_line', Callable(self, 'turn_line'))
	Lineinfo.connect('jump_line', Callable(self, 'jump'))
	Lineinfo.connect("change_auto", Callable(self, "do_auto_play"))
	turn = Lineinfo.turn
	do_auto_play()
	Levelinfo.song_time = 0

func do_auto_play():
	if Lineinfo.auto_play:
		load_autoplay()
	else:
		timeLine = []
		
func load_autoplay():
	var chart := FileAccess.open(Levelinfo.chart_file, FileAccess.READ)
	if not chart:
		return
	
	var json := chart.get_as_text()
	var roadInfo:Dictionary = JSON.parse_string(json)
	timeLine = []
	while (roadInfo.note.size() > 1):
		var a_note:Array = roadInfo.note[0].beat
		var bpm:float = roadInfo.time[0].bpm
		timeLine.push_back((a_note[0]*60/bpm)+(60/bpm/4*(a_note[1]+1)))
		roadInfo.note.remove_at(0)
		
func jump():
	velocity.y += Lineinfo.jump_velocity
	
func _physics_process(delta):
	if Lineinfo.auto_play and timeLine.size() > 0:
		if Levelinfo.song_time >= timeLine[0]:
			turn_line()
			timeLine.remove_at(0)
	
	if not Lineinfo.auto_play:
		turn = Lineinfo.turn
		
	if Lineinfo.can_fall:
		if not is_on_floor():
			velocity.y -= gravity * delta
			lastonfloor = false
		else:
			if not lastonfloor:
				var fall:CPUParticles3D = fallpar.duplicate()
				add_child(fall)
				fall.top_level = true
				fall.position = global_position
				fall.emitting = true
				lastonfloor = true
				make_line()
			
	move_and_slide()
	
	if is_on_wall():
		go_die()
		
	var SPEED:float = Lineinfo.speed
	if Levelinfo.started and not Lineinfo.dead and Lineinfo.can_move:
		if turn == 'left':
			position.x += delta * -SPEED
			if is_on_floor() or not Lineinfo.can_fall:
				last_linetail.scale.x += delta *SPEED
				if last_linetail.top_level:
					last_linetail.position.x += delta * -SPEED/2
				else:
					last_linetail.position.x += delta * SPEED/2
		elif turn == 'up':
			position.z += delta * -SPEED
			if is_on_floor() or not Lineinfo.can_fall:
				last_linetail.scale.z += delta * SPEED
				if last_linetail.top_level:
					last_linetail.position.z += delta * -SPEED/2
				else:
					last_linetail.position.z += delta * SPEED/2
		elif turn == 'down':
			position.z += delta * SPEED
			if is_on_floor() or not Lineinfo.can_fall:
				last_linetail.scale.z += delta * SPEED
				if last_linetail.top_level:
					last_linetail.position.z += delta * SPEED/2
				else:
					last_linetail.position.z += delta * -SPEED/2
		elif turn == 'right':
			position.x += delta * SPEED
			if is_on_floor() or not Lineinfo.can_fall:
				last_linetail.scale.x += delta *SPEED
				if last_linetail.top_level:
					last_linetail.position.x += delta * SPEED/2
				else:
					last_linetail.position.x += delta * -SPEED/2
					
		Lineinfo.moving = true
	else:
		Lineinfo.moving = false
		
	if Input.is_action_just_pressed("play"):
		turn_line()
	
	if song.playing:
		song_time += delta
		Levelinfo.song_time = song_time
	
	Levelinfo.song_percent = int(song_time / (song.stream.get_length() - 40) *100)

func go_die():
	if not Lineinfo.dead:
		song.stop()
		$AudioStreamPlayer2.play()
		$CPUParticles3D2.emitting = true
		end_ui.visible = true
		playing_ui.visible = false
		Lineinfo.dead = true

func _input(event):
	if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT and (is_on_floor() or not Lineinfo.can_fall) and not Lineinfo.dead and Lineinfo.can_control:
		if Lineinfo.auto_play and Levelinfo.started:
			return
			
		turn_line()
		
func _start():
	start.visible = false

func turn_line():
	if not start.visible:
		if not Levelinfo.started:
			Levelinfo.started = true
			var offset:float = Levelinfo.offset
			if offset > 0:
				var tween = create_tween()
				tween.tween_interval(offset/1000)
				tween.tween_callback(playsong)
			elif offset <= 0:
				song.play()
				song.seek(-offset/1000)
			
			#song_time += offset/1000
			playing_ui.visible = true
			return
		
		if Lineinfo.next_turn != '':
			Lineinfo.turn = Lineinfo.next_turn
			Lineinfo.next_turn = ''
		else:
			if Lineinfo.auto_play:
				if turn == 'left':
					turn = 'up'
				elif turn == 'up':
					turn = 'left'
				elif turn == 'down':
					turn = 'right'
				elif turn == 'right':
					turn = 'down'
			else:	
				if Lineinfo.turn == 'left':
					Lineinfo.turn = 'up'
				elif Lineinfo.turn == 'up':
					Lineinfo.turn = 'left'
				elif Lineinfo.turn == 'down':
					Lineinfo.turn = 'right'
				elif Lineinfo.turn == 'right':
					Lineinfo.turn = 'down'
	
		make_line()
		
func playsong():
	song.play()

func make_line():
	last_linetail.top_level = true
	var linetail:MeshInstance3D = $MeshInstance3D2.duplicate()
	linetail.position = body.position
	linetail.scale = body.scale
	last_linetail = linetail
	add_child(linetail)

标签:last,turn,C#,linetail,Lineinfo,var,delta,GodotDL,D2
From: https://www.cnblogs.com/meny233/p/18264076

相关文章

  • ABC359E的有趣解法
    题意:给定一个h序列,对于\(h_i\),找到一个\(j\)满足\(j<i\)且\(h_j>=h_i\),令\(ans_i=h_i*(i-j+1)+ans_j+1\),最后输出ans序列。赛时给了个很魔怔的解法,不知道是不是正解,反正是过了(摊手)我们可以开一个idx数组,将h[i]从小到大排序后将其原下表存入idx数组,这样我们从前......
  • LeetCode热题100(136.只出现一次数字)
    一.只出现一次数字给你一个非空整数数组nums,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。你必须设计并实现线性时间复杂度的算法来解决此问题,且该算法只使用常量额外空间。示例1:输入:nums=[2,2,1]输出:1示例2:输入:nums=......
  • 王鼎杯 RCE命令執行 五字節限制
    源碼:<?phperror_reporting(0);highlight_file(__FILE__);if(strlen($_GET['cmd'])<=5&&!preg_match('/rm/',$_GET['cmd'])){echoshell_exec($_GET['cmd']);}?>对cmd限制长度五字节起初是想通过变量来绕过五字节但好像不起作用......
  • C++系统相关操作3 - 获取操作系统的平台类型
    1.关键词2.sysutil.h3.sysutil.cpp4.测试代码5.运行结果6.源码地址1.关键词C++系统调用操作系统平台类型跨平台2.sysutil.h#pragmaonce#include<cstdint>#include<string>namespacecutl{/***@briefOperatingsystemplatformtype.......
  • LeetCode 70. 爬楼梯 使用c++解答
    假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?示例1:输入:n=2输出:2解释:有两种方法可以爬到楼顶。1.1阶+1阶2.2阶示例2:输入:n=3输出:3解释:有三种方法可以爬到楼顶。1.1阶+1阶......
  • opencv
    importtimeimportcv2importpyautoguidefget_xy(img_model_path):"""用来判定游戏画面的点击坐标:paramimg_model_path:用来检测的模板图片的路径:return:以元组形式返回检测到的区域的中心坐标"""#将屏幕截图保存pyautogui.scr......
  • C - Tile Distance 2
    C-TileDistance2https://atcoder.jp/contests/abc359/tasks/abc359_c 思路在x方向上,让s<t然后如果s在tile的左边,移动到右边, 如果t在tile的右边,移动到左边,计算x和y方便的必走的steps,y方向上容易计算(跨的格子就是),x方向有些复杂,s在x方向上,不用花费(配合y方向上走步......
  • AI 大模型企业应用实战(10)-LLMs和Chat Models
    1模型来看两种不同类型的模型--LLM和聊天模型。然后,它将介绍如何使用提示模板来格式化这些模型的输入,以及如何使用输出解析器来处理输出。LangChain中的语言模型有两种类型:1.1ChatModels聊天模型通常由LLM支持,但专门针对会话进行了调整。提供者API使用与纯文本补全模......
  • vue3 provide inject使用
    概论provide就是父类用来提供数据给子类或者孙子类inject就是子类或者孙子类用来获取父类或者祖先提供的provide数据代码app.vue祖先层<template><header><imgalt="Vuelogo"class="logo"src="@/assets/logo.svg"width="125"height="125&......
  • springmvc的视图
    目录6、springmvc的视图6.1、ThymeleafView6.2、转发视图6.3、重定向视图6.4、视图控制器view-contronller6、springmvc的视图SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户,SpringMVC视图的种类很多,默认有转发视图和重定向视图当工程引入jstl的......