private void onWalkLineCompleted(WalkInstruction currentWalk, WalkLineInstruction currentLine, bool success) { currentWalk.CurrentLine = null; if (currentLine == null) { return; } currentLine.LastFrame = null; currentLine.OnCompletion.TrySetResult(success); }
private void onRepeatedlyExecute(object sender, AGSEventArgs args) { WalkLineInstruction currentLine = _currentWalkLine; if (currentLine == null) { return; } if (currentLine.CancelToken.IsCancellationRequested || currentLine.NumSteps <= 1f) { _currentWalkLine = null; //Possible race condition here? If so, need to replace with concurrent queue _lastFrame = null; _compensateScrollX = _compensateScrollY = 0f; currentLine.OnCompletion.TrySetResult(null); return; } if (_cutscene.IsSkipping) { _obj.X = currentLine.Destination.X; _obj.Y = currentLine.Destination.Y; _currentWalkLine = null; //Possible race condition here? If so, need to replace with concurrent queue _lastFrame = null; _compensateScrollX = _compensateScrollY = 0f; currentLine.OnCompletion.TrySetResult(null); return; } PointF walkSpeed = adjustWalkSpeed(WalkStep); float xStep = currentLine.XStep * walkSpeed.X; float yStep = currentLine.YStep * walkSpeed.Y; if (MovementLinkedToAnimation && _animation != null && _animation.Animation.Frames.Count > 1 && _animation.Animation.Sprite == _lastFrame) { //If the movement is linked to the animation and the animation speed is slower the the viewport movement, it can lead to flickering //so we do a smooth movement for this scenario. _obj.X += compensateForViewScrollIfNeeded(_obj.Room.Viewport.X, xStep, ref _compensateScrollX, ref _lastViewportX); _obj.Y += compensateForViewScrollIfNeeded(_obj.Room.Viewport.Y, yStep, ref _compensateScrollY, ref _lastViewportY); return; } if (_animation != null) { _lastFrame = _animation.Animation.Sprite; } _lastViewportX = _obj.Room.Viewport.X; currentLine.NumSteps -= Math.Abs(currentLine.IsBaseStepX ? xStep : yStep); if (currentLine.NumSteps >= 0f) { _obj.X += (xStep - _compensateScrollX); _obj.Y += (yStep - _compensateScrollY); } _compensateScrollX = _compensateScrollY = 0f; }
private async Task <bool> walkStraightLine(ILocation destination, CancellationTokenSource token, List <IObject> debugRenderers) { if (debugRenderers != null) { GLLineRenderer line = new GLLineRenderer(_glUtils, _translate.X, _translate.Y, destination.X, destination.Y); IObject renderer = _objFactory.GetObject("Debug Line"); renderer.CustomRenderer = line; await renderer.ChangeRoomAsync(_room.Room); debugRenderers.Add(renderer); } if (_cutscene.IsSkipping) { _translate.X = destination.X; _translate.Y = destination.Y; return(true); } if (!isDistanceVeryShort(destination)) { var lastDirection = _faceDirection.Direction; var walkAnimation = _outfit.Outfit[AGSOutfit.Walk]; bool alreadyWalking = _faceDirection.CurrentDirectionalAnimation == walkAnimation; _faceDirection.CurrentDirectionalAnimation = walkAnimation; await _faceDirection.FaceDirectionAsync(_translate.X, _translate.Y, destination.X, destination.Y); if (lastDirection != _faceDirection.Direction && alreadyWalking) { await Task.Delay(200); } } float xSteps = Math.Abs(destination.X - _translate.X); float ySteps = Math.Abs(destination.Y - _translate.Y); float numSteps = Math.Max(xSteps, ySteps); bool isBaseStepX = xSteps >= ySteps; float xStep = xSteps / numSteps; if (_translate.X > destination.X) { xStep = -xStep; } float yStep = ySteps / numSteps; if (_translate.Y > destination.Y) { yStep = -yStep; } WalkLineInstruction instruction = new WalkLineInstruction(token, numSteps, xStep, yStep, isBaseStepX, destination); _currentWalkLine = instruction; Task timeout = Task.Delay(WalkLineTimeoutInMilliseconds); Task completedTask = await Task.WhenAny(instruction.OnCompletion.Task, timeout); if (completedTask == timeout) { instruction.CancelToken.Cancel(); return(false); } if (instruction.CancelToken.IsCancellationRequested || !isWalkable(_translate.Location)) { return(false); } _translate.X = destination.X; _translate.Y = destination.Y; return(true); }
private void onRepeatedlyExecute() { WalkLineInstruction currentLine = _currentWalkLine; if (currentLine == null) { return; } if (currentLine.CancelToken.IsCancellationRequested || currentLine.NumSteps <= 1f || !isWalkable(_translate.Location) || _room?.Room != currentLine.Room) { _currentWalkLine = null; //Possible race condition here? If so, need to replace with concurrent queue _lastFrame = null; _compensateScrollX = _compensateScrollY = 0f; currentLine.OnCompletion.TrySetResult(null); return; } if (_cutscene.IsSkipping) { _translate.X = currentLine.Destination.X; _translate.Y = currentLine.Destination.Y; _currentWalkLine = null; //Possible race condition here? If so, need to replace with concurrent queue _lastFrame = null; _compensateScrollX = _compensateScrollY = 0f; currentLine.OnCompletion.TrySetResult(null); return; } PointF walkSpeed = adjustWalkSpeed(WalkStep); float xStep = currentLine.XStep * walkSpeed.X; float yStep = currentLine.YStep * walkSpeed.Y; if (MovementLinkedToAnimation && _animation != null && _animation.Animation.Frames.Count > 1 && _animation.Animation.Sprite == _lastFrame) { //If the movement is linked to the animation and the animation speed is slower the the viewport movement, it can lead to flickering //so we do a smooth movement for this scenario. var compensateX = _compensateScrollX; var compensateY = _compensateScrollY; var candidateX = _translate.X + compensateForViewScrollIfNeeded(_state.Viewport.X, xStep, ref compensateX, ref _lastViewportX); var candidateY = _translate.Y + compensateForViewScrollIfNeeded(_state.Viewport.Y, yStep, ref compensateY, ref _lastViewportY); if (isWalkable(new AGSLocation(candidateX, candidateY))) { _compensateScrollX = compensateX; _compensateScrollY = compensateY; _translate.X = candidateX; _translate.Y = candidateY; return; } } if (_animation != null) { _lastFrame = _animation.Animation.Sprite; } _lastViewportX = _state.Viewport.X; currentLine.NumSteps -= Math.Abs(currentLine.IsBaseStepX ? xStep : yStep); if (currentLine.NumSteps >= 0f) { _translate.X += (xStep - _compensateScrollX); _translate.Y += (yStep - _compensateScrollY); } _compensateScrollX = _compensateScrollY = 0f; }
private async Task <bool> walkStraightLine(WalkInstruction currentWalk, Position destination, List <IObject> debugRenderers) { if (_room?.Room != currentWalk.Room) { return(false); } if (debugRenderers != null) { IObject renderer = _objFactory.GetObject("Debug Line"); var line = renderer.AddComponent <GLLineRenderer>(); if (line != null) { line.X1 = _translate.X; line.Y1 = _translate.Y; line.X2 = destination.X; line.Y2 = destination.Y; } await renderer.ChangeRoomAsync(currentWalk.Room); debugRenderers.Add(renderer); } if (_cutscene.IsSkipping) { _translate.Position = destination; return(true); } if (!isDistanceVeryShort(destination)) { var lastDirection = _faceDirection.Direction; var walkAnimation = _outfit.Outfit[AGSOutfit.Walk]; bool alreadyWalking = _faceDirection.CurrentDirectionalAnimation == walkAnimation; _faceDirection.CurrentDirectionalAnimation = walkAnimation; await _faceDirection.FaceDirectionAsync(_translate.X, _translate.Y, destination.X, destination.Y); if (lastDirection != _faceDirection.Direction && alreadyWalking) { await Task.Delay(200); } } float xSteps = Math.Abs(destination.X - _translate.X); float ySteps = Math.Abs(destination.Y - _translate.Y); float numSteps = Math.Max(xSteps, ySteps); bool isBaseStepX = xSteps >= ySteps; float xStep = xSteps / numSteps; if (_translate.X > destination.X) { xStep = -xStep; } float yStep = ySteps / numSteps; if (_translate.Y > destination.Y) { yStep = -yStep; } WalkLineInstruction instruction = new WalkLineInstruction(numSteps, xStep, yStep, isBaseStepX, destination); currentWalk.CurrentLine = instruction; if (_currentWalk != currentWalk) { onWalkLineCompleted(currentWalk, instruction, false); currentWalk.CancelToken.Cancel(); return(false); } Task timeout = Task.Delay(WalkLineTimeoutInMilliseconds); Task completedTask = await Task.WhenAny(instruction.OnCompletion.Task, currentWalk.OnCompletion.Task, timeout); if (completedTask == timeout) { currentWalk.CancelToken.Cancel(); return(false); } if (completedTask == currentWalk.OnCompletion.Task) { return(false); } if (!instruction.OnCompletion.Task.Result || currentWalk.CancelToken.IsCancellationRequested || _room?.Room != currentWalk.Room || (!currentWalk.WalkAnywhere && !isWalkable(_translate.Position))) { return(false); } if (currentWalk.WalkAnywhere || isWalkable(destination)) { _translate.Position = destination; return(true); } return(false); }
private void onRepeatedlyExecute() { WalkInstruction currentWalk = getWalkInstruction(); WalkLineInstruction currentLine = currentWalk?.CurrentLine; if (currentLine == null) { return; } if (currentWalk.CancelToken.IsCancellationRequested || (!currentWalk.WalkAnywhere && !isWalkable(_translate.Position)) || _room?.Room != currentWalk.Room) { onWalkLineCompleted(currentWalk, currentLine, false); return; } if (currentLine.NumSteps <= 1f) { onWalkLineCompleted(currentWalk, currentLine, true); return; } if (_cutscene.IsSkipping) { _translate.Position = currentLine.Destination; onWalkLineCompleted(currentWalk, currentLine, true); return; } PointF walkSpeed = adjustWalkSpeed(WalkStep); float xStep = currentLine.XStep * walkSpeed.X; float yStep = currentLine.YStep * walkSpeed.Y; if (MovementLinkedToAnimation && _animation != null && _animation.Animation.Frames.Count > 1 && _animation.Animation.Sprite == currentLine.LastFrame) { //If the movement is linked to the animation and the animation speed is slower the the viewport movement, it can lead to flickering //so we do a smooth movement for this scenario. (var compensateX, var compensateY) = currentLine.Compensate; (var lastViewportX, var lastViewportY) = currentLine.Viewport; var candidateX = _translate.X + compensateForViewScrollIfNeeded(_state.Viewport.X, xStep, ref compensateX, ref lastViewportX); var candidateY = _translate.Y + compensateForViewScrollIfNeeded(_state.Viewport.Y, yStep, ref compensateY, ref lastViewportY); if (currentWalk.WalkAnywhere || isWalkable(new Position(candidateX, candidateY))) { currentLine.Compensate = (compensateX, compensateY); _translate.Position = (candidateX, candidateY); return; } } currentLine.LastFrame = _animation?.Animation.Sprite; currentLine.Viewport = (_state.Viewport.X, _state.Viewport.Y); currentLine.NumSteps -= Math.Abs(currentLine.IsBaseStepX ? xStep : yStep); if (currentLine.NumSteps >= 0f) { var candidateX = _translate.X + (xStep - currentLine.Compensate.x); var candidateY = _translate.Y + (yStep - currentLine.Compensate.y); if (currentWalk.WalkAnywhere || isWalkable(new Position(candidateX, candidateY))) { _translate.Position = (candidateX, candidateY); } else { onWalkLineCompleted(currentWalk, currentLine, false); return; } } currentLine.Compensate = (0f, 0f); }
private async Task<bool> walkStraightLine(ILocation destination, CancellationTokenSource token, List<IObject> debugRenderers) { if (debugRenderers != null) { GLLineRenderer line = new GLLineRenderer (_glUtils, _obj.X, _obj.Y, destination.X, destination.Y); IObject renderer = _objFactory.GetObject("Debug Line"); renderer.CustomRenderer = line; renderer.ChangeRoom(_obj.Room); debugRenderers.Add (renderer); } if (_cutscene.IsSkipping) { _obj.X = destination.X; _obj.Y = destination.Y; return true; } if (!isDistanceVeryShort(destination)) { var lastDirection = _faceDirection.Direction; var walkAnimation = _outfit.Outfit[AGSOutfit.Walk]; bool alreadyWalking = _faceDirection.CurrentDirectionalAnimation == walkAnimation; _faceDirection.CurrentDirectionalAnimation = walkAnimation; await _faceDirection.FaceDirectionAsync(_obj.X, _obj.Y, destination.X, destination.Y); if (lastDirection != _faceDirection.Direction && alreadyWalking) { await Task.Delay(200); } } float xSteps = Math.Abs (destination.X - _obj.X); float ySteps = Math.Abs (destination.Y - _obj.Y); float numSteps = Math.Max (xSteps, ySteps); bool isBaseStepX = xSteps >= ySteps; float xStep = xSteps / numSteps; if (_obj.X > destination.X) xStep = -xStep; float yStep = ySteps / numSteps; if (_obj.Y > destination.Y) yStep = -yStep; WalkLineInstruction instruction = new WalkLineInstruction(token, numSteps, xStep, yStep, isBaseStepX, destination); _currentWalkLine = instruction; Task timeout = Task.Delay(WalkLineTimeoutInMilliseconds); Task completedTask = await Task.WhenAny(instruction.OnCompletion.Task, timeout); if (completedTask == timeout) { instruction.CancelToken.Cancel(); return false; } if (instruction.CancelToken.IsCancellationRequested) return false; _obj.X = destination.X; _obj.Y = destination.Y; return true; }
private void onRepeatedlyExecute(object sender, AGSEventArgs args) { WalkLineInstruction currentLine = _currentWalkLine; if (currentLine == null) return; if (currentLine.CancelToken.IsCancellationRequested || currentLine.NumSteps <= 1f) { _currentWalkLine = null; //Possible race condition here? If so, need to replace with concurrent queue _lastFrame = null; _compensateScrollX = _compensateScrollY = 0f; currentLine.OnCompletion.TrySetResult(null); return; } if (_cutscene.IsSkipping) { _obj.X = currentLine.Destination.X; _obj.Y = currentLine.Destination.Y; _currentWalkLine = null; //Possible race condition here? If so, need to replace with concurrent queue _lastFrame = null; _compensateScrollX = _compensateScrollY = 0f; currentLine.OnCompletion.TrySetResult(null); return; } PointF walkSpeed = adjustWalkSpeed(WalkStep); float xStep = currentLine.XStep * walkSpeed.X; float yStep = currentLine.YStep * walkSpeed.Y; if (MovementLinkedToAnimation && _animation != null && _animation.Animation.Frames.Count > 1 && _animation.Animation.Sprite == _lastFrame) { //If the movement is linked to the animation and the animation speed is slower the the viewport movement, it can lead to flickering //so we do a smooth movement for this scenario. _obj.X += compensateForViewScrollIfNeeded(_obj.Room.Viewport.X, xStep, ref _compensateScrollX, ref _lastViewportX); _obj.Y += compensateForViewScrollIfNeeded(_obj.Room.Viewport.Y, yStep, ref _compensateScrollY, ref _lastViewportY); return; } if (_animation != null) _lastFrame = _animation.Animation.Sprite; _lastViewportX = _obj.Room.Viewport.X; currentLine.NumSteps -= Math.Abs(currentLine.IsBaseStepX ? xStep : yStep); if (currentLine.NumSteps >= 0f) { _obj.X += (xStep - _compensateScrollX); _obj.Y += (yStep - _compensateScrollY); } _compensateScrollX = _compensateScrollY = 0f; }