/// <summary> /// 放到地图指定坐标, /// </summary> /// <param name="randomXS"></param> public void MPutToMapCoor(GameCoordinate targetGc, bool NeedGoThere) { Point po_nowdeskzuobiao = new Point(); string cityName = GetNowMap().CityName; City city = new City() { CityName = cityName, MapMaxGameCoor = new GameCoordinate(191, 119), MapSize = new int[2] { 441, 276 } }; po_nowdeskzuobiao = MPutToMayMapCenter(city, targetGc); if (NeedGoThere) { WindowAPI.MMouseClick(1); } try { MPutToMap(city, targetGc, NeedGoThere); } catch (HengTimeOutException ex) { if (ex.GetError() == "restart") { MPutToMapCoor(targetGc, NeedGoThere);//todo思考问题是否有 } } }
/// <summary> /// 与实际值超过40就异常的 /// </summary> /// <param name="city"></param> /// <param name="po_nowdeskzuobiao"></param> /// <param name=""></param> public bool IsErrorRange(GameCoordinate result, GameCoordinate targetGC_JQ, int time) { //超过20就异常 int WuCha = 0; switch (time) { case 1: WuCha = 890; break; case 2: case 3: WuCha = 40; break; case 4: case 5: WuCha = 40; break; default: WuCha = 40; break; } if (Math.Abs(targetGC_JQ.X - result.X) > WuCha || Math.Abs(targetGC_JQ.Y - result.Y) > WuCha) { return(true); } else { return(false); } }
public ChessMovements(GameCoordinate initCoordinate) { _initCoordinate = initCoordinate; var coordinate = initCoordinate.ToString(); _initLetter = coordinate[0].ToString(); _initNumber = coordinate[1].ToString(); _initValue = (int)initCoordinate; }
/// <summary> /// 如出现在边界外,获取该值的目标坐标并且返回可能的实际坐标值 /// </summary> /// <param name="po"></param> /// <param name="wantpo"></param> /// <param name="realpo"></param> /// <returns></returns> private void DoMove(GameCoordinate wantpo) { //确定移动的方向 int[] arr = { 0, 0 }; if (wantpo.X > (int)(548 / 2)) //x轴方向 { arr[0] = 1; //右移动 } else if (wantpo.X < (int)(548 / 2)) { arr[0] = -1;//左移动 } else { arr[0] = 0; //不移动 } if (wantpo.Y > (int)(278 / 2)) //y轴方向 { arr[1] = 1; //上移动 } else if (wantpo.Y < (int)(278 / 2)) { arr[1] = -1;//下移动 } else { arr[1] = 0;//不移动 } //推测4个象限的落点可能性 //TODO(是否需要判断多次),x轴右边的,则给他目标值偏差30px int x = 0; if (arr[0] > 0) { //TODO(548和1),对应地图移动距离 x = (548 - wantpo.X) * 1 - 30 * 1; } if (arr[0] < 0) { x = 30 * 1 - wantpo.X * 1; } int y = 0; if (arr[1] > 0) { //TODO(278和1),对应地图移动距离 y = -1 * ((278 - wantpo.Y) * 1 - 30 * 1); } if (arr[1] < 0) { x = -1 * (30 * 1 - wantpo.Y * 1); } //移动 WindowAPI.MMouseMove(0, x, y); }
private GameCoordinate GetExitPoint(IReadOnlyList <int> list) { var exitPoint = new GameCoordinate { CoordX = list[0], CoordY = list[1] }; return(exitPoint); }
/** * 執行敵人的動作(每個拍點執行一次,先執行玩家動作,再執行敵人動作) * 敵人的動作包含 移動現有攻擊、對玩家造成傷害、發動新攻擊 三個部分 * 1.移動現有攻擊:所有攻擊物件都移動到下一圈的同一格 * 2.對玩家造成傷害:若玩家所在位置有攻擊物件,會減低玩家的生命值 * 3.發動新攻擊:玩家在最外圈時生成 0 個攻擊物件,玩家每前進一步,生成的攻擊物件的數量加 1; * 隨機決定這些攻擊物件要落在第 -3 圈的哪幾格(玩家的活動範圍只到第 0 圈,攻擊物件可在第 0 圈之內) */ private void BossAttack() { var objectMapKeys = new List <GameObject>(objectMap.Keys); var candidates = new List <int>(); var numberOfNewAttack = GameCoordinate.GetSizeOfM() - objectMap[player].M() - 1; foreach (var key in objectMapKeys) { if (key == player) { continue; } var newPosition = new GameCoordinate( objectMap[key].M() + 1, objectMap[key].N(), true ); if (newPosition.M() < GameCoordinate.GetSizeOfM()) { objectMap[key] = newPosition; key.transform.position = newPosition.ToCartesianCoordinate(); var playerAttacked = newPosition.Equals(objectMap[player]); if (playerAttacked) { Debug.Log("玩家被攻擊!"); playerHealth -= 1; } } else { objectMap.Remove(key); Destroy(key); } } for (var i = 0; i < GameCoordinate.GetSizeOfN(); i++) { candidates.Add(i); } for (var i = 0; i < numberOfNewAttack; i++) { var index = Random.Range(0, candidates.Count); var attackPosition = new GameCoordinate(-3, candidates[index], true); var newBossAttackMarker = Instantiate( bossAttackMarker, attackPosition.ToCartesianCoordinate(), Quaternion.identity ); objectMap.Add(newBossAttackMarker, attackPosition); candidates.RemoveAt(index); if (candidates.Count == 0) { break; } } }
public void ConvertToGameCoordinateTest_1() { // Arrange GameCoordinate _coordinate = new Model.GameCoordinate() { X = XCoordinate.EIGHT, Y = YCoordinate.D }; // Act var _equivalentCoordinate = GameCoordinate.ConvertToGameCoordinate("8", "4"); //Assert Assert.IsTrue(_coordinate.Equals(_equivalentCoordinate)); }
private void Start() { detectedActions = new Queue <PlayerAction>(); // set beat spawn delay beatSpawnEarly = beatPool.GetSpawnEarly(); gameMusic = GetComponent <AudioSource>(); gameMusic.loop = true; // if (!gameMusic.playOnAwake) gameMusic.Play(); //showTransform = showTransformCarrier.GetComponent<ShowTransform>(); sampleLight = sampleLightCarrier.GetComponent <Light>(); // 測試用,音樂完成後應替換成真實資料(單位:秒)// Test, change 5 to 2, change 60 to 200 /* for (float i = 5; i <= 200; i += 2) * { * beats.Enqueue(i - 0.2f); * atkBeats.Enqueue(i - 0.5f); * } */ // 各個拍點分別需要從哪個方向攻擊敵人 while (sols.Count < beats.Count) { // 隨機產生一個方向,相同方向重覆八次(僅供參考,歡迎修改) var nextAction = (PlayerAction)Random.Range( (int)PlayerAction.AttackUp, (int)PlayerAction.AttackRight + 1 ); for (var _ = 0; _ < 8; _++) { sols.Enqueue(nextAction); } } var initialPosition = new GameCoordinate(5, 0); objectMap.Add(player, initialPosition); player.transform.position = initialPosition.ToCartesianCoordinate(height); player.transform.LookAt(Vector3.zero); // Show all possible positions ShowAllPosition(); playerHealth = maxPlayerHealth; bossHealth = maxBossHealth; InvokeRepeating(nameof(RecoverHealth), 0.5f, 0.5f); UpdateUI(); }
public void ConvertToGameCoordinateTest() { // Arrange GameCoordinate _coordinate = new Model.GameCoordinate() { X = XCoordinate.EIGHT, Y = YCoordinate.D }; Point _point = new Point(8, 4); // Act var _equivalentCoordinate = GameCoordinate.ConvertToGameCoordinate(_point); //Assert Assert.IsTrue(_coordinate.Equals(_equivalentCoordinate)); }
/// <summary> /// 放到地图指定坐标TODO,少了个系数!!! /// </summary> /// <param name="randomXS"></param> private Point MPutToMayMapCenter(City city, GameCoordinate targetGc) { //20为误差阈值,其实阈值15已经很高了,不会超过20的! //计算预估坐标 //TODO 15这个觉得没什么大问题,测试多次能正确放置 int numX = (int)(city.MapMaxGameCoor.X / 15); int numY = (int)(city.MapMaxGameCoor.Y / 15); int x = 0; int y = 0; //如果4个点均是在内切20px范围的,直接转移进入 if (targetGc.X > numX && (Math.Abs(city.MapMaxGameCoor.X - targetGc.X) > numX)) { x = (int)((targetGc.X - city.MapCenterCoor.X) * city.XS) + CenterPoint.X; } else { if (targetGc.X <= numX) { x = (int)((targetGc.X - city.MapCenterCoor.X) * city.XS) + CenterPoint.X + (int)((numX - targetGc.X) * city.XS); } else { x = (int)((targetGc.X - city.MapCenterCoor.X) * city.XS) + CenterPoint.X - (int)((numX - city.MapMaxGameCoor.X + targetGc.X) * city.XS); } } if (targetGc.Y > numY && (Math.Abs(city.MapMaxGameCoor.Y - targetGc.Y) > numY)) { y = CenterPoint.Y - (int)((targetGc.Y - city.MapCenterCoor.Y) * city.XS); } else { if (targetGc.Y <= numY) { y = CenterPoint.Y - (int)((targetGc.Y - city.MapCenterCoor.Y) * city.XS) - (int)((numY - targetGc.Y) * city.XS); } else { y = CenterPoint.Y - (int)((targetGc.Y - city.MapCenterCoor.Y) * city.XS) + (int)((numY - city.MapMaxGameCoor.Y + targetGc.Y) * city.XS); } } //mayPoint = new Point(CenterPoint.X + targetGc.X - city.MapCenterCoor.X, city.MapCenterCoor.Y - targetGc.Y + CenterPoint.Y); WindowAPI.MMouseMoveTo(0, x, y); Thread.Sleep(100); return(new Point(x, y)); }
// Show all possible positions private void ShowAllPosition() { for (var i = 0; i < GameCoordinate.GetSizeOfM(); ++i) { for (var j = 0; j < GameCoordinate.GetSizeOfN(); ++j) { var position = new GameCoordinate(i, j); var newCoordinateMark = Instantiate( coordinateMark, position.ToCartesianCoordinate(), coordinateMark.transform.rotation, coordinateMark.transform.parent ); newCoordinateMark.SetActive(true); } } }
private List <GameCoordinate> GetMineCoordinates(string line) { var list = _readDataService.GetPairNumbersList(line); var mineCoordinates = new List <GameCoordinate>(); foreach (var item in list) { var mine = new GameCoordinate { CoordX = item.Item1, CoordY = item.Item2 }; mineCoordinates.Add(mine); } return(mineCoordinates); }
/// <summary> /// 鼠标放到NPC上面 /// </summary> /// <param name="gc"></param> /// <returns></returns> public bool PointPutToGameCoor(GameCoordinate targetGC) { //直接获取中心点坐标做偏移量 GameCoordinate nowGC = GetNowMap().coor; int x = targetGC.X - nowGC.X; int y = targetGC.Y - nowGC.Y; //已经在该坐标的话返回 if (x == 0 && y == 0) { return(true); } if (Math.Abs(x) < 10 && Math.Abs(y) < 10) { MMoveToRealCoor(x, -y); } return(true); }
/// <summary> /// 做了一堆容错,百度坑爹爹爹,相对稳定了 /// </summary> /// <returns></returns> private void MPutToMap(City city, GameCoordinate targetGC_JQ, bool NeedGoThere) { try { Point po_nowdeskzuobiao = new Point(); WindowAPI.GetCursorPos(out po_nowdeskzuobiao); //获取当前的游戏坐标 GameCoordinate nowGameCoor = MapOcr(city, targetGC_JQ); int x = targetGC_JQ.X - nowGameCoor.X; //要移动的距离 int y = targetGC_JQ.Y - nowGameCoor.Y; //要移动的距离 //获取到理想的地址 Point po_targetwdeskzuobiao = new Point((int)(po_nowdeskzuobiao.X + x * city.XS), (int)(po_nowdeskzuobiao.Y - y * city.XS)); //前往该地址 WindowAPI.MMouseMoveTo(0, po_targetwdeskzuobiao.X, po_targetwdeskzuobiao.Y); if (NeedGoThere) { WindowAPI.MMouseClick(1); } new GameCommonUtil().ThreadRest(2); //------------------ //截图 nowGameCoor = MapOcr(city, targetGC_JQ); if (nowGameCoor.X == -1) { Console.WriteLine("没颜色,重新来"); throw new HengTimeOutException("没颜色,重新来"); } if (Math.Abs(nowGameCoor.X - targetGC_JQ.X) < 3 && Math.Abs(nowGameCoor.Y - targetGC_JQ.Y) < 3) { Console.WriteLine("跳出"); return; } Console.WriteLine("进入循环 x::" + Math.Abs(nowGameCoor.X - targetGC_JQ.X) + "y:" + Math.Abs(nowGameCoor.Y - targetGC_JQ.Y)); //递归该方法 MPutToMap(city, targetGC_JQ, NeedGoThere); } catch (HengTimeOutException ex) { throw new HengTimeOutException(ex.GetError()); } }
/// <summary> /// move方法,每次move下,1格格move回 /// </summary> /// <param name="city"></param> /// <param name="targetGC_JQ"></param> public void Move11Step(City city, GameCoordinate targetGC_JQ, ref int times) { //看下目标点的落点,理论落点 if (targetGC_JQ.X > city.MapCenterCoor.X && targetGC_JQ.Y > city.MapCenterCoor.Y) { //第一象限,看下与最大值的哪个差的多,做左下方移动 if (Math.Abs(city.MapMaxGameCoor.X - targetGC_JQ.X) < Math.Abs(city.MapMaxGameCoor.Y - targetGC_JQ.Y)) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, -2, 0); times++; if (times > 5) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 0, -2); times++; } } else { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 0, -2); times++; if (times > 30) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, -2, 0); times++; } } } else if (targetGC_JQ.X <= city.MapCenterCoor.X && targetGC_JQ.Y > city.MapCenterCoor.Y) { //第二象限 //第一象限,看下与最大值的哪个差的多,做左下方移动 if (Math.Abs(targetGC_JQ.X - 0) < Math.Abs(city.MapMaxGameCoor.Y - targetGC_JQ.Y)) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 2, 0); times++; if (times > 30) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 0, -2); times++; } } else { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 0, -2); times++; if (times > 30) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 2, 0); times++; } } } else if (targetGC_JQ.X <= city.MapCenterCoor.X && targetGC_JQ.Y <= city.MapCenterCoor.Y) { //第三象限右上 if (Math.Abs(targetGC_JQ.X - 0) < Math.Abs(city.MapMaxGameCoor.Y - 0)) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 2, 0); times++; if (times > 30) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 0, 2); times++; } } else { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 0, 2); times++; if (times > 30) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 2, 0); times++; } } } else { //第4象限坐上 if (Math.Abs(city.MapMaxGameCoor.X - targetGC_JQ.X) < Math.Abs(city.MapMaxGameCoor.Y - 0)) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, -2, 0); times++; if (times > 30) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 0, 2); times++; } } else { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, 0, 2); times++; if (times > 30) { //大概率y轴方向,则延y轴移动2格子,移动 WindowAPI.MMouseMove(0, -2, 0); times++; } } } }
private void FixedUpdate() { if (!isStartPlay) { return; } // UI show beat, by 冠群 if (beats.Count != 0 && gameMusic.time >= beats.Peek() - beatSpawnEarly) { beats.Dequeue(); // 1 is from right to left,移動我預設是左到右 beatPool.ReuseByDirection(1); } // UI show atkBeats, by 文胤 if (atkBeats.Count != 0 && gameMusic.time >= atkBeats.Peek() - beatSpawnEarly) { float time = atkBeats.Dequeue(); // -1 is from left to right,攻擊我預設是右到左 beatPool.ReuseByDirection(-1); // 不用在這邊生成刀刻了,在開始的時候已經生成好了 //GameObject obj = Instantiate(SwordPathObj, Vector3.zero, Quaternion.identity); //SwordPathGen temp = new SwordPathGen(time); //obj.GetComponent<SwordPathManagement>().SetAnswer(temp.GetAnswer()); } /* * if (beats.Count != 0 && gameMusic.time >= beats.Peek()) { * // 下一個拍點到了 * beats.Dequeue(); * // 啟動玩家動作偵測 * showTransform.turnedOn = true; * // 變換燈光顏色(示範) * sampleLight.color = colorsForBeats[colorsIndex]; * colorsIndex = (colorsIndex + 1) % 3; * } */ if (detectedActions.Count != 0) { // 處理玩家的動作 GameCoordinate newPosition; var hurtableAction = (sols.Count != 0) ? sols.Dequeue() : PlayerAction.NoAction; // Note: sols.Count == 0 should not happen var playerAction = detectedActions.Dequeue(); switch (playerAction) { case PlayerAction.MoveFront: newPosition = new GameCoordinate( objectMap[player].M() - 1, objectMap[player].N() ); objectMap[player] = newPosition; StartCoroutine(MoveSmoothly(newPosition.ToCartesianCoordinate(height))); break; case PlayerAction.MoveBack: newPosition = new GameCoordinate( objectMap[player].M() + 1, objectMap[player].N() ); objectMap[player] = newPosition; StartCoroutine(MoveSmoothly(newPosition.ToCartesianCoordinate(height))); break; case PlayerAction.MoveLeft: newPosition = new GameCoordinate( objectMap[player].M(), objectMap[player].N() - 1 ); objectMap[player] = newPosition; StartCoroutine(MoveSmoothly(newPosition.ToCartesianCoordinate(height))); break; case PlayerAction.MoveRight: newPosition = new GameCoordinate( objectMap[player].M(), objectMap[player].N() + 1 ); objectMap[player] = newPosition; StartCoroutine(MoveSmoothly(newPosition.ToCartesianCoordinate(height))); break; case PlayerAction.AttackUp: case PlayerAction.AttackDown: case PlayerAction.AttackLeft: case PlayerAction.AttackRight: if (objectMap[player].M() == 0 && hurtableAction == playerAction) { Debug.Log("敵人被攻擊!"); bossHealth -= 1; } break; case PlayerAction.NoAction: break; } // 敵人移動現有攻擊、對玩家造成傷害、發動新攻擊 // BossAttack(); // Debug.Log(ObjectMapToString(objectMap)); } UpdateUI(); if (bossHealth <= 0.0001) { GameEnd(true); } else if (playerHealth <= 0.0001) { GameEnd(false); } }
public Pawn(GameCoordinate startingCoordinate) { StartingCoordinate = startingCoordinate; }
/// <summary> /// 根据桌面坐标获取返回当前坐标 /// </summary> /// <param name="po"></param> /// <returns></returns> public GameCoordinate MapOcr(City city, GameCoordinate targetGC_JQ) { string words = ""; int isize = 3; string[] strWords = { "0", "0" }; GameCoordinate nowgc = new GameCoordinate(0, 0); int times = 0; while (true) { if (times > 10) { throw new HengTimeOutException("restart"); } Point po_nowdeskzuobiao = new Point(); WindowAPI.GetCursorPos(out po_nowdeskzuobiao); if (isize < 8) { isize++; } try { new GameCommonUtil().ThreadRest(2); //获取到键盘的,然后截图大约是x-60 y-40 大小是120,80 Bitmap bm = PicUtil.GetScreen(po_nowdeskzuobiao.X - 50, po_nowdeskzuobiao.Y - 50, 100, 80); // 处理坐标图像 // 1、截图 // 2、分析点图,黄色图片,减少变换量 // 3、颜色反转 // 4、放大图片 List <Point> lisp = PicCorFinder.FindColor(bm, "#FFFF00", Rectangle.Empty, 0); if (lisp.Count == 0) { bm.Dispose(); return(new GameCoordinate(-1, -1)); } bm = PicUtil.CaptureImage(bm, new Point(lisp[0].X - 10, lisp[0].Y - 8), 60, 25); bm = PicUtil.ChangeColor(bm, Color.FromArgb(255, 255, 0)); //百度人工智能有一定错误率,主要95%集中在了,所以这边只要有,就放大图片重新识别,直到ok Bitmap bm2 = PicUtil.PicSized(bm, isize); JObject jobject = PicUtil.BaiDuOCR(bm2); JArray jarr = (JArray)jobject["words_result"]; //如果超出边界,移动移出来 if (jarr.Count == 0) { DoMove(targetGC_JQ); bm2.Dispose(); bm.Dispose(); continue; } bm.Dispose(); bm2.Dispose(); bool flag = false; for (int i = 0; i < jarr.Count; i++) { words = jarr[i]["words"].ToString(); flag = StringUtil.IsNumber(words.Replace(",", "")); if (flag) { break; } } Console.WriteLine("words:" + words); if (!words.Contains(',') || !flag) { //大概率没识别出来,就动下鼠标 Move11Step(city, targetGC_JQ, ref times); continue; } strWords = words.Split(','); nowgc = new GameCoordinate(Convert.ToInt32(strWords[0]), Convert.ToInt32(strWords[1])); //if (IsErrorRange(nowgc, targetGC_JQ, isize)) //{ // //大概率没识别出来,就动下鼠标 // Move11Step(city, targetGC_JQ, ref times); // continue; //} break; } catch (Exception) { //大概率没识别出来,就动下鼠标 Move11Step(city, targetGC_JQ, ref times); continue; } } return(nowgc); }