<commit_before>angular.module('chaos_engine')
	.factory(
		'SpriteSheet',
		[
			function () {
				var _spriteSheet = function (options) {
					this.img_src = options.img_src;
					this.x_offset = options.x_offset || 0;
					this.y_offset = options.y_offset || 0;
					this.tile_width = options.tile_width;
					this.tile_height = options.tile_height;
					this.image = null;

					return this;
				}
				_spriteSheet.prototype.getTile = function (x, y) {
					if (!this.canvas) {
						this.canvas = document.createElement('canvas');
						this.canvas.width = this.tile_width;
						this.canvas.height = this.tile_height;
						this.ctx = this.canvas.getContext('2d');
					}
					this.ctx.drawImage(
						this.image,
						(x * this.tile_width) + this.x_offset,
						(y * this.tile_height) + this.y_offset,
						this.tile_width,
						this.tile_height,
						0,
						0,
						this.tile_width,
						this.tile_height
					);
					var tImage = new Image();
					tImage.src = this.canvas.toDataURL();
					return tImage;

				}
				_spriteSheet.prototype.load = function (callback) {
					//Append canvas
					this.image = new Image();
					this.image.onload = function () {
						return callback(null, this.image);
					}
					this.image.src = this.img_src;
				}

				return _spriteSheet
			}
		]
	).service(
		'Socket',
		[
			'$rootScope',
			'$cookies',
			'GameScreen',
			'WorldCache',
			'InventoryModal',
			function ($rootScope, $cookies, GameScreen, WorldCache, InventoryModal) {
				return {
					init: function (config) {

						var socket = window.io();
						socket.on('hello', function (data) {
							//console.log($cookies);
							socket.emit('greet', { user: $cookies.user /*$cookieStore.get('user')*/ });
						});
						socket.on('create-user', function (data) {

							//Create cookie
							//console.log(data.user);
							//$cookieStore.remove('user');
							//$cookieStore.put('user', data.user);
							$cookies.user = data.user;
							$cookies.view_port_focus = data.view_port_focus;
							$rootScope.$digest();

						});
						socket.on('refresh-world', function (data) {
							GameScreen.set_viewport_focus($cookies.view_port_focus);

							WorldCache.init(data);
							GameScreen.startRendering();

						});
						socket.on('update-world', function (data) {


							WorldCache.update(data);


						});
						/** Setup for user generated events */

						$rootScope.$on('keypress', function (e, a, key) {
							if (GameScreen.modal) {
								//Pass control to the modal
								return GameScreen.modal.apply(key);

							}
							switch (key) {
								case('s'):
									//Trigger Down
									socket.emit('user-input', {
										action: 'player.move.down'
									})
									break;
								case('d'):
									//Trigger Down
									socket.emit('user-input', {
										action: 'player.move.right'
									})
									break;
								case('a'):
									//Trigger Down
									socket.emit('user-input', {
										action: 'player.move.left'
									})
									break;
								case('w'):
									//Trigger Down

									socket.emit('user-input', {
										action: 'player.move.up'
									})
									break;
								case('e'):
									//Figure out what the user is trying to interact with
									socket.emit('user-input', {
										action: 'player.interact'
									})
									break;
								case('i'):
									//Display the inventory Modal
									GameScreen.showModal(new InventoryModal({
										ingest: function (inventory_object) {
											socket.emit('user-input', {
												action: 'player.ingest',
												object: inventory_object
											})
										}
									}));

									break;
							}

							/*$scope.$apply(function () {
							 $scope.key = key;
							 });*/
						})
						return socket;
					}
				}
			}
		]
	)
