|
|
@ -1,10 +1,9 @@
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* Copyright 2012-2018 the original author or authors.
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
*
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* (the "License"); you may not use this file except in compliance with
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -29,14 +28,12 @@ import org.springframework.web.socket.handler.TextWebSocketHandler;
|
|
|
|
|
|
|
|
|
|
|
|
public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
|
|
|
public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
|
|
|
|
|
|
|
|
|
|
|
public static final int PLAYFIELD_WIDTH = 640;
|
|
|
|
|
|
|
|
public static final int PLAYFIELD_HEIGHT = 480;
|
|
|
|
|
|
|
|
public static final int GRID_SIZE = 10;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final AtomicInteger snakeIds = new AtomicInteger(0);
|
|
|
|
private static final AtomicInteger snakeIds = new AtomicInteger(0);
|
|
|
|
|
|
|
|
|
|
|
|
private static final Random random = new Random();
|
|
|
|
private static final Random random = new Random();
|
|
|
|
|
|
|
|
|
|
|
|
private final int id;
|
|
|
|
private final int id;
|
|
|
|
|
|
|
|
|
|
|
|
private Snake snake;
|
|
|
|
private Snake snake;
|
|
|
|
|
|
|
|
|
|
|
|
public static String getRandomHexColor() {
|
|
|
|
public static String getRandomHexColor() {
|
|
|
@ -50,15 +47,15 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Location getRandomLocation() {
|
|
|
|
public static Location getRandomLocation() {
|
|
|
|
int x = roundByGridSize(random.nextInt(PLAYFIELD_WIDTH));
|
|
|
|
int x = roundByGridSize(random.nextInt(SnakeUtils.PLAYFIELD_WIDTH));
|
|
|
|
int y = roundByGridSize(random.nextInt(PLAYFIELD_HEIGHT));
|
|
|
|
int y = roundByGridSize(random.nextInt(SnakeUtils.PLAYFIELD_HEIGHT));
|
|
|
|
return new Location(x, y);
|
|
|
|
return new Location(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static int roundByGridSize(int value) {
|
|
|
|
private static int roundByGridSize(int value) {
|
|
|
|
value = value + (GRID_SIZE / 2);
|
|
|
|
value = value + (SnakeUtils.GRID_SIZE / 2);
|
|
|
|
value = value / GRID_SIZE;
|
|
|
|
value = value / SnakeUtils.GRID_SIZE;
|
|
|
|
value = value * GRID_SIZE;
|
|
|
|
value = value * SnakeUtils.GRID_SIZE;
|
|
|
|
return value;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -109,4 +106,5 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
|
|
|
SnakeTimer.broadcast(
|
|
|
|
SnakeTimer.broadcast(
|
|
|
|
String.format("{'type': 'leave', 'id': %d}", Integer.valueOf(this.id)));
|
|
|
|
String.format("{'type': 'leave', 'id': %d}", Integer.valueOf(this.id)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|