一直两个坐标点求旋转角度,或已知角度求移动的方向和位置

namespace Pioneer
{
 
export class Game extends Behaviour{
private nState:number;
private Camera:Camera2D;
 
nScore:number;
nGold:number;
private nRotationAngle:number = 0;
private nRotationCount:number = 0;

private nFrameRateTimer:number;
private nScoreTimer:number;
private bLoaded:boolean;

private Bg:any;
private Ball:any;
private Player1:any;
private Player2:any;
private Player1Base:any;
private Player2Base:any;
private Btn1:any;
private YuanStage:any;

private vSpeed:any;
private nPlayer1PosX:number = Laya.stage.width/2;
private nPlayer1PosY:number = Laya.stage.height/2+260;
private nPlayer2ToBallDistance:number;
private nBallPos:any;
private nPlayer2Pos:any;
private nOffsetX:any;
private nOffsetY:any;
private nMoveTimer:number = 0;
private bisPlayer2Rotate:boolean = true;
private nPlayer2Angle:number = 0;
private nBallDestroyCount:number = 0;
private nYuanStagePos:any;
private nPlayer1Pos:any;

private Pawn:any;
private PawnOffset:any;
private bDrag = false;

private Scene:Laya.Sprite;
private GameField:Laya.Rectangle;

private nEndTimer = -1;
YCenter = 0;
super()
{
this.bLoaded = false;
this.nFrameRateTimer = 0;
}
Start()
{
this.vSpeed = new EVector3(1, 1, 1);
this.OnAssetLoaded();
}
OnAssetLoaded()
{
this.bLoaded = true;
this.Scene = new Laya.Sprite();
this.Scene.hitArea = new Laya.Rectangle(0, 0, Laya.stage.width, Laya.stage.height);

this.InitEvents(this.Scene);
 
SceneManager.SetStage(this.Scene);
Laya.stage.addChildAt(this.Scene, 0);
 
this.Bg = new Entity(1);
this.Bg.Show();
this.Bg.SetPosition(Laya.stage.width / 2, Laya.stage.height / 2);

this.Ball = new Ball(4);
this.Ball.Show();
this.Ball.SetPosition(Laya.stage.width / 2, Laya.stage.height / 2 + 260 );
this.Ball.SetLayer(4);

this.YuanStage = new Entity(10);
this.YuanStage.Show();
this.YuanStage.SetPosition(Laya.stage.width / 2, Laya.stage.height / 2 + 260);
this.YuanStage.SetLayer(1);

this.Player1 = new Player(5, Laya.stage.width / 2, Laya.stage.height / 2 + 520, 4);
this.Pawn=this.Player1;

this.Player2 = new Player(6, Laya.stage.width / 2, Laya.stage.height / 2, 4 );

this.Player1Base = new Player(11, Laya.stage.width / 2, Laya.stage.height / 2 + 520, 3);

this.Player2Base = new Player(12, Laya.stage.width / 2, Laya.stage.height / 2, 3);

this.nPlayer1PosX = Laya.stage.width / 2;
this.nPlayer1PosY = Laya.stage.height / 2 + 520;
this.nYuanStagePos = this.YuanStage.GetPosition();

this.ResetGame();
this.StartGame();
}

StartGame()
{
DlgBattle.GetInstance().Show(true);
}
GetPlayerScore(id)
{
if (id == 1) {
return this.Player1.nPlayerScore;
}
else {
return this.Player2.nPlayerScore;
}
}

CreatBall()
{
this.Ball = new Ball(4);
this.Ball.Show();
this.Ball.SetPosition(this.Ball.RandamPosX(), this.Ball.RandamPosY());
this.Ball.SetLayer(4);
}

CreatPlayer1()
{
this.Player1 = new Player(5, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 4 );
this.Pawn = this.Player1;
}

CreatPlayer2()
{
this.Player2 = new Player(6, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 4 );
}

CreatPlayer1Base()
{
this.Player1Base = new Player(11, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 3);
}

CreatPlayer2Base()
{
this.Player2Base = new Player(12, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 3 );
}

ResetGame()
{
this.nGold = 0;
this.nScore = 0;
}

Update()
{
if(this.nRotationCount % 2 == 0)
{
this.nRotationAngle++;
this.Pawn.SetRotation(this.nRotationAngle);
}
else
{
this.nRotationAngle--;
this.Pawn.SetRotation(this.nRotationAngle);
}
if(this.Ball.Obj == null)
{
this.bisPlayer2Rotate = true;
this.nBallDestroyCount++;
this.CreatBall();
}

this.nBallPos = this.Ball.GetPosition();
this.nPlayer2Pos = this.Player2.GetPosition();
this.nPlayer1Pos = this.Player1.GetPosition();
let nStageToBallPos = Math.pow(this.nBallPos.x - this.nYuanStagePos.x, 2) + Math.pow(this.nBallPos.y - this.nYuanStagePos.y, 2);
let nStageToPlayer1Pos = Math.pow(this.nPlayer1Pos.x - this.nYuanStagePos.x, 2) + Math.pow(this.nPlayer1Pos.y - this.nYuanStagePos.y, 2);
let nStageToPlayer2Pos = Math.pow(this.nPlayer2Pos.x - this.nYuanStagePos.x, 2) + Math.pow(this.nPlayer2Pos.y - this.nYuanStagePos.y, 2);
 
if(nStageToPlayer1Pos > 90000)
{
this.Player1.Destroy();
this.Player1Base.Destroy();
this.nPlayer1PosX = Laya.stage.width / 2;
this.nPlayer1PosY = Laya.stage.height / 2 + 260;
this.CreatPlayer1();
this.CreatPlayer1Base();
}

if(nStageToPlayer2Pos > 90000)
{
this.Player2.Destroy();
this.Player2Base.Destroy();
this.nPlayer1PosX = Laya.stage.width / 2;
this.nPlayer1PosY = Laya.stage.height / 2 + 260;
this.CreatPlayer2();
this.CreatPlayer2Base();
}

if(nStageToBallPos > 90000)
{
this.Ball.Destroy();
this.CreatBall();
}

this.nPlayer2ToBallDistance = Math.pow(this.nBallPos.x - this.nPlayer2Pos.x, 2) + Math.pow(this.nBallPos.y - this.nPlayer2Pos.y, 2);
this.nOffsetX = this.nBallPos.x - this.nPlayer2Pos.x;
this.nOffsetY = this.nBallPos.y - this.nPlayer2Pos.y;

let angle = Math.atan2(this.nOffsetY, this.nOffsetX) * 180 / Math.PI + 90;
if(angle <= 0){
angle = 360 + angle;
}
 
if(this.bisPlayer2Rotate)
{
if(this.nBallDestroyCount % 2 == 0){
this.nPlayer2Angle = this.nPlayer2Angle % 360 + 1;
}
if(this.nBallDestroyCount % 2 == 1)
{
this.nPlayer2Angle = this.nPlayer2Angle % 360 - 1 ;
if(this.nPlayer2Angle <= 0){
this.nPlayer2Angle = 360 + this.nPlayer2Angle;
}
}
 
if(Math.abs(this.nPlayer2Angle - Math.abs(angle)) < 1){
this.bisPlayer2Rotate = false;
}

this.Player2.SetRotation(this.nPlayer2Angle);

if(!this.bisPlayer2Rotate){
this.Player2.SetRotation(angle);
}

}
 
this.nMoveTimer++;

if(this.nMoveTimer % 20 == 0 && !this.bisPlayer2Rotate)
{
this.Player2.SetPosition(this.nPlayer2Pos.x + this.nOffsetX / 5, this.nPlayer2Pos.y + this.nOffsetY / 5);
this.Player2Base.SetPosition(this.nPlayer2Pos.x + this.nOffsetX / 5, this.nPlayer2Pos.y + this.nOffsetY / 5);
}
 
if(this.Player2.TestCollider(this.Ball))
{
this.Ball.Destroy();
this.Player2.nPlayerScore++;
console.log("玩家2的分数" + this.Player2.nPlayerScore);

if (DlgBattle.IsInstanced()) {
DlgBattle.GetInstance().UpdateLifeList();
}
}

}
OnMouse(e:Laya.Event)
{
if (!this.Pawn) {
return;
}
if (e.type == Laya.Event.MOUSE_DOWN)
{
this.nRotationCount++;
if (this.Bg.TestContains(e.stageX, e.stageY))
{
//this.Pawn.SetScale(1.2,1.2);
let x1 = Math.cos((this.nRotationAngle - 90) * Math.PI / 180);
let y1 = Math.sin((this.nRotationAngle - 90) * Math.PI / 180);
this.Pawn.SetPosition(this.nPlayer1PosX += x1 * 30,this.nPlayer1PosY += y1 * 30);
this.Player1Base.SetPosition(this.nPlayer1PosX, this.nPlayer1PosY);
 
if(this.Pawn.TestCollider(this.Ball))
{
this.Ball.Destroy();
this.Player1.nPlayerScore++;
console.log("玩家1的分数" + this.Player1.nPlayerScore);
 
if (DlgBattle.IsInstanced()) {
DlgBattle.GetInstance().UpdateLifeList();
}
}
}

}
else if(e.type == Laya.Event.MOUSE_UP){
//this.Pawn.SetScale(1, 1);
}
else if (e.type == Laya.Event.MOUSE_OUT) {
}
}

 
}
}
 
 
 

原文地址:https://www.cnblogs.com/StevenChancxy/p/9171657.html