【grasshopper自定义电池开发】使用Visual Studio 2022借助官方扩展插件开发一个贪吃蛇电池
grasshopper自定義電池開發(fā)——貪吃蛇
- 一、 為什么使用Visual Studio 2022和C#?
- 二、.如何創(chuàng)建一個空grasshopper項目?
- a.下載并安裝Visual Studio
- b.安裝擴展并使用
- c. 添加依賴
- d. 添加GH電池目錄
- 三、貪吃蛇電池
- a. 功能
- b.具體代碼
- c.使用方法及與效果
- 參考資料
一、 為什么使用Visual Studio 2022和C#?
Visual Studio中擁有官方的grasshopper擴展插件,可以提供一個模板快速開發(fā)。
C#是grasshopper的原生語言,該使用語言開發(fā)擴展性,兼容性最好。
二、.如何創(chuàng)建一個空grasshopper項目?
a.下載并安裝Visual Studio
b.安裝擴展并使用
4.添加新項目后,會要求輸入一些電池相關(guān)的信息。如下:
1.Add-on display name :決定了這個電池在GH插件加載的時候顯示的名字。
2.Component class :是C#的類的名字,與GH顯示無關(guān),這里可以使用默認值,任何不能作為C#的類名的字符都不可以出現(xiàn)在這里。
3.Name :是該電池在GH里會顯示的名字。
4.Nickname :是該電池在GH里選擇顯示簡稱時的名字。
5.Category :是該電池在GH里會被放在的大類的名字,比如Params、Math、Sets等。GH如果本身不存在輸入的大類的名字的話,GH會為這個電池單獨創(chuàng)建一個大類。在這里我們可以填入MyComp,并且以后所有的電池都可以填相同的大類,這樣方便我們找到自己創(chuàng)建的電池。
6.Subcategory:是該電池在GH里會被放在大類下的哪個子類里。
7.Description :是該電池在GH里顯示的額外描述性信息。
c. 添加依賴
進行修改。
修改并保存后,可以發(fā)現(xiàn),錯誤已經(jīng)消失了。此時空項目便做好了,可以正常編譯。
d. 添加GH電池目錄
GH的電池需要放置在特定目錄才能被加載,我們將工程目錄添加到GH電池目錄,GH就可以自動加載最新的電池使用。
首先打開Rhino,在命令欄里輸入GrasshopperDeveloperSettings
1.把 Memory load *.GHA assemblies using COFF byte arrays 這個復(fù)選框前的鉤去掉,該選項會影響我們代碼的斷點調(diào)試
2.點擊 Add Folder
3.點擊右側(cè)的 … , 將我們剛剛的代碼的存儲位置下的 bin 文件夾選中,這樣GH就知道去這個文件夾找到剛剛我們生成的 gha 文件了。
三、貪吃蛇電池
a. 功能
b.具體代碼
類成員變量:
public sealed class Dir{public static readonly Dir Up = new Dir("Up");public static readonly Dir Down = new Dir("Down");public static readonly Dir Left = new Dir("Left");public static readonly Dir Right = new Dir("Right");private Dir(string value){Name = value;}public string Name { get; private set; }}Point3d headPoint = new Point3d(0, 0, 0);double headXinPlane = 0;double headYinPlane = 0;List<Point3d> bodyPoints = new List<Point3d>() { new Point3d(0, 0, 0) };int seed = 0;Circle lastFood = new Circle(1);List<Curve> lastSnake = new List<Curve>();int nowdirection = 0;定義輸入:
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager){pManager.AddRectangleParameter("rectangle", "rec", "蛇運動平面", GH_ParamAccess.item);pManager.AddIntegerParameter("xDivideNumber", "xDiv", "x軸分割的塊數(shù)", GH_ParamAccess.item);pManager.AddIntegerParameter("yDivideNumber", "yDiv", "y軸分割的塊數(shù)", GH_ParamAccess.item);pManager.AddBooleanParameter(Dir.Up.Name, Dir.Up.Name, "連接一個按鈕來讓蛇向+y方向轉(zhuǎn)", GH_ParamAccess.item);pManager.AddBooleanParameter(Dir.Down.Name, Dir.Down.Name, "連接一個按鈕來讓-y方向轉(zhuǎn)", GH_ParamAccess.item);pManager.AddBooleanParameter(Dir.Left.Name, Dir.Left.Name, "連接一個按鈕來讓蛇向+x方向轉(zhuǎn)", GH_ParamAccess.item);pManager.AddBooleanParameter(Dir.Right.Name, Dir.Right.Name, "連接一個按鈕來讓蛇向-x方向轉(zhuǎn)", GH_ParamAccess.item);pManager.AddBooleanParameter("Reset", "Reset", "連接一個按鈕來重置游戲.", GH_ParamAccess.item);}定義輸出:
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager){pManager.AddCurveParameter("Snake", "Snake", "蛇身體的曲線", GH_ParamAccess.list);pManager.AddCircleParameter("Food", "Food", "事物的圓", GH_ParamAccess.item);}邏輯:
protected override void SolveInstance(IGH_DataAccess DA){//Inputbool[] dirs = { false, false, false, false };bool zReset = false;int xMax = 0;int yMax = 0;Rectangle3d rec = new Rectangle3d();//為了避免方便設(shè)計避免掉頭邏輯,使用上左下右的排列順序bool judge = DA.GetData(Dir.Up.Name, ref dirs[0]);judge &= DA.GetData(Dir.Down.Name, ref dirs[2]);judge &= DA.GetData(Dir.Left.Name, ref dirs[1]);judge &= DA.GetData(Dir.Right.Name, ref dirs[3]);bool judge2 = DA.GetData("Reset", ref zReset);DA.GetData("xDivideNumber", ref xMax);DA.GetData("yDivideNumber", ref yMax);DA.GetData("rectangle", ref rec);Plane plane = rec.Plane;//rec.pointAt()使用的是百分數(shù),故直接除以1double xStep = 1d / xMax;double yStep = 1d / yMax;//間隔double step = 0;//半徑double r = 1;if (xStep > yStep){step = yStep;r = yStep * rec.Height / 2;}else{step = xStep;r = xStep * rec.Width / 2;}AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, r + " " + xStep + " " + yStep + " " + step);if (!judge) AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "請連接一個按鈕來控制方向");if (!judge2) AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "請連接一個按鈕來復(fù)原");AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "記得加上Trigger. 建議間隔:500ms.");List<Curve> bodies = new List<Curve>();Random foodRandomNumber = new Random(seed);//Resetif (zReset){headXinPlane = 0;headYinPlane = 0;headPoint = rec.PointAt((headXinPlane + 0.5) * xStep, (headYinPlane + 0.5) * yStep);bodyPoints = new List<Point3d>() { headPoint };return;}//Directionfor (int i = 0; i < dirs.Length; i++){if (dirs[i]){if (nowdirection != i && (nowdirection + 2) % dirs.Length != i){//只有在往另外一個軸進行移動,才算有效移動nowdirection = i;}//避免在多次輸入的情況下快速刷新,只更新當前方向就返回DA.SetData("Food", lastFood);DA.SetDataList("Snake", lastSnake);//return;}}bodyPoints.Add(headPoint);if (nowdirection % 2 == 0){//此時是上或下if (nowdirection == 0){headYinPlane += 1;}else{headYinPlane -= 1;}//超出邊界,從另一邊回歸if (headYinPlane >= yMax){headYinPlane = 0;}else if (headYinPlane < 0){headYinPlane = yMax - 1;}}else{if (nowdirection == 1){headXinPlane += 1;}else{headXinPlane -= 1;}if (headXinPlane >= xMax){headXinPlane = 0;}else if (headXinPlane < 0){headXinPlane = xMax - 1;}}headPoint = rec.PointAt((headXinPlane + 0.5) * xStep, (headYinPlane + 0.5) * yStep);//Food section belowdouble foodRandomX = foodRandomNumber.Next(0, xMax - 1);double foodRandomY = foodRandomNumber.Next(0, yMax - 1);Point3d foodPoint = rec.PointAt((foodRandomX + 0.5) * xStep, (foodRandomY + 0.5) * yStep);Circle FoodCircle = new Circle(plane, foodPoint, r);//Eating judgeif (foodPoint.DistanceTo(headPoint) <= 2 * r) { seed++; }else{bodyPoints.RemoveRange(0, 1);}foreach (Point3d point in bodyPoints){Circle body = new Circle(plane, point, r);bodies.Add(body.ToNurbsCurve());}Curve[] zSnake = Curve.CreateBooleanUnion(bodies, 1);List<Curve> output = new List<Curve>(zSnake);//OutputDA.SetData("Food", FoodCircle);DA.SetDataList("Snake", output);lastFood = FoodCircle;lastSnake = output;}c.使用方法及與效果
調(diào)整Trigger的間隔可以改變蛇運動的快慢
參考資料
總結(jié)
以上是生活随笔為你收集整理的【grasshopper自定义电池开发】使用Visual Studio 2022借助官方扩展插件开发一个贪吃蛇电池的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第三十四期:花了一个星期,我终于把RPC
- 下一篇: 猎鹰spacex_我如何重新创建Spac