/**
 * Caches world data for quicker socket comunitication
 */
	.service(
		'WorldCache',
		[
			'ObjectCache',
			'TileCache',

			function (ObjectCache, TileCache) {
				var _WorldCache = {
					world: null,
					_listeners:{},
					on:function(event, callback){
						if(!_WorldCache._listeners[event]){
							_WorldCache._listeners[event] = [];
						}
						_WorldCache._listeners[event].push(callback);
					},
					trigger:function(event, data){
						for(var i in _WorldCache._listeners[event]){
							_WorldCache._listeners[event][i](data);
						}
					},
					init: function (data) {
						//First init the objects
						_WorldCache.world = {
							objects:{},
							tiles:{}
						}
						for(var object_id in data.objects){
							var instance = ObjectCache.createNewObjectInstance(data.objects[object_id]);
							if(!instance){
								console.log("No object class for:" + object_id);
							}else{
								_WorldCache.world.objects[object_id] = instance;
							}
						}
						//Quick clone for now:
						for(var x in  data.tiles){
							if(!_WorldCache.world.tiles[x]){
								_WorldCache.world.tiles[x] = {}
							}
							for(var y in  data.tiles[x]){
								_WorldCache.world.tiles[x][y] = TileCache.initTileInstance(data.tiles[x][y]);
							}
						}

					},
					update: function (data) {
						for (var object_id in data.objects) {
							if(_WorldCache.world.objects[object_id]){
								_WorldCache.world.objects[object_id].update(data.objects[object_id]);
							}else{

								var instance = ObjectCache.createNewObjectInstance(data.objects[object_id]);
								if(!instance){
									//console.log("No object class for:" + object_id);
								}else{
									console.log("Adding Object:" + object_id);
									_WorldCache.world.objects[object_id] = instance;
								}
							}
						}
						_WorldCache.trigger('update', _WorldCache.world);
					}
				}
				return _WorldCache;
			}
		]
	)
	.service(
		'GameScreen',
		[
			'$timeout',
			'ObjectCache',
			'WorldCache',
			'TileCache',

			function ($timeout, ObjectCache, WorldCache, TileCache) {
				var _GameScreen = function (options) {
					this.view_radius = 40;
					this.view_port = {
						x: 0,
						y: 0,
						z: 0
					}
					this.gameArea = document.getElementById('gameArea');
					if (!this.gameArea) {
						throw new Error("Game Area Element not found");
					}
					this.gameCanvas = document.getElementById('gameCanvas');

					this.widthToHeight = 2 / 1;
					this.tile_width = 75;
					this.tile_height = 38;
					this.selected_tile = {
						x:-1,
						y:-1
					};

					this.gameContext = this.gameCanvas.getContext("2d");
					this.gameContext.font = "12px Arial";
					var _this = this;
					window.addEventListener('resize', function () {
						_this.resize()
					}, false);
					window.addEventListener('orientationchange', function () {
						_this.resize()
					}, false);
					var _this = this;
					window.addEventListener('mousedown', function(e) {
						var mouseStartPos = _this.getMousePos(e);
						//Find Tile

						//If tile has object on it move to then interact

						//If tile does not have object just interact

					});
					window.addEventListener('mousemove', function(e) {
						var mouseStartPos = _this.getMousePos(e);

						var pos = _this.getScreenPosFromWorldXY(0,0)
						var xDiff =  mouseStartPos.x - pos.x;
						var yDiff = mouseStartPos.y - pos.y;
						var x = Math.floor((xDiff/ _this.tile_width) -.5);

						var y = Math.floor((yDiff/ _this.tile_height) -.5);


						var y_offset = x ;
						var x_offset = y;
						var world_y = (y - y_offset );
						var world_x = x + (x_offset);
						//Center Should line up with view port
						//The difference should determine the block

						_this.selected_tile = {
							x:world_x,
							y:world_y
						}
						console.log(_this.selected_tile, x_offset , y_offset);
//console.log(mouseStartPos.x, mouseStartPos.y);

					});
				}
				_GameScreen.prototype.getMousePos = function(evt) {
					var rect = this.gameCanvas.getBoundingClientRect();
					return {
						x: evt.clientX - rect.left,
						y: evt.clientY - rect.top
					};
				}
				_GameScreen.prototype.startRendering = function(){
					if(this.timeout){
						return; //We are already rendering
					}
					this.setTimeout();
				}
				_GameScreen.prototype.setTimeout = function(){
					var _this = this;
					this.timeout = $timeout(function(){
						_this.render_world();<commit_msg>Making progress on the rendering stuff<commit_after>angular.module('chaos_engine')
	.factory(
		'SpriteSheet',
		[
			function () {
				var _spriteSheet = function (options) {
					this.img_src = options.img_src;
					this.x_offset = options.x_offset || 0;
					this.y_offset = options.y_offset || 0;
					this.tile_width = options.tile_width;
					this.tile_height = options.tile_height;
					this.image = null;

					return this;
				}
				_spriteSheet.prototype.getTile = function (x, y) {
					if (!this.canvas) {
						this.canvas = document.createElement('canvas');
						this.canvas.width = this.tile_width;
						this.canvas.height = this.tile_height;
						this.ctx = this.canvas.getContext('2d');
					}
					this.ctx.drawImage(
						this.image,
						(x * this.tile_width) + this.x_offset,
						(y * this.tile_height) + this.y_offset,
						this.tile_width,
						this.tile_height,
						0,
						0,
						this.tile_width,
						this.tile_height
					);
					var tImage = new Image();
					tImage.src = this.canvas.toDataURL();
					return tImage;

				}
				_spriteSheet.prototype.load = function (callback) {
					//Append canvas
					this.image = new Image();
					this.image.onload = function () {
						return callback(null, this.image);
					}
					this.image.src = this.img_src;
				}

				return _spriteSheet
			}
		]
	).service(
		'Socket',
		[
			'$rootScope',
			'$cookies',
			'GameScreen',
			'WorldCache',
			'InventoryModal',
			function ($rootScope, $cookies, GameScreen, WorldCache, InventoryModal) {
				return {
					init: function (config) {

						var socket = window.io();
						socket.on('hello', function (data) {
							//console.log($cookies);
							socket.emit('greet', { user: $cookies.user /*$cookieStore.get('user')*/ });
						});
						socket.on('create-user', function (data) {

							//Create cookie
							//console.log(data.user);
							//$cookieStore.remove('user');
							//$cookieStore.put('user', data.user);
							$cookies.user = data.user;
							$cookies.view_port_focus = data.view_port_focus;
							$rootScope.$digest();

						});
						socket.on('refresh-world', function (data) {
							GameScreen.set_viewport_focus($cookies.view_port_focus);

							WorldCache.init(data);
							GameScreen.startRendering();

						});
						socket.on('update-world', function (data) {


							WorldCache.update(data);


						});
						/** Setup for user generated events */

						$rootScope.$on('keypress', function (e, a, key) {
							if (GameScreen.modal) {
								//Pass control to the modal
								return GameScreen.modal.apply(key);

							}
							switch (key) {
								case('s'):
									//Trigger Down
									socket.emit('user-input', {
										action: 'player.move.down'
									})
									break;
								case('d'):
									//Trigger Down
									socket.emit('user-input', {
										action: 'player.move.right'
									})
									break;
								case('a'):
									//Trigger Down
									socket.emit('user-input', {
										action: 'player.move.left'
									})
									break;
								case('w'):
									//Trigger Down

									socket.emit('user-input', {
										action: 'player.move.up'
									})
									break;
								case('e'):
									//Figure out what the user is trying to interact with
									socket.emit('user-input', {
										action: 'player.interact'
									})
									break;
								case('i'):
									//Display the inventory Modal
									GameScreen.showModal(new InventoryModal({
										ingest: function (inventory_object) {
											socket.emit('user-input', {
												action: 'player.ingest',
												object: inventory_object
											})
										}
									}));

									break;
							}

							/*$scope.$apply(function () {
							 $scope.key = key;
							 });*/
						})
						return socket;
					}
				}
			}
		]
	)
/**
 * Caches world data for quicker socket comunitication
 */
	.service(
		'WorldCache',
		[
			'ObjectCache',
			'TileCache',

			function (ObjectCache, TileCache) {
				var _WorldCache = {
					world: null,
					_listeners:{},
					on:function(event, callback){
						if(!_WorldCache._listeners[event]){
							_WorldCache._listeners[event] = [];
						}
						_WorldCache._listeners[event].push(callback);
					},
					trigger:function(event, data){
						for(var i in _WorldCache._listeners[event]){
							_WorldCache._listeners[event][i](data);
						}
					},
					init: function (data) {
						//First init the objects
						_WorldCache.world = {
							objects:{},
							tiles:{}
						}
						for(var object_id in data.objects){
							var instance = ObjectCache.createNewObjectInstance(data.objects[object_id]);
							if(!instance){
								console.log("No object class for:" + object_id);
							}else{
								_WorldCache.world.objects[object_id] = instance;
							}
						}
						//Quick clone for now:
						for(var x in  data.tiles){
							if(!_WorldCache.world.tiles[x]){
								_WorldCache.world.tiles[x] = {}
							}
							for(var y in  data.tiles[x]){
								_WorldCache.world.tiles[x][y] = TileCache.initTileInstance(data.tiles[x][y]);
							}
						}

					},
					update: function (data) {
						for (var object_id in data.objects) {
							if(_WorldCache.world.objects[object_id]){
								_WorldCache.world.objects[object_id].update(data.objects[object_id]);
							}else{

								var instance = ObjectCache.createNewObjectInstance(data.objects[object_id]);
								if(!instance){
									//console.log("No object class for:" + object_id);
								}else{
									console.log("Adding Object:" + object_id);
									_WorldCache.world.objects[object_id] = instance;
								}
							}
						}
						_WorldCache.trigger('update', _WorldCache.world);
					}
				}
				return _WorldCache;
			}
		]
	)
	.service(
		'GameScreen',
		[
			'$timeout',
			'ObjectCache',
			'WorldCache',
			'TileCache',

			function ($timeout, ObjectCache, WorldCache, TileCache) {
				var _GameScreen = function (options) {
					this.view_radius = 40;
					this.view_port = {
						x: 0,
						y: 0,
						z: 0
					}
					this.gameArea = document.getElementById('gameArea');
					if (!this.gameArea) {
						throw new Error("Game Area Element not found");
					}
					this.gameCanvas = document.getElementById('gameCanvas');

					this.widthToHeight = 2 / 1;
					this.tile_width = 75;
					this.tile_height = 38;
					this.selected_tile = {
						x:-1,
						y:-1
					};

					this.gameContext = this.gameCanvas.getContext("2d");
					this.gameContext.font = "12px Arial";
					var _this = this;
					window.addEventListener('resize', function () {
						_this.resize()
					}, false);
					window.addEventListener('orientationchange', function () {
						_this.resize()
					}, false);
					var _this = this;
					window.addEventListener('mousedown', function(e) {
						var mouseStartPos = _this.getMousePos(e);
						//Find Tile

						//If tile has object on it move to then interact

						//If tile does not have object just interact

					});
					window.addEventListener('mousemove', function(e) {
						var mouseStartPos = _this.getMousePos(e);

						var pos = _this.getScreenPosFromWorldXY(0,0)
						var xDiff =  mouseStartPos.x - pos.x;
						var yDiff = mouseStartPos.y - pos.y;
						var x = Math.round((xDiff/ _this.tile_width) -.5);

						var y = Math.round((yDiff/ _this.tile_height) -.5);


						var y_offset = Math.floor((xDiff/ _this.tile_width) -.5) ;
						var x_offset = Math.floor((yDiff/ _this.tile_height) -.5);
						var world_y = (y - y_offset );
						var world_x = x + (x_offset);
						//Center Should line up with view port
						//The difference should determine the block

						_this.selected_tile = {
							x:world_x,
							y:world_y
						}
						console.log(_this.selected_tile, xDiff , yDiff);
//console.log(mouseStartPos.x, mouseStartPos.y);

					});
				}
				_GameScreen.prototype.getMousePos = function(evt) {
					var rect = this.gameCanvas.getBoundingClientRect();
					return {
						x: evt.clientX - rect.left,
						y: evt.clientY - rect.top
					};
				}
				_GameScreen.prototype.startRendering = function(){
					if(this.timeout){
						return; //We are already rendering
					}
					this.setTimeout();
				}
				_GameScreen.prototype.setTimeout = function(){
					var _this = this;
					this.timeout = $timeout(function(){
						_this.render_world();
{'fraction_non_alphanumeric': 0.13713257348530294, 'fraction_numerical': 0.003119376124775045, 'mean_word_length': 2.099851245816289, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4081326', 'n_tokens_mistral': 8058, 'n_tokens_neox': 5228, 'n_words': 1004}