Skip to content

Commit

Permalink
Merge branch 'master' into ai
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomen committed Aug 1, 2015
2 parents c7e27da + a3a5b32 commit 958be98
Show file tree
Hide file tree
Showing 58 changed files with 1,103 additions and 339 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -5,4 +5,4 @@ lib/server/logs
build/
.pub
.DS_Store
.idea
.idea
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -5,7 +5,7 @@ Asteroid Racers is a multiplayer browser game that lets you race against other p

Set up the development environment
----------------------------------
- Download DartEditor from www.dartlang.org.
- Download the Dart SDK: https://www.dartlang.org/downloads/
- Clone the Asteroid-Racers repo
- pub get

Expand Down Expand Up @@ -40,3 +40,6 @@ There are two ways to test the client locally.
For details visit the wiki: https://github.com/BitAlchemists/Asteroid-Racers/wiki


Install protobuf
https://www.dartlang.org/articles/serialization/#protobuf-review

31 changes: 31 additions & 0 deletions doc/TESTBOOK
@@ -0,0 +1,31 @@
Run server and two clients. Validate results on both clients

1) Startup Server
2) Startup Client 1
3) Startup Client 2

4) On Connection
- connect without a username - you should be given one
- disconnect
- connect with a username

- see asteroids
- see space ship with name
- see launch pad, check point and finish
- see space station
- see background with stars, earth, moon, sun
- see spinning station

5) Movement

- accelerate
-- particle effect should be visible
- rotate
- move around
- crash into asteroid
-- collision effect should be visible
- crash into a player
- race


6) Chat
2 changes: 2 additions & 0 deletions lib/_needs_refactoring/file-logger.dart
@@ -1,4 +1,5 @@
/*
library filelogger;
import 'dart:isolate';
import 'dart:io';
Expand Down Expand Up @@ -34,4 +35,5 @@ void initLogging(String logFileName) {
//_loggingPort = Isolate.spawn(startLogging);
//_loggingPort.send(logFileName.toNativePath());
}
*/
2 changes: 2 additions & 0 deletions lib/_needs_refactoring/message_dispatcher.dart
@@ -1,5 +1,6 @@
/*
typedef MessageHandler(Message message);
class MessageDispatcher {
Expand All @@ -26,4 +27,5 @@ class MessageDispatcher {
}
}
}
*/
6 changes: 3 additions & 3 deletions lib/game_client/controllers/player_controller.dart
Expand Up @@ -187,10 +187,10 @@ class PlayerController extends EntityController implements stagexl.Animatable {
updateFromServer(Entity entity){
// we exclude orientation and rotationSpeed from the update, so that we
// can maintain smooth rotations on the client
if(this.isLocalPlayer && this.entity.canMove){
if(this.isLocalPlayer && _movable.canMove){
Movable movable = entity as Movable;
entity.orientation = null;
entity.rotationSpeed = null;
movable.orientation = null;
movable.rotationSpeed = null;
}
super.updateFromServer(entity);
}
Expand Down
42 changes: 27 additions & 15 deletions lib/game_client/game_client.dart
Expand Up @@ -3,17 +3,19 @@ library game_client;
//Dart
import 'dart:html' as html;
import 'dart:math' as Math;
import "dart:async";

//Packages
import 'package:vector_math/vector_math.dart';
import 'package:stagexl/stagexl.dart' as stagexl;
import "package:stagexl_particle/stagexl_particle.dart" as stagexl_particle;
import 'utils/client_logger.dart';

//Ours
import 'package:asteroidracers/shared/shared_client.dart';
import 'package:asteroidracers/shared/ui.dart';
import 'package:asteroidracers/services/chat/chat_client.dart';
import "package:asteroidracers/game_server/game_server.dart";
import "package:asteroidracers/shared/net.dart" as net; //todo: remove this and layer all its code out to serverproxy
import "package:asteroidracers/game_client/server_proxy.dart";
import "package:asteroidracers/shared/shared_client.dart";

//Views
part "game_renderer.dart";
Expand All @@ -32,25 +34,19 @@ part 'controllers/player_controller.dart';
part "controllers/checkpoint_controller.dart";
part "controllers/race_portal_controller.dart";

part 'utils/client_logger.dart';

//server connection
part "net/server_connection.dart";
part "net/local_server_connection.dart";
part 'net/web_socket_server_connection.dart';
part "server_proxy.dart";
Math.Random random = new Math.Random();

class GameConfig {
bool localServer = true;
bool debugJson = false;
bool debugLocalServerNetEncoding = false;
bool debugCollisions = false;
bool renderBackground = true;
}

/**
* The Game client sets up the game and handles the interaction between player and server
*/
class GameClient implements stagexl.Animatable {
class GameClient implements stagexl.Animatable, IGameClient {
GameConfig _config;

PhysicsSimulator _simulator;
Expand Down Expand Up @@ -111,7 +107,7 @@ class GameClient implements stagexl.Animatable {
// send chat messages entered by the player to the server proxy
_chat.onSendChatMesage.listen(server.send);
// register the chat controller for chat messages. The server proxy will send them to the chat controller
server.registerMessageHandler(MessageType.CHAT, _chat.onReceiveMessage);
server.registerMessageHandler(net.MessageType.CHAT, _chat.onReceiveMessage);
// send log messages to onReceiveLogMessage()
ClientLogger.instance.stdout.listen(_chat.onReceiveLogMessage);
}
Expand All @@ -133,7 +129,7 @@ class GameClient implements stagexl.Animatable {
String username = _renderer.username;

print("connecting...");
server.connect(_config.localServer, _config.debugJson, username).then(_onConnect).catchError((html.Event e){
server.connect(_config.localServer, _config.debugLocalServerNetEncoding, username).then(_onConnect).catchError((html.Event e){
log("could not connect.");
_onDisconnect();
});
Expand Down Expand Up @@ -200,7 +196,17 @@ class GameClient implements stagexl.Animatable {
{
// notify the server
if(server != null){
server.send(new Message(MessageType.INPUT, new MovementInput(_player.entity.orientation, _player.accelerate)));

//TODO: move this code to ServerProxy
net.MovementInput movementInput = new net.MovementInput();
movementInput.newOrientation = _player.entity.orientation;
movementInput.accelerate = _player.accelerate;

net.Envelope envelope = new net.Envelope();
envelope.messageType = net.MessageType.INPUT;
envelope.payload = movementInput.writeToBuffer();

server.send(envelope);
}
}

Expand Down Expand Up @@ -317,6 +323,12 @@ class GameClient implements stagexl.Animatable {
handleCollision(int entityId)
{
EntityController ec = _entityControllers[entityId];

if(ec == null) {
print("cannot handle collision: entity controller for entity with id $entityId not found");
return;
}

if(ec.entity is Movable){
(ec.entity as Movable).canMove = false;
}
Expand Down
28 changes: 16 additions & 12 deletions lib/game_client/net/local_server_connection.dart
@@ -1,16 +1,16 @@
part of game_client;
part of game_client_net;

typedef void OnReceiveMessageFunction(Message message);
typedef void OnReceiveMessageFunction(Envelope envelope);

class LocalServerConnection implements ServerConnection {

final bool _debug;
bool _isMaster; //is this one the master or the slave?
LocalServerConnection _inverseConnection;
ClientProxy _clientProxy;
final StreamController<Message> _receiveMessageStreamController = new StreamController<Message>.broadcast();
IClientProxy _clientProxy;
final StreamController<Envelope> _receiveMessageStreamController = new StreamController<Envelope>.broadcast();

Stream<Message> get onReceiveMessage => _receiveMessageStreamController.stream;
Stream<Envelope> get onReceiveMessage => _receiveMessageStreamController.stream;
Function onDisconnectDelegate;

LocalServerConnection([bool this._debug = false])
Expand Down Expand Up @@ -46,20 +46,24 @@ class LocalServerConnection implements ServerConnection {
}


void send(var message){
void send(Envelope envelope){

var object = envelope;

if(_debug) {
message = message.toJson();
object = envelope.writeToBuffer();
}
_inverseConnection._receiveMessage(message);

_inverseConnection._receiveMessage(object);
}

void _receiveMessage(var message)
void _receiveMessage(var object)
{
assert(message != null);
assert(object != null);

if(_debug) {
message = new Message.fromJson(message);
object = new Envelope.fromBuffer(object);
}
_receiveMessageStreamController.add(message);
_receiveMessageStreamController.add(object);
}
}
2 changes: 1 addition & 1 deletion lib/game_client/net/server_connection.dart
@@ -1,4 +1,4 @@
part of game_client;
part of game_client_net;

abstract class ServerConnection implements Connection {
Future connect();
Expand Down
16 changes: 9 additions & 7 deletions lib/game_client/net/web_socket_server_connection.dart
@@ -1,4 +1,4 @@
part of game_client;
part of game_client_net;

class WebSocketServerConnection implements ServerConnection {
//Private Fields
Expand All @@ -7,10 +7,10 @@ class WebSocketServerConnection implements ServerConnection {
bool _isConnecting = false;

Completer _onConnectCompleter = new Completer();
final StreamController<Message> _receiveMessageStreamController = new StreamController<Message>.broadcast();
final StreamController<Envelope> _receiveMessageStreamController = new StreamController<Envelope>.broadcast();

//Public properties
Stream<Message> get onReceiveMessage => _receiveMessageStreamController.stream;
Stream<Envelope> get onReceiveMessage => _receiveMessageStreamController.stream;
Function onDisconnectDelegate;

//ctor
Expand All @@ -20,6 +20,7 @@ class WebSocketServerConnection implements ServerConnection {
Future connect(){
//log("Connecting to Web socket");
_webSocket = new html.WebSocket(_url);
_webSocket.binaryType = "arraybuffer";

_webSocket.onOpen.listen(_onConnected);
_webSocket.onClose.listen(_onDisconnected);
Expand Down Expand Up @@ -57,8 +58,8 @@ class WebSocketServerConnection implements ServerConnection {
}

// Message handling
void send(Message message) {
String encodedMessage = message.toJson();
void send(Envelope envelope) {
List<int> encodedMessage = envelope.writeToBuffer();

if (_webSocket != null && _webSocket.readyState == html.WebSocket.OPEN) {
_webSocket.send(encodedMessage);
Expand All @@ -69,8 +70,9 @@ class WebSocketServerConnection implements ServerConnection {

_onReceiveMessage(html.MessageEvent e) {
//print("onReceiveMessage");
Message message = new Message.fromJson(e.data);
_receiveMessageStreamController.add(message);
Uint8List encodedEnvelope = (e.data as ByteBuffer).asUint8List();
Envelope envelope = new Envelope.fromBuffer(encodedEnvelope);
_receiveMessageStreamController.add(envelope);
}
}

0 comments on commit 958be98

Please sign in to comment.