当前位置: 首页 > news >正文

Unity3D仿星露谷物语开发37之浇水动画

1、目标

当点击水壶时,实现浇水的动画。同时有一个水从水壶中流出来的特效。

假如某个grid被浇过了,则不能再浇水了。。

如果某个grid没有被dug过,也不能被浇水。

2、优化Settings.cs脚本

增加如下内容:

  public static float liftToolAnimationPause = 0.4f;public static float afterLiftToolAnimationPause = 0.4f;

3、优化Player.cs脚本

添加以下代码:

private WaitForSeconds afterLiftToolAnimationPause;
private WaitForSeconds liftToolAnimationPause;// 在Start函数中添加如下代码
liftToolAnimationPause = new WaitForSeconds(Settings.liftToolAnimationPause);
afterLiftToolAnimationPause = new WaitForSeconds(Settings.afterLiftToolAnimationPause);

在ProcessPlayerClickInput的switch中增加如下一行代码:

在ProcessPlayerClickInputTool中增加如下代码:

添加如下代码:

 private void WaterGroundAtCursor(GridPropertyDetails gridPropertyDetails, Vector3Int playerDirection) { // Trigger animationStartCoroutine(WaterGroundAtCursorRoutine(playerDirection, gridPropertyDetails));}

添加如下代码:

private IEnumerator WaterGroundAtCursorRoutine(Vector3Int playerDirection, GridPropertyDetails gridPropertyDetails) 
{PlayerInputIsDisabled = true;playerToolUseDisabled = true;// Set tool animation to watering can in override animationtoolCharacterAttribute.partVariantType = PartVariantType.wateringCan;characterAttributeCustomisationList.Clear();characterAttributeCustomisationList.Add(toolCharacterAttribute);animationOverrides.ApplyCharacterCustomisationParameters(characterAttributeCustomisationList);toolEffect = ToolEffect.watering;if(playerDirection == Vector3Int.right){isLiftingToolRight = true;}else if(playerDirection == Vector3Int.left){isLiftingToolLeft = true;}else if(playerDirection == Vector3Int.up){isLiftingToolUp = true;}else if(playerDirection == Vector3Int.down){isLiftingToolDown = true;}yield return liftToolAnimationPause;// Set Grid property details for watered groundif(gridPropertyDetails.daysSinceWatered == -1){gridPropertyDetails.daysSinceWatered = 0;}// Set grid property to wateredGridPropertiesManager.Instance.SetGridPropertyDetails(gridPropertyDetails.gridX, gridPropertyDetails.gridY, gridPropertyDetails);// After animation pauseyield return afterLiftToolAnimationPause;PlayerInputIsDisabled = false;playerToolUseDisabled = false;
}

其中:

ToolEffect.watering是枚举值,值为1.

watering的触发条件:

Player.cs完整代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Player : SingletonMonobehaviour<Player>
{private WaitForSeconds afterUseToolAnimationPause;private WaitForSeconds useToolAnimationPause;private WaitForSeconds afterLiftToolAnimationPause;private WaitForSeconds liftToolAnimationPause;private bool playerToolUseDisabled = false;  // 如果玩家正在使用某个道具,其他道具将被禁用private AnimationOverrides animationOverrides;  // 动画重写控制器private GridCursor gridCursor;private List<CharacterAttribute> characterAttributeCustomisationList;  // 目标动作列表[Tooltip("Should be populated in the prefab with the equippped item sprite renderer")][SerializeField] private SpriteRenderer equippedItemSpriteRenderer = null; // 武器的图片// Player attributes that can be swappedprivate CharacterAttribute armsCharacterAttribute;private CharacterAttribute toolCharacterAttribute;private float xInput;private float yInput;private bool isWalking;private bool isRunning;private bool isIdle;private bool isCarrying = false;private ToolEffect toolEffect = ToolEffect.none;private bool isUsingToolRight;private bool isUsingToolLeft;private bool isUsingToolUp;private bool isUsingToolDown;private bool isLiftingToolRight;private bool isLiftingToolLeft;private bool isLiftingToolUp;private bool isLiftingToolDown;private bool isPickingRight;private bool isPickingLeft;private bool isPickingUp;private bool isPickingDown;private bool isSwingToolRight;private bool isSwingToolLeft;private bool isSwingToolUp;private bool isSwingToolDown;private Camera mainCamera;private Rigidbody2D rigidbody2D;private Direction playerDirection;private float movementSpeed;private bool _playerInputIsDisabled = false;public bool PlayerInputIsDisabled { get => _playerInputIsDisabled; set => _playerInputIsDisabled = value; }protected override void Awake(){base.Awake();rigidbody2D = GetComponent<Rigidbody2D>();animationOverrides = GetComponentInChildren<AnimationOverrides>();// Initialise swappable character attributesarmsCharacterAttribute = new CharacterAttribute(CharacterPartAnimator.arms, PartVariantColour.none, PartVariantType.none);// Initialise character attribute listcharacterAttributeCustomisationList = new List<CharacterAttribute>();mainCamera = Camera.main;}private void Start(){gridCursor = FindObjectOfType<GridCursor>();useToolAnimationPause = new WaitForSeconds(Settings.useToolAnimationPause);afterUseToolAnimationPause = new WaitForSeconds(Settings.afterUseToolAnimationPause);liftToolAnimationPause = new WaitForSeconds(Settings.liftToolAnimationPause);afterLiftToolAnimationPause = new WaitForSeconds(Settings.afterLiftToolAnimationPause);}private void Update(){#region  Player Inputif (!PlayerInputIsDisabled){ResetAnimationTrigger();PlayerMovementInput();PlayerWalkInput();PlayerClickInput();PlayerTestInput();// Send event to any listeners for player movement inputEventHandler.CallMovementEvent(xInput, yInput, isWalking, isRunning, isIdle, isCarrying, toolEffect,isUsingToolRight, isUsingToolLeft, isUsingToolUp, isUsingToolDown,isLiftingToolRight, isLiftingToolLeft, isLiftingToolUp, isLiftingToolDown,isPickingRight, isPickingLeft, isPickingUp, isPickingDown,isSwingToolRight, isSwingToolLeft, isSwingToolUp, isSwingToolDown,false, false, false, false);}#endregion Player Input}private void FixedUpdate(){PlayerMovement();}/// <summary>/// 展示拿东西的动画/// </summary>/// <param name="itemCode"></param>public void ShowCarriedItem(int itemCode){ItemDetails itemDetails = InventoryManager.Instance.GetItemDetails(itemCode);if(itemDetails != null){equippedItemSpriteRenderer.sprite = itemDetails.itemSprite;equippedItemSpriteRenderer.color = new Color(1f, 1f, 1f, 1f);// Apply 'carry' character arms customisationarmsCharacterAttribute.partVariantType = PartVariantType.carry;characterAttributeCustomisationList.Clear();characterAttributeCustomisationList.Add(armsCharacterAttribute);animationOverrides.ApplyCharacterCustomisationParameters(characterAttributeCustomisationList);isCarrying = true;}}public void ClearCarriedItem(){equippedItemSpriteRenderer.sprite = null;equippedItemSpriteRenderer.color = new Color(1f, 1f, 1f, 0f);// Apply base character arms customisationarmsCharacterAttribute.partVariantType = PartVariantType.none;characterAttributeCustomisationList.Clear();characterAttributeCustomisationList.Add(armsCharacterAttribute);animationOverrides.ApplyCharacterCustomisationParameters(characterAttributeCustomisationList);isCarrying = false;}private void PlayerMovement(){Vector2 move = new Vector2(xInput * movementSpeed * Time.deltaTime, yInput * movementSpeed * Time.deltaTime);rigidbody2D.MovePosition(rigidbody2D.position + move);}private void ResetAnimationTrigger(){toolEffect = ToolEffect.none;isUsingToolRight = false;isUsingToolLeft = false;isUsingToolUp = false;isUsingToolDown = false;isLiftingToolRight = false;isLiftingToolLeft = false;isLiftingToolUp = false;isLiftingToolDown = false;isPickingRight = false;isPickingLeft = false;isPickingUp = false;isPickingDown = false;isSwingToolRight = false;isSwingToolLeft = false;isSwingToolUp = false;isSwingToolDown = false;}private void PlayerMovementInput(){xInput = Input.GetAxisRaw("Horizontal");yInput = Input.GetAxisRaw("Vertical");// 斜着移动if (xInput != 0 && yInput != 0) {xInput = xInput * 0.71f;yInput = yInput * 0.71f;}// 在移动if (xInput != 0 || yInput != 0) {isRunning = true;isWalking = false;isIdle = false;movementSpeed = Settings.runningSpeed;// Capture player direction for save gameif (xInput < 0){playerDirection = Direction.left;}else if (xInput > 0){playerDirection = Direction.right;}else if (yInput < 0){playerDirection = Direction.down;}else{playerDirection = Direction.up;}}else if(xInput == 0 && yInput == 0){isRunning = false;isWalking = false;isIdle = true;}}// 按住Shift键移动为walkprivate void PlayerWalkInput(){if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)){isRunning = false;isWalking = true;isIdle = false;movementSpeed = Settings.walkingSpeed;}else{isRunning = true;isWalking = false;isIdle = false;movementSpeed = Settings.runningSpeed;}}private void PlayerClickInput(){if (!playerToolUseDisabled){if (Input.GetMouseButton(0)){if (gridCursor.CursorIsEnabled){// Get Cursor Grid PositionVector3Int cursorGridPosition = gridCursor.GetGridPositionForCursor();// Get Player Grid PositionVector3Int playerGridPosition = gridCursor.GetGridPositionForPlayer();ProcessPlayerClickInput(cursorGridPosition, playerGridPosition);}}}}private void ProcessPlayerClickInput(Vector3Int cursorGridPosition, Vector3Int playerGridPosition){ResetMovement();Vector3Int playerDirection = GetPlayerClickDirection(cursorGridPosition, playerGridPosition);// Get Selected item detailsItemDetails itemDetails = InventoryManager.Instance.GetSelectedInventoryItemDetails(InventoryLocation.player);// Get Grid property details at cursor position (the GridCursor validation routine ensures that grid property details are not null)GridPropertyDetails gridPropertyDetails = GridPropertiesManager.Instance.GetGridPropertyDetails(cursorGridPosition.x, cursorGridPosition.y);if(itemDetails != null){switch (itemDetails.itemType){case ItemType.Seed:if (Input.GetMouseButtonDown(0)){ProcessPlayerClickInputSeed(itemDetails);}break;case ItemType.Commodity:if (Input.GetMouseButtonDown(0)){ProcessPlayerClickInputCommodity(itemDetails);}break;case ItemType.Watering_tool:case ItemType.Hoeing_tool:ProcessPlayerClickInputTool(gridPropertyDetails, itemDetails, playerDirection);break;case ItemType.none:break;case ItemType.count:break;default:break;}}}private Vector3Int GetPlayerClickDirection(Vector3Int cursorGridPosition, Vector3Int playerGridPosition) {if(cursorGridPosition.x > playerGridPosition.x){return Vector3Int.right;}else if(cursorGridPosition.x < playerGridPosition.x){return Vector3Int.left;}else if(cursorGridPosition.y > playerGridPosition.y){return Vector3Int.up;}else{return Vector3Int.down;}}private void ProcessPlayerClickInputSeed(ItemDetails itemDetails){if(itemDetails.canBeDropped && gridCursor.CursorPositionIsValid){EventHandler.CallDropSelectedItemEvent();}}private void ProcessPlayerClickInputCommodity(ItemDetails itemDetails){if(itemDetails.canBeDropped && gridCursor.CursorPositionIsValid){EventHandler.CallDropSelectedItemEvent();}}private void ProcessPlayerClickInputTool(GridPropertyDetails gridPropertyDetails, ItemDetails itemDetails, Vector3Int playerDirection){// Switch on toolswitch (itemDetails.itemType){case ItemType.Hoeing_tool:if (gridCursor.CursorPositionIsValid){HoeGroundAtCursor(gridPropertyDetails, playerDirection);}break;case ItemType.Watering_tool:if (gridCursor.CursorPositionIsValid){WaterGroundAtCursor(gridPropertyDetails, playerDirection);}break;default:break;}}private void WaterGroundAtCursor(GridPropertyDetails gridPropertyDetails, Vector3Int playerDirection) { // Trigger animationStartCoroutine(WaterGroundAtCursorRoutine(playerDirection, gridPropertyDetails));}private IEnumerator WaterGroundAtCursorRoutine(Vector3Int playerDirection, GridPropertyDetails gridPropertyDetails) {PlayerInputIsDisabled = true;playerToolUseDisabled = true;// Set tool animation to watering can in override animationtoolCharacterAttribute.partVariantType = PartVariantType.wateringCan;characterAttributeCustomisationList.Clear();characterAttributeCustomisationList.Add(toolCharacterAttribute);animationOverrides.ApplyCharacterCustomisationParameters(characterAttributeCustomisationList);toolEffect = ToolEffect.watering;if(playerDirection == Vector3Int.right){isLiftingToolRight = true;}else if(playerDirection == Vector3Int.left){isLiftingToolLeft = true;}else if(playerDirection == Vector3Int.up){isLiftingToolUp = true;}else if(playerDirection == Vector3Int.down){isLiftingToolDown = true;}yield return liftToolAnimationPause;// Set Grid property details for watered groundif(gridPropertyDetails.daysSinceWatered == -1){gridPropertyDetails.daysSinceWatered = 0;}// Set grid property to wateredGridPropertiesManager.Instance.SetGridPropertyDetails(gridPropertyDetails.gridX, gridPropertyDetails.gridY, gridPropertyDetails);// After animation pauseyield return afterLiftToolAnimationPause;PlayerInputIsDisabled = false;playerToolUseDisabled = false;}private void HoeGroundAtCursor(GridPropertyDetails gridPropertyDetails, Vector3Int playerDirection) {// Trigger animationStartCoroutine(HoeGroundAtCursorRoutine(playerDirection, gridPropertyDetails));}private IEnumerator HoeGroundAtCursorRoutine(Vector3Int playerDirection, GridPropertyDetails gridPropertyDetails){PlayerInputIsDisabled = true;playerToolUseDisabled = true;// Set tool animation to hoe in override animationtoolCharacterAttribute.partVariantType = PartVariantType.hoe;characterAttributeCustomisationList.Clear();characterAttributeCustomisationList.Add(toolCharacterAttribute);animationOverrides.ApplyCharacterCustomisationParameters(characterAttributeCustomisationList);if(playerDirection == Vector3Int.right){isUsingToolRight = true;}else if(playerDirection == Vector3Int.left){isUsingToolLeft = true;}else if(playerDirection == Vector3Int.up){isUsingToolUp = true;}else if(playerDirection == Vector3Int.down){isUsingToolDown = true;}yield return useToolAnimationPause;// Set Grid property details for dug groundif(gridPropertyDetails.daysSinceDug == -1){gridPropertyDetails.daysSinceDug = 0;}// Set grid property to dugGridPropertiesManager.Instance.SetGridPropertyDetails(gridPropertyDetails.gridX, gridPropertyDetails.gridY, gridPropertyDetails);// Display dug grid tilesGridPropertiesManager.Instance.DisplayDugGround(gridPropertyDetails);// After animation pauseyield return afterUseToolAnimationPause;PlayerInputIsDisabled = false;playerToolUseDisabled = false;}public Vector3 GetPlayerViewportPosition(){// Vector3 viewport position for player (0,0) viewport bottom left, (1,1) viewport top rightreturn mainCamera.WorldToViewportPoint(gameObject.transform.position);}public void DisablePlayerInputAndResetMovement(){DisablePlayerInpupt();ResetMovement();// Send event to any listeners for player movement inputEventHandler.CallMovementEvent(xInput, yInput, isWalking, isRunning, isIdle, isCarrying, toolEffect,isUsingToolRight, isUsingToolLeft, isUsingToolUp, isUsingToolDown,isLiftingToolRight, isLiftingToolLeft, isLiftingToolUp, isLiftingToolDown,isPickingRight, isPickingLeft, isPickingUp, isPickingDown,isSwingToolRight, isSwingToolLeft, isSwingToolUp, isSwingToolDown,false, false, false, false);}private void ResetMovement(){// Reset movementxInput = 0f;yInput = 0f;isRunning = false;isWalking = false;isIdle = true;}public void DisablePlayerInpupt(){PlayerInputIsDisabled = true;}public void EnablePlayerInput(){PlayerInputIsDisabled = false;}/// <summary>/// Temp routine for test input/// </summary>private void PlayerTestInput(){// Trigger Advance Timeif (Input.GetKeyDown(KeyCode.T)){TimeManager.Instance.TestAdvanceGameMinute();}// Trigger Advance Dayif (Input.GetKeyDown(KeyCode.G)){TimeManager.Instance.TestAdvanceGameDay();}// Test scene unload / loadif (Input.GetKeyDown(KeyCode.L)){SceneControllerManager.Instance.FadeAndLoadScene(SceneName.Scene1_Farm.ToString(), transform.position);}}
}

4、优化GridCursor.cs脚本

1)调整SetCursorValidity的逻辑。

增加如下一行代码:

2)调整IsCursorValidForTool的逻辑。

添加如下代码:

其完整代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class GridCursor : MonoBehaviour
{private Canvas canvas; // 存储白色画布,UI光标位于其中private Grid grid;  // tilemap地图private Camera mainCamera; // 对主相机的引用[SerializeField] private Image cursorImage = null;[SerializeField] private RectTransform cursorRectTransform = null;  // 光标对象的引用[SerializeField] private Sprite greenCursorSprite = null;[SerializeField] private Sprite redCursorSprite = null;private bool _cursorPositionIsValid = false;public bool CursorPositionIsValid { get => _cursorPositionIsValid; set => _cursorPositionIsValid = value; }private int _itemUseGridRadius = 0;public int ItemUseGridRadius { get => _itemUseGridRadius; set => _itemUseGridRadius = value; }private ItemType _selectedItemType;public ItemType SelectedItemType { get => _selectedItemType; set => _selectedItemType = value; }private bool _cursorIsEnabled = false;public bool CursorIsEnabled { get => _cursorIsEnabled; set => _cursorIsEnabled = value; }private void OnDisable(){EventHandler.AfterSceneLoadEvent -= SceneLoaded;}private void OnEnable(){EventHandler.AfterSceneLoadEvent += SceneLoaded;}// Start is called before the first frame updatevoid Start(){mainCamera = Camera.main;canvas = GetComponentInParent<Canvas>();}// Update is called once per framevoid Update(){if (CursorIsEnabled){DisplayCursor();}}private Vector3Int DisplayCursor(){if (grid != null){   // 之所以需要Grid是因为某些定位是基于grid的// Get grid position for cursorVector3Int gridPosition = GetGridPositionForCursor();// Get grid position for playerVector3Int playerGridPosition = GetGridPositionForPlayer();// Set cursor sprite,基于gridPosition和playerGridPosition设置光标的有效性SetCursorValidity(gridPosition, playerGridPosition);// Get rect transform position for cursorcursorRectTransform.position = GetRectTransformPositionForCursor(gridPosition);return gridPosition;}else{return Vector3Int.zero;}}public Vector3Int GetGridPositionForCursor(){// z is how far the objects are in front of the camera - camera is at -10 so objects are(-)-10 in front = 10Vector3 worldPosition = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -mainCamera.transform.position.z));return grid.WorldToCell(worldPosition);}public Vector3Int GetGridPositionForPlayer(){return grid.WorldToCell(Player.Instance.transform.position);}public Vector2 GetRectTransformPositionForCursor(Vector3Int gridPosition){Vector3 gridWorldPosition = grid.CellToWorld(gridPosition);Vector2 gridScreenPosition = mainCamera.WorldToScreenPoint(gridWorldPosition);return RectTransformUtility.PixelAdjustPoint(gridScreenPosition, cursorRectTransform, canvas);}private void SceneLoaded(){grid = GameObject.FindObjectOfType<Grid>();}private void SetCursorValidity(Vector3Int cursorGridPosition, Vector3Int playerGridPosition){SetCursorToValid();// Check item use radius is validif(Mathf.Abs(cursorGridPosition.x -  playerGridPosition.x) > ItemUseGridRadius|| Mathf.Abs(cursorGridPosition.y - playerGridPosition.y) > ItemUseGridRadius){SetCursorToInvalid();return;}// Get selected item details ItemDetails itemDetails = InventoryManager.Instance.GetSelectedInventoryItemDetails(InventoryLocation.player);if(itemDetails == null){SetCursorToInvalid();return;}// Get grid property details at cursor positionGridPropertyDetails gridPropertyDetails = GridPropertiesManager.Instance.GetGridPropertyDetails(cursorGridPosition.x, cursorGridPosition.y);if(gridPropertyDetails != null){// Determine cursor validity based on inventory item selected and grid property detailsswitch (itemDetails.itemType){case ItemType.Seed:if (!IsCursorValidForSeed(gridPropertyDetails)){SetCursorToInvalid();return;}break;case ItemType.Commodity:if (!IsCursorValidForCommodity(gridPropertyDetails)){SetCursorToInvalid();return;}break;case ItemType.Watering_tool:case ItemType.Hoeing_tool:if(!IsCursorValidForTool(gridPropertyDetails, itemDetails)){SetCursorToInvalid();return;}break;case ItemType.none:break;case ItemType.count:break;default:break;}}else{SetCursorToInvalid();return;}}private bool IsCursorValidForSeed(GridPropertyDetails gridPropertyDetails){return gridPropertyDetails.canDropItem;}private bool IsCursorValidForCommodity(GridPropertyDetails gridPropertyDetails){return gridPropertyDetails.canDropItem;}/// <summary>/// Sets the cursor as either valid or invalid for the tool for the target gridPropertyDetails. /// Returns true if valid or false if invalid/// </summary>/// <param name="gridPropertyDetails"></param>/// <param name="itemDetails"></param>/// <returns></returns>private bool IsCursorValidForTool(GridPropertyDetails gridPropertyDetails, ItemDetails itemDetails){// Switch on toolswitch (itemDetails.itemType){case ItemType.Hoeing_tool:if (gridPropertyDetails.isDiggable == true && gridPropertyDetails.daysSinceDug == -1){#region Need to get any items at location so we can check if they are reapable// Get world position for cursorVector3 cursorWorldPosition = new Vector3(GetWorldPositionForCursor().x + 0.5f, GetWorldPositionForCursor().y + 0.5f, 0f);// Get list of items at cursor locationList<Item> itemList = new List<Item>();HelperMethods.GetComponentsAtBoxLocation<Item>(out itemList, cursorWorldPosition, Settings.cursorSize, 0f);#endregion// Loop through items found to see if any are reapable type - we are not goint to let the player dig where there are reapable scenary itemsbool foundReapable = false;foreach(Item item in itemList){if(InventoryManager.Instance.GetItemDetails(item.ItemCode).itemType == ItemType.Reapable_scenary){foundReapable = true;break;}}if (foundReapable){return false;}else{return true;}}else{return false;}case ItemType.Watering_tool:if(gridPropertyDetails.daysSinceDug > -1 && gridPropertyDetails.daysSinceWatered == -1){return true;}else{return false;}default:return false;}}private void SetCursorToValid(){cursorImage.sprite = greenCursorSprite;CursorPositionIsValid = true;}private void SetCursorToInvalid(){cursorImage.sprite = redCursorSprite;CursorPositionIsValid = false;}/// <summary>/// DisableCursor is called in the UIInventorySlot.ClearCursors() method when an inventory slot item is no longer selected/// </summary>public void DisableCursor(){cursorImage.color = Color.clear;CursorIsEnabled = false;}/// <summary>/// EnableCursor is called in the UIInventorySlot.SetSelectedItem() method when an inventory slot item is selected and its itemUseGrid radius>0/// </summary>public void EnableCursor(){cursorImage.color = new Color(1f, 1f, 1f, 1f);CursorIsEnabled = true;}public Vector3 GetWorldPositionForCursor(){return grid.CellToWorld(GetGridPositionForCursor());}}

5、设置浇水半径

默认情况下也是1,无需改动。

6、运行游戏

相关文章:

Unity3D仿星露谷物语开发37之浇水动画

1、目标 当点击水壶时&#xff0c;实现浇水的动画。同时有一个水从水壶中流出来的特效。 假如某个grid被浇过了&#xff0c;则不能再浇水了。。 如果某个grid没有被dug过&#xff0c;也不能被浇水。 2、优化Settings.cs脚本 增加如下内容&#xff1a; public static float…...

JavaScript 一维数组转不含零的两个数

问题描述&#xff1a; /*** param {number} n* return {number[]}*/ var getNoZeroIntegers function(n) {for(let i 1;i<n;i){if(String(i).indexOf(0) -1&&String(n-i).indexOf(0) -1){return [i,n-i]}}};String类型indexOf()函数如果找不到字串则返回-1&…...

抽象工厂模式及其在自动驾驶中的应用举例(c++代码实现)

模式定义 抽象工厂模式&#xff08;Abstract Factory Pattern&#xff09;是一种创建型设计模式&#xff0c;用于封装一组具有共同主题的独立对象创建过程。该模式通过提供统一接口创建相关对象家族&#xff0c;而无需指定具体实现类&#xff0c;特别适合需要保证系统组件兼容…...

知乎十四载:从精英问答到AI时代的知识灯塔

一、起源&#xff1a;Quora 的火种与周源的执念 2010 年&#xff0c;互联网浪潮正汹涌澎湃&#xff0c;各种新兴平台如雨后春笋般不断涌现。就在这一年的一个夜晚&#xff0c;周源像往常一样在网上浏览着各类信息&#xff0c;当他打开美国问答网站 Quora 时&#xff0c;瞬间被…...

Linux文件时间戳详解:Access、Modify、Change时间的区别与作用

在 Linux 系统中&#xff0c;文件的这三个时间戳&#xff08;Access、Modify、Change&#xff09;分别表示不同的文件状态变更时间&#xff0c;具体含义如下&#xff1a; 1. Access Time (Access) 含义&#xff1a;文件最后一次被访问的时间&#xff08;读取内容或执行&#xf…...

Doris + Iceberg 构建冷热分层数据湖架构:架构设计与实战指南

在海量数据治理与存储演进中&#xff0c;冷热数据分层 已成为降本增效的关键策略。本篇将深入探讨如何结合 Apache Doris 与 Apache Iceberg 构建一套高性能、可扩展的数据湖架构&#xff0c;支持冷热数据自动分层、快速查询与灵活扩展。 一、背景&#xff1a;为什么需要冷热数…...

顺序表和链表的区别(C语言)

前言 线性表是最基础的数据结构之一&#xff0c;其中顺序表与链表分别代表两种存储方式&#xff1a; 顺序表&#xff08;Sequential List&#xff09;&#xff1a;底层用数组实现&#xff0c;要求内存连续&#xff0c;典型代码以 int data[N] 或动态分配的 malloc/realloc 数…...

【Redis】Redis中的常见数据类型(一)

文章目录 前言一、Redis前置知识1. 全局命令2、数据结构和内部编码3. 单线程架构 二、String 字符串1. 常见命令2. 计数命令3.其他命令4. 内部编码5. 典型使用场景 三、Hash哈希1. 命令2.内部编码3. 使用场景4. 缓存方式对比 结语 前言 Redis 提供了 5 种数据结构&#xff0c;…...

Vue3 + TypeScript,使用祖先传后代模式重构父传子模式

父传子模式 父组件 SampleInput.vue <script setup lang"ts" name"SampleInput"> import { ref } from "vue"; import type { ApplyBasicInfo, Apply, ApplySample } from "/interface"; import CommonApplySampleTable from …...

MySQL:9.表的内连和外连

9.表的内连和外连 表的连接分为内连和外连 9.1 内连接 内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选&#xff0c;之前查询都是内连 接&#xff0c;也是在开发过程中使用的最多的连接查询。 语法&#xff1a; select 字段 from 表1 inner join 表2 on 连接…...

(mac)Grafana监控系统之监控Linux的Redis

Grafana安装-CSDN博客 普罗米修斯Prometheus监控安装&#xff08;mac&#xff09;-CSDN博客 1.Redis_exporter安装 直接下载 wget https://github.com/oliver006/redis_exporter/releases/download/v1.0.3/redis_exporter-v1.0.3.linux-amd64.tar.gz 解压 tar -xvf redis_…...

Multisim使用教程详尽版--(2025最新版)

一、Multisim14前言 1.1、主流电路仿真软件 1. Multisim&#xff1a;NI开发的SPICE标准仿真工具&#xff0c;支持模拟/数字电路混合仿真&#xff0c;内置丰富的元件库和虚拟仪器&#xff08;示波器、频谱仪等&#xff09;&#xff0c;适合教学和竞赛设计。官网&#xff1a;艾…...

python pdf转图片再OCR

先pdf转图片 import os from pdf2image import convert_from_path# PDF文件路径 pdf_path /Users/xxx/2022.pdf # 输出图片的文件夹 output_folder ./output_images2022 # 输出图片的命名格式 output_name page# 如果输出文件夹不存在&#xff0c;创建它 if not os.path.ex…...

npm link 使用指南

npm link 使用指南 npm link 是一个非常有用的命令&#xff0c;主要用于在开发过程中将本地 npm 包链接到全局 npm 目录&#xff0c;从而可以在其他项目中使用这个本地包&#xff0c;而不需要发布到 npm 仓库。 基本用法 1. 创建全局链接 进入你要链接的本地包的根目录&…...

免费图片软件,可矫正倾斜、调整去底效果

软件介绍 有个超棒的软件要给大家介绍一下哦&#xff0c;它就是——ImgTool&#xff0c;能实现图片漂白去底的功能&#xff0c;而且重点是&#xff0c;它是完全免费使用的呢&#xff0c;功能超强大&#xff01; 软件特点及使用便捷性 这软件是绿色版本的哟&#xff0c;就像一…...

5G网络切片:精准分配资源,提升网络效率的关键技术

5G网络切片&#xff1a;精准分配资源&#xff0c;提升网络效率的关键技术 随着5G技术的广泛应用&#xff0c;网络切片&#xff08;Network Slicing&#xff09;作为其核心创新之一&#xff0c;正在改变传统网络架构。它通过将物理网络划分为多个逻辑网络&#xff08;切片&…...

seate TCC模式案例

场景描述 用户下单时&#xff0c;需要创建订单并从用户账户中扣除相应的余额。如果订单创建成功但余额划扣失败&#xff0c;则需要回滚订单创建操作。使用 Seata 的 TCC 模式来保证分布式事务的一致性。 1. 项目结构 假设我们有两个微服务&#xff1a; Order Service&#x…...

Transfomer的本质

Transformer是一个基于自注意力机制的深度学习模型&#xff0c;用于处理序列数据&#xff0c;主要结构包括编码器和解码器&#xff0c;每个部分由多头自注意力和前馈网络组成&#xff0c;加上残差连接和层归一化&#xff0c;以及位置编码。它解决了RNN的并行处理问题&#xff0…...

final修饰变量的注意

在Java中&#xff0c;使用final修饰变量时&#xff0c;需注意以下关键事项&#xff1a; 1. 初始化规则 实例变量&#xff1a; 必须在声明时、构造器或实例初始化块中初始化。所有构造器分支必须保证初始化。 class Example {final int x; // 实例变量final int y;public Exampl…...

前端与传统接口的桥梁:JSONP解决方案

1.JSONP原理 1.1.动态脚本注入 说明&#xff1a;通过创建 <script> 标签绕过浏览器同源策略 1.2.回调约定 说明&#xff1a;服务端返回 函数名(JSON数据) 格式的JS代码 1.3.自动执行 说明&#xff1a;浏览器加载脚本后立即触发前端预定义的回调函数&#xff08;现代开…...

SQL注入 01

0x01 用户、脚本、数据库之间的关系 首先客户端发出了ID36的请求&#xff0c;脚本引擎收到后将ID36的请求先代入脚本的sql查询语句Select * from A where id 36 &#xff0c; 然后将此代入到数据库中进行查询&#xff0c;查到后将返回查询到的所有记录给脚本引擎&#xff0c;接…...

Java之封装(学习笔记)

封装定义&#xff08;个人理解&#xff1a;&#xff09; 封装就像电视遥控器的按钮&#xff0c;比如音量键&#xff0c;对于我们使用者来说就是可以直接按下去调控音量&#xff0c;对于代码写作者来说就是封装了调控音量的方法&#xff0c;使得我们只能去调控&#xff0c;不能改…...

每天学一个 Linux 命令(27):head

​​可访问网站查看,视觉品味拉满: http://www.616vip.cn/27/index.html head 是 Linux 中用于查看文件开头部分内容的命令,默认显示文件前 10 行,适合快速预览文件结构或日志头部信息。 命令格式 head [选项] [文件]常用选项 选项说明-n <行数>指定显示前 N 行(如…...

山东大学软件学院创新项目实训开发日志(20)之中医知识问答自动生成对话标题bug修改

在原代码中存在一个bug&#xff1a;当前对话的标题不是现有对话的用户的第一段的前几个字&#xff0c;而是历史对话的第一段的前几个字。 这是生成标题的逻辑出了错误&#xff1a; 当改成size()-1即可...

论文阅读:2024 ICML Is DPO Superior to PPO for LLM Alignment? A Comprehensive Study

Is DPO Superior to PPO for LLM Alignment? A Comprehensive Study https://www.doubao.com/chat/3506902534329346 https://arxiv.org/pdf/2404.10719 速览 这篇论文主要探讨了大语言模型对齐中两种主流方法——**DPO&#xff08;直接偏好优化&#xff09;和PPO&#xf…...

2025年人工智能指数报告:技术突破与社会变革的全景透视

《2025年人工智能指数报告》作为斯坦福大学人工智能实验室与多方合作的年度重磅研究&#xff0c;以超过千页的篇幅全景式展现了人工智能技术在全球范围内的发展轨迹与深远影响。这份报告不仅延续了对AI技术性能、科研进展与产业应用的追踪&#xff0c;更首次深入探讨了AI硬件的…...

【Python笔记 01】变量、标识符

一、 变量 1、变量的作用 计算机存储空间&#xff0c;用于保存数据。 2、定义变量的格式 格式&#xff1a;变量名 值 示例&#xff1a; num1 3 # num1就是一个变量&#xff0c;保存蛋糕的价格 num2 10 #num2也是一个变量&#xff0c;保存雪碧的价格 total num1 num2 …...

WebSocket启用备忘

一&#xff1a;引入依赖&#xff1a; <!--WebSocket专用--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency><dependency><groupId>org…...

文件管理详解(曼波脑图版)

(✪ω✪)曼波来啦&#xff01;文件管理的知识曼波这就为你详细讲解哟~ 记得要准备好小本本做笔记哦&#xff01;(๑˃̵ᴗ˂̵)و &#x1f31f; 文件读写操作 &#x1f31f; // 最可爱的文件读取写法 (✧∇✧) try (BufferedReader reader new BufferedReader(new FileRead…...

学习笔记十九——Rust多态

&#x1f9e9; Rust 多态终极通俗指南 &#x1f4da; 目录导航 多态一句话概念静态分派 vs 动态分派——根本差异参数化多态&#xff08;泛型&#xff09; 3.1 函数里的泛型 3.2 结构体里的泛型 3.3 方法里的泛型 3.4 枚举里的泛型Ad hoc 多态&#xff08;特例多态&#xff0…...

在 Linux 上部署 .NET Core 应用并配置为开机自动启动

在本文中&#xff0c;我们将详细介绍如何在 Linux 系统上部署 .NET Core 应用程序&#xff0c;并配置为开机自动启动。以下是一步一步的详细部署过程&#xff0c;适用于将 .NET Core Web 应用部署到生产环境中。 1. 安装 .NET 运行时和 SDK 首先&#xff0c;确保 Linux 系统上…...

dubbo SPI插件扩展点使用

参考&#xff1a;SPI插件扩展点 Dubbo SPI概述 使用IoC容器帮助管理组件的生命周期、依赖关系注入等是很多开发框架的常用设计&#xff0c;Dubbo中内置了一个轻量版本的IoC容器&#xff0c;用来管理框架内部的插件&#xff0c;实现包括插件实例化、生命周期、依赖关系自动注入…...

P8512 [Ynoi Easy Round 2021] TEST_152 Solution

Description 有一序列 c ( c 1 , c 2 , ⋯ , c m ) c(c_1,c_2,\cdots,c_m) c(c1​,c2​,⋯,cm​) 和 n n n 个三元组 ( l i , r i , v i ) (l_i,r_i,v_i) (li​,ri​,vi​). 回答 q q q 次形如 ( L , R ) (L,R) (L,R) 的询问&#xff0c;具体如下&#xff1a; 将 c c …...

开源项目FastAPI-MCP:一键API转换MCP服务

在当今AI开发的世界中,应用程序与AI模型之间的无缝集成至关重要。 模型上下文协议(Model Context Protocol, MCP)通过允许AI模型访问外部工具和数据源,弥合了这一差距。 FastAPI MCP是一个强大的工具,它可以通过最少的配置将您现有的FastAPI端点转换为MCP兼容的工具。 本…...

8、constexpr if、inline、类模版参数推导、lambda的this捕获---c++17

一、constexpr if&#xff1a;编译时条件分支 作用&#xff1a;在模板编程中&#xff0c;根据条件在编译时选择不同的代码路径&#xff0c;无需特化版本或复杂SFINAE技巧[替代SFINAE]。[SFINAE将在模版元编程再讲。下个月了。]基本语法 if constexpr (condition) {// 如果 co…...

github新建一个远程仓库并添加了README.md,本地git仓库无法push

1.本地git仓库与远程仓库绑定 2.push时报错&#xff0c;本地的 main 分支落后于远程仓库的 main 分支&#xff08;即远程有更新&#xff0c;但你本地没有&#xff09;&#xff0c;需要拉取远程的仓库--->在merge合并&#xff08;解决冲突&#xff09;--->push 3.但是git …...

贝叶斯分类器:原理、算法与应用详解

内容摘要 本文聚焦贝叶斯分类器&#xff0c;介绍其在各类分类器中分类错误概率最小的特性。详细阐述贝叶斯分类器的基本原理、朴素贝叶斯和半朴素贝叶斯分类器的算法&#xff0c;结合西瓜数据集实例说明朴素贝叶斯的应用。此外&#xff0c;还深入探讨极大似然估计和贝叶斯估计…...

算法篇之单调栈

单调栈算法入门 单调栈是一种特殊的数据结构应用&#xff0c;它的核心在于维护一个栈&#xff0c;使得栈内元素保持单调递增或者单调递减的顺序。这种数据结构在解决很多算法问题时非常有效&#xff0c;例如求数组中每个元素的下一个更大元素、每日温度问题等。 一、单调栈的…...

用python + PIL 实现图片格式转换工具

用python PIL 实现图片格式转换工具 要运行该程序&#xff0c;需要使用第三方库PIL&#xff08;pillow&#xff09;&#xff0c;详情可见https://blog.csdn.net/cnds123/article/details/126141838 格式支持&#xff1a; 支持常见图片格式转换&#xff08;JPEG, PNG, BMP, GIF…...

【数据库】事务

目录 1. 什么是事务&#xff1f; 2. 事务的ACID特性 3. 为什么使用事务&#xff1f; 4. 如何使用事务 4.1 查看支持事务的存储引擎 4.2 语法 4.3 保存点 4.4 自动/手动提交事务 5. 事物的隔离性和隔离级别 5.1 什么是隔离性 5.2 隔离级别 5.3 查看和设置隔离级别 1…...

C++:详解命名空间

目录 前言 一、命名空间是什么&#xff1f; 1.1命名空间域的定义 二、为什么有命名空间&#xff1f; 三、命名空间的用法 总结 前言 主要讲解命名空间是什么&#xff1f;为什么有命名空间&#xff1f;以及它的用法 一、命名空间是什么&#xff1f; 命名空间域&#xff08;nam…...

ClickHouse核心架构设计

列式存储原理与数据压缩 列式存储原理 列式存储 vs 行式存储 特性行式存储&#xff08;如MySQL&#xff09;列式存储&#xff08;ClickHouse&#xff09;数据排列按行连续存储&#xff08;所有字段相邻&#xff09;按列连续存储&#xff08;单列数据紧密排列&#xff09;适用场…...

K8s-Pod详解

Pod介绍 Pod是Kubernetes中能够创建和部署的最小单元&#xff0c;是Kubernetes集群中的一个应用实例&#xff0c;总是部署在同一个节点Node上。&#xff08;程序运行部署在容器中&#xff0c;容器必须存在pod中。pod可以认为是容器的封装&#xff0c;一个pod中可以存在一个或者…...

SSM(SpringMVC+spring+mybatis)整合的步骤以及相关依赖

目录 &#xff08;一&#xff09;导入SSM框架相关的依赖 ①创建一个新的web工程&#xff08;idea2023版&#xff09; ②思考三者之间的联系&#xff0c;回忆依赖 ③在pom.xml文件中引入依赖坐标 &#xff08;二&#xff09;使用注解开发&#xff0c;编写Spring的配置类&am…...

【LeetCode】算法详解#5 ---轮转数组

1.题目介绍 给定一个整数数组 nums&#xff0c;将数组中的元素向右轮转 k 个位置&#xff0c;其中 k 是非负数。 1 < nums.length < 105-231 < nums[i] < 231 - 10 < k < 105 2.解决思路 这道题的解决方法有很多&#xff0c;我这里给大家介绍我使用的方法。…...

LeetCode 打家劫舍+删除并获得点数

题目描述 打家劫舍题目传送门1 删除并获得点数传送门2 思路 这两道题看似毫无关系&#xff0c;但竟然可以用桶数组联系起来&#xff01;&#xff01; 先说打家劫舍这道题 限制条件是不能走相邻的屋&#xff0c;再联想到跳台阶&#xff08;走一格或两格&#xff09;&#x…...

【计量地理学】实验四 主成分分析与莫兰指数

一、实验内容 &#xff08;一&#xff09; 某地区35个城市2004年的7项经济统计指标数据见&#xff08;数据中的“题目1”sheet&#xff09;。 &#xff08;1&#xff09;试用最短距离聚类法对35个城市综合实力进行系统聚类分析&#xff0c;并画出聚类谱系图: 在此次实验内容…...

TDengine 性能监控与调优实战指南(二)

四、TDengine 性能调优实战 4.1 硬件层面优化 硬件是 TDengine 运行的基础&#xff0c;其性能直接影响着 TDengine 的整体表现。在硬件层面进行优化&#xff0c;就如同为高楼大厦打下坚实的地基&#xff0c;能够为 TDengine 的高效运行提供有力支持。 CPU&#xff1a;CPU 作…...

Linux `init 5` 相关命令的完整使用指南

Linux init 5 相关命令的完整使用指南—目录 一、init 系统简介二、init 5 的含义与作用三、不同 Init 系统下的 init 5 行为1. SysVinit&#xff08;如 CentOS 6、Debian 7&#xff09;2. systemd&#xff08;如 CentOS 7、Ubuntu 16.04&#xff09;3. Upstart&#xff08;如 …...

uni-app中map的使用

uni-app中map的使用 一、基本使用步骤 1. 引入 map 组件 在 .vue 文件的 template 中直接使用 <map> 标签&#xff1a; <template><view><map :latitude"latitude" :longitude"longitude" :markers"markers" style&quo…...