forked from alklepin/Robots-old
-
Notifications
You must be signed in to change notification settings - Fork 0
Коробицын Александр, FixRobot #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlexKorobitsyn
wants to merge
4
commits into
master
Choose a base branch
from
fixAllComponents
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fe344ee
Добавил всё в ветку без мусора, Робот находит путь , делая максимум 2…
AlexKorobitsyn 6ba5446
makeExit and save different methods
AlexKorobitsyn 86f242f
small changes for exit method
AlexKorobitsyn a86e89a
Save and restore methods
AlexKorobitsyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package Serialize; | ||
|
|
||
| public interface ObjectWithState { | ||
| void save(); | ||
| void restore(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package Serialize; | ||
|
|
||
| import java.awt.Dimension; | ||
| import java.awt.Point; | ||
| import java.io.BufferedInputStream; | ||
| import java.io.BufferedOutputStream; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.ObjectInputStream; | ||
| import java.io.ObjectOutputStream; | ||
| import java.io.OutputStream; | ||
| import javax.swing.JInternalFrame; | ||
|
|
||
| public class WindowWithSerialize extends JInternalFrame implements ObjectWithState { | ||
|
|
||
| private String path; | ||
| private int x; | ||
| private int y; | ||
| private boolean isIcon = false; | ||
| private Dimension dimension; | ||
|
|
||
| public WindowWithSerialize(String title, boolean b, boolean b1, boolean b2, boolean b3, | ||
| String path) { | ||
| super(title, b, b1, b2, b3); | ||
| setPath(path); | ||
| } | ||
|
|
||
| public void setLocation() { | ||
| setLocation(this.x, this.y); | ||
| } | ||
|
|
||
| public boolean getStateIcon() { | ||
| return this.isIcon; | ||
| } | ||
|
|
||
| public void setSize() { | ||
| setSize(getDimension()); | ||
| } | ||
|
|
||
| public Dimension getDimension() { | ||
| return this.dimension; | ||
| } | ||
|
|
||
| public static boolean isFileExists(File file) { | ||
| return file.isFile(); | ||
| } | ||
|
|
||
| @Override | ||
| public void save() { | ||
| try (OutputStream os = new FileOutputStream(getPath())) { | ||
| try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(os))) { | ||
| Point point = this.getLocation(); | ||
| oos.writeObject(point.x); | ||
| oos.writeObject(point.y); | ||
| oos.writeObject(this.isIcon()); | ||
| oos.writeObject(this.getSize()); | ||
| oos.flush(); | ||
| } | ||
| } catch (IOException ex) { | ||
| ex.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void restore() { | ||
| String path = getPath(); | ||
| File file = new File(path); | ||
|
|
||
| if (!isFileExists(file)) { | ||
| this.x = 50; | ||
| this.y = 50; | ||
| this.isIcon = false; | ||
| this.dimension = new Dimension(500, 300); | ||
| } else { | ||
| System.out.println("FileExist"); | ||
| try (InputStream is = new FileInputStream(path)) { | ||
| try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(is))) { | ||
| this.x = (int) ois.readObject(); | ||
| this.y = (int) ois.readObject(); | ||
| this.isIcon = (boolean) ois.readObject(); | ||
| this.dimension = (Dimension) ois.readObject(); | ||
| } catch (ClassNotFoundException ex) { | ||
| ex.printStackTrace(); | ||
| } | ||
| } catch (IOException ex) { | ||
| ex.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public String getPath() { | ||
| return path; | ||
| } | ||
|
|
||
| public void setPath(String path) { | ||
| this.path = path; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package gui; | ||
|
|
||
| import java.awt.*; | ||
| import java.util.Observable; | ||
|
|
||
| public class GameDataModel extends Observable { | ||
| double angularVelocity = 0; | ||
| static int counter = 0; | ||
| Position targetPosition = new Position(250, 100); | ||
| static Position robotPosition = new Position(100, 100); | ||
| Target target = new Target(targetPosition); | ||
| static Robot robot = new Robot(robotPosition, 0, 0.1, 0.001, 0); | ||
| public static String KEY_COORDINATE_ROBOT = "robot changed"; | ||
| public static String KEY_COORDINATE_TARGET = "target changed"; | ||
|
|
||
| enum Turn{ | ||
| RIGHT, | ||
| LEFT; | ||
|
|
||
| public static boolean checkForLeftTurn(double angleToTarget){ | ||
| return angleToTarget < robot.m_robotDirection && robot.angularVelocity != -robot.maxAngularVelocity; | ||
| } | ||
| public static boolean checkForRightTurn(double angleToTarget) { | ||
| return angleToTarget > robot.m_robotDirection && robot.angularVelocity != robot.maxAngularVelocity; | ||
| } | ||
|
|
||
| public void actionForRight() { | ||
| counter++; | ||
| robot.angularVelocity = robot.maxAngularVelocity; | ||
| } | ||
| public void actionForLeft(){ | ||
| counter++; | ||
| robot.angularVelocity = -robot.maxAngularVelocity; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| protected void setTargetPosition(Point p) { | ||
| target.position.x = p.x; | ||
| target.position.y = p.y; | ||
| setChanged(); | ||
| notifyObservers(KEY_COORDINATE_TARGET); | ||
| clearChanged(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| protected void onModelUpdateEvent() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. сложно читать такой код, разбейте его на понятные функциональные блоки (методы), по которым можно было бы легко и понятно понимать логику. |
||
|
|
||
| double distance = Geometry.distance(target.position, robot.position); | ||
| if (distance < 0.5) { | ||
| counter = 0; | ||
| return; | ||
| } | ||
| double velocity = robot.maxVelocity; | ||
| double angleToTarget = Geometry.angleTo(robot.position, target.position); | ||
|
|
||
| takeAngleForTurn(distance, angleToTarget); | ||
|
|
||
| moveRobot(velocity, robot.angularVelocity, 10); | ||
| } | ||
|
|
||
| private static void takeAngleForTurn(double distance, double angleToTarget) { | ||
| if (distance >=robot.angularVelocity*robot.maxVelocity&& angleToTarget !=robot.m_robotDirection) { | ||
| if (counter<2) { | ||
| if (Turn.checkForRightTurn(angleToTarget)) { | ||
| Turn turn = Turn.RIGHT; | ||
| turn.actionForRight(); | ||
| } | ||
| if (Turn.checkForLeftTurn(angleToTarget)) { | ||
| Turn turn = Turn.LEFT; | ||
| turn.actionForLeft(); | ||
| } | ||
| }else { | ||
| if (Math.abs(angleToTarget - robot.m_robotDirection)<0.1) { | ||
| robot.angularVelocity = 0; | ||
| if (angleToTarget > robot.m_robotDirection) | ||
| robot.angularVelocity = robot.maxAngularVelocity; | ||
| if (angleToTarget < robot.m_robotDirection) | ||
| robot.angularVelocity = -robot.maxAngularVelocity; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //enum как состояние поворота, Метод для onUpdate, | ||
| private static double applyLimits(double value, double min, double max) { | ||
| if (value < min) | ||
| return min; | ||
| if (value > max) | ||
| return max; | ||
| return value; | ||
| } | ||
|
|
||
| private void moveRobot(double velocity, double angularVelocity, double duration) { | ||
| velocity = applyLimits(velocity, 0, robot.maxVelocity); | ||
| angularVelocity = applyLimits(angularVelocity, -robot.maxAngularVelocity, robot.maxAngularVelocity); | ||
| double newX = robot.position.x + velocity / angularVelocity * | ||
| (Math.sin(robot.m_robotDirection + angularVelocity * duration) - | ||
| Math.sin(robot.m_robotDirection)); | ||
| if (!Double.isFinite(newX)) { | ||
| newX = robot.position.x + velocity * duration * Math.cos(robot.m_robotDirection); | ||
| } | ||
| double newY = robot.position.y - velocity / angularVelocity * | ||
| (Math.cos(robot.m_robotDirection + angularVelocity * duration) - | ||
| Math.cos(robot.m_robotDirection)); | ||
| if (!Double.isFinite(newY)) { | ||
| newY = robot.position.y + velocity * duration * Math.sin(robot.m_robotDirection); | ||
| } | ||
| robot.position.x = newX; | ||
| robot.position.y = newY; | ||
| double newDirection = Geometry.asNormalizedRadians(robot.m_robotDirection + angularVelocity * duration ); | ||
| robot.m_robotDirection = newDirection; | ||
| setChanged(); | ||
| notifyObservers(KEY_COORDINATE_ROBOT); | ||
| clearChanged(); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
сгруппируйте переменные, у Вас идут позиции и между ними KEY_COORDINATE_...