class Vector2 {
constructor(vec, y) {
if(typeof vec === "undefined" &&
typeof y === "undefined") {
this.x = 0;
this.y = 0;
} else if(typeof vec === "number" &&
typeof y === "undefined") {
this.x = vec;
this.y = vec;
} else if(typeof vec === "number" &&
typeof y === "number") {
this.x = vec;
this.y = y;
} else if(typeof vec === "object" && vec.constructor == Vector2) {
this.x = vec.getX()
this.y = vec.getY();
} else
throw 'Vector2::constructor(): Invalid types ' + (typeof vec) + ' ' + (typeof y);
}
getX() {
return this.x;
}
getY() {
return this.y;
}
setX(x) {
if(typeof x !== "number")
throw 'Vector2::setX(): Invalid type ' + (typeof x);
this.x = x;
}
setY(y) {
if(typeof y !== "number")
throw 'Vector2::setY(): Invalid type ' + (typeof y);
this.y = y;
}
add(vec, y) {
if(typeof vec === "number" &&
typeof y === "undefined") {
return new Vector2(
this.x + vec,
this.y + vec
);
} else if(typeof vec === "number" &&
typeof y === "number") {
return new Vector2(
this.x + vec,
this.y + y
);
} else if(typeof vec === "object" && vec.constructor == Vector2) {
return new Vector2(
this.x + vec.getX(),
this.y + vec.getY()
);
} else
throw 'Vector2::add(): Invalid types ' + (typeof vec) + ' ' + (typeof y);
}
sub(vec, y) {
if(typeof vec === "number" &&
typeof y === "undefined") {
return new Vector2(
this.x - vec,
this.y - vec
);
} else if(typeof vec === "number" &&
typeof y === "number") {
return new Vector2(
this.x - vec,
this.y - y
);
} else if(typeof vec === "object" && vec.constructor == Vector2) {
return new Vector2(
this.x - vec.getX(),
this.y - vec.getY()
);
} else
throw 'Vector2::sub(): Invalid types ' + (typeof vec) + ' ' + (typeof y);
}
mul(vec, y) {
if(typeof vec === "number" &&
typeof y === "undefined") {
return new Vector2(
this.x * vec,
this.y * vec
);
} else if(typeof vec === "number" &&
typeof y === "number") {
return new Vector2(
this.x * vec,
this.y * y
);
} else if(typeof vec === "object" && vec.constructor == Vector2) {
return new Vector2(
this.x * vec.getX(),
this.y * vec.getY()
);
} else
throw 'Vector2::mul(): Invalid types ' + (typeof vec) + ' ' + (typeof y);
}
div(vec, y) {
if(typeof vec === "number" &&
typeof y === "undefined") {
return new Vector2(
this.x / vec,
this.y / vec
);
} else if(typeof vec === "number" &&
typeof y === "number") {
return new Vector2(
this.x / vec,
this.y / y
);
} else if(typeof vec === "object" && vec.constructor == Vector2) {
return new Vector2(
this.x / vec.getX(),
this.y / vec.getY()
);
} else
throw 'Vector2::div(): Invalid types ' + (typeof vec) + ' ' + (typeof y);
}
length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
};
$(document).ready(function($) {
var entities = [];
var entityId = 0;
class Entity {
constructor() {
var entity = this;
this.id = entityId++;
this.position = new Vector2();
this.speed = new Vector2();
this.elem = $('
').addClass('point').appendTo('body');
this.row = $('
').appendTo('#table_entities');
this.columns = {
id: $('| ').text(this.id).appendTo(this.row),
position: $(' | ').appendTo(this.row),
speed: $(' | ').appendTo(this.row),
color: $(' | ').appendTo(this.row),
actions: $(' | ').appendTo(this.row),
}
$('')
.attr('type', 'color')
.change(function(){
entity.elem.css('background-color', $(this).val());
})
.appendTo(this.columns.color)
.change();
$(' |