Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions robots/src/Serialize/ObjectWithState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package Serialize;

public interface ObjectWithState {
void save();
void restore();
}
101 changes: 101 additions & 0 deletions robots/src/Serialize/WindowWithSerialize.java
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;
}
}
120 changes: 120 additions & 0 deletions robots/src/gui/GameDataModel.java
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";
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

сгруппируйте переменные, у Вас идут позиции и между ними KEY_COORDINATE_...

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() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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();
}

}
Loading