Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit f134b171ffaf752974098dd6cf0b7be5055aad57 travis134 committed Oct 13, 2011
Showing with 346 additions and 0 deletions.
  1. BIN img/.DS_Store
  2. BIN img/test.png
  3. +92 −0 index.html
  4. +43 −0 js/GameObject.class.js
  5. +10 −0 js/Point.class.js
  6. +66 −0 js/Rectangle.class.js
  7. +135 −0 js/Vector2.class.js
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,92 @@
+<!DOCTYPE html>
+<html>
+
+<head>
+
+<title>JSNA Demo</title>
+
+<!--Import local javascript classes (Vector2, Rectangle, Point)-->
+<script type="text/javascript" language="javascript" src="js/Vector2.class.js"></script>
+<script type="text/javascript" language="javascript" src="js/Rectangle.class.js"></script>
+<script type="text/javascript" language="javascript" src="js/Point.class.js"></script>
+<script type="text/javascript" language="javascript" src="js/GameObject.class.js"></script>
+
+</head>
+
+<body onload="Initialize()">
+
+<!--Canvas to draw to-->
+<canvas width="800" height="600" id="gameCanvas" />
+
+<!--Game logic-->
+<script>
+var CANVAS_X = 0;
+var CANVAS_Y = 0;
+var CANVAS_WIDTH = 800;
+var CANVAS_HEIGHT = 600;
+var INTERVAL = 20;
+var gameObjects;
+var viewRectangle;
+var ctx;
+
+function Initialize()
+{
+ var canvas = document.getElementById('gameCanvas');
+ ctx = canvas.getContext('2d');
+
+ gameObjects = new Array();
+ viewRectangle = new Rectangle(CANVAS_X, CANVAS_Y, CANVAS_WIDTH, CANVAS_HEIGHT);
+
+ var g = new GameObject("img/test.png", new Vector2(10,10));
+ g.velocity = new Vector2(1,1);
+ g.CustomUpdate = function() {
+ if(this.rectangle.Right() >= viewRectangle.Right() || this.rectangle.Left() <= viewRectangle.Left())
+ {
+ this.velocity.X *= -1;
+ }
+ if(this.rectangle.Bottom() >= viewRectangle.Bottom() || this.rectangle.Top() <= viewRectangle.Top())
+ {
+ this.velocity.Y *= -1;
+ }
+ this.position.Add(this.velocity);
+ };
+ gameObjects.push(g);
+
+ LoadContent();
+ setInterval(Update, INTERVAL);
+ setInterval(Draw, INTERVAL);
+}
+
+function LoadContent()
+{
+ for(var i = 0; i < gameObjects.length; i++)
+ {
+ gameObjects[i].LoadContent();
+ }
+}
+
+function Update()
+{
+ for(var i = 0; i < gameObjects.length; i++)
+ {
+ gameObjects[i].Update();
+ }
+}
+
+function Draw()
+{
+ ctx.globalCompositeOperation = 'destination-over';
+ ctx.clearRect(viewRectangle.X, viewRectangle.Y, viewRectangle.Right(), viewRectangle.Bottom());
+ ctx.save();
+
+ for(var i = 0; i < gameObjects.length; i++)
+ {
+ gameObjects[i].Draw(ctx);
+ }
+}
+
+</script>
+
+</body>
+
+</html>
@@ -0,0 +1,43 @@
+//GameObject
+
+//Constructors
+function GameObject(texturePath, position) {
+ this.texturePath = texturePath;
+ this.position = position;
+ this.visible = true;
+ this.scale = 1.0;
+ this.originalPosition = position;
+ this.texture = new Image();
+ this.rectangle = Rectangle.Empty;
+}
+
+//Instance Methods
+GameObject.prototype.LoadContent = function()
+{
+ this.texture.src = this.texturePath;
+ this.rectangle.X = this.position.X;
+ this.rectangle.Y = this.position.Y;
+ this.rectangle.Width = this.texture.width;
+ this.rectangle.Height = this.texture.height;
+};
+
+GameObject.prototype.Update = function()
+{
+ this.rectangle.X = this.position.X;
+ this.rectangle.Y = this.position.Y;
+ this.CustomUpdate();
+};
+
+GameObject.prototype.CustomUpdate = function()
+{
+};
+
+GameObject.prototype.Draw = function(c)
+{
+ c.drawImage(this.texture, this.position.X, this.position.Y, this.rectangle.Width, this.rectangle.Height);
+};
+
+GameObject.prototype.Reset = function()
+{
+ this.position = this.originalPosition;
+};
@@ -0,0 +1,10 @@
+//Point
+
+//Constructors
+function Point(x, y) {
+ this.X = x;
+ this.Y = y;
+}
+
+//Class Properties
+Point.Zero = new Point(0,0);
@@ -0,0 +1,66 @@
+//Rectangle
+
+//Constructors
+function Rectangle(x, y, width, height) {
+ this.X = x;
+ this.Y = y;
+ this.Width = width;
+ this.Height = height;
+}
+
+//Class Properties
+Rectangle.Empty = new Rectangle(0,0,0,0);
+
+//Instance Methods
+Rectangle.prototype.Bottom = function() {
+ return this.Y + this.Height;
+};
+
+Rectangle.prototype.Center = function() {
+ return new Point((this.X + this.Width) / 2, (this.Y + this.Height) / 2);
+};
+
+Rectangle.prototype.ContainsPoint = function(point) {
+ return (point.X >= this.Left() && point.X <= this.Right() && point.Y >= this.Top() && point.Y <= this.Bottom());
+};
+
+Rectangle.prototype.ContainsRectangle = function(other) {
+ return (other.Left() >= this.Left() && other.Right() <= this.Right() && other.Top() >= this.Top() && other.Bottom() <= this.Bottom());
+};
+
+Rectangle.prototype.Inflate = function(horizontalAmount, verticalAmount) {
+ this.X -= horizontalAmount;
+ this.Width += hotizontalAmount;
+ this.Y -= verticalAmount;
+ this.Height += verticalAmount;
+ return this;
+};
+
+Rectangle.prototype.Intersects = function(other) {
+ return (this.Left() <= other.Right() && this.Right() >= other.Left() && this.Top() <= other.Bottom() && this.Bottom() >= other.Top());
+};
+
+Rectangle.prototype.IsEmpty = function() {
+ return (this.X == 0 && this.Y == 0 && this.Width == 0 && this.Height == 0);
+};
+
+Rectangle.prototype.Left = function() {
+ return this.X;
+};
+
+Rectangle.prototype.Location = function() {
+ return new Point(this.X, this.Y);
+};
+
+Rectangle.prototype.Right = function() {
+ return this.X + this.Width;
+};
+
+Rectangle.prototype.Top = function() {
+ return this.Y;
+};
+
+//Class Methods
+Rectangle.Intersect = function(value1, value2) {
+ //NotImplemented
+};
@@ -0,0 +1,135 @@
+//Vector2
+
+//Constructors
+function Vector2(x, y) {
+ this.X = x;
+ this.Y = y;
+}
+
+//Class Properties
+Vector2.One = new Vector2(1,1);
+Vector2.UnitX = new Vector2(1,0);
+Vector2.UnitY = new Vector2(0,1);
+Vector2.Zero = new Vector2(0,0);
+
+//Instance Methods
+Vector2.prototype.Add = function(other) {
+ this.X += other.X;
+ this.Y += other.Y;
+ return this;
+};
+
+Vector2.prototype.DivideSingle = function(divider) {
+ this.X /= divider;
+ this.Y += divider;
+ return this;
+};
+
+Vector2.prototype.DivideVector = function(other) {
+ this.X /= other.X;
+ this.Y /= other.Y;
+ return this;
+};
+
+Vector2.prototype.Length = function() {
+ return Math.sqrt(Math.pow(this.X, 2) + Math.pow(this.Y, 2));
+};
+
+Vector2.prototype.LengthSquared = function() {
+ return Math.pow(this.Length(), 2);
+};
+
+Vector2.prototype.MultiplySingle = function(scaleFactor) {
+ this.X *= scaleFactor;
+ this.Y *= scaleFactor;
+ return this;
+};
+
+Vector2.prototype.MultiplyVector = function(other) {
+ this.X *= other.X;
+ this.Y *= other.Y;
+ return this;
+};
+
+Vector2.prototype.Normalize = function() {
+ var length = this.Length();
+ this.X /= length;
+ this.Y /= length;
+ return this;
+};
+
+Vector2.prototype.Subtract = function(other) {
+ this.X -= other.X;
+ this.Y -= other.Y;
+ return this;
+};
+
+//Class Methods
+Vector2.Add = function(value1, value2) {
+ return new Vector2(value1.X + value2.X, value1.Y + value2.Y);
+};
+
+Vector2.Clamp = function(value1, min, max) {
+ var result = new Vector(value1.X, value1.Y);
+ result.X = (value1.X > max.X) ? max.X : value1.X;
+ result.Y = (value1.Y > max.Y) ? max.Y : value1.Y;
+ result.X = (value1.X < min.X) ? min.X : value1.X;
+ result.Y = (value1.Y < min.Y) ? min.Y : value1.Y;
+ return result;
+};
+
+Vector2.Distance = function(value1, value2) {
+ return Math.sqrt(Math.pow(value2.X - value1.X, 2) + Math.pow(value2.Y - value1.Y, 2));
+};
+
+Vector2.DistanceSquared = function(value1, value2) {
+ return Math.pow(Vector2.Distance(value1, value2), 2);
+};
+
+Vector2.DivideSingle = function(value, divider) {
+ return new Vector2(value.X / divider, value.Y / divider);
+};
+
+Vector2.DivideVector = function(value1, value2) {
+ return new Vector2(value1.X / value1.X, value1.Y / value2.Y);
+};
+
+Vector2.Dot = function(value1, value2) {
+ return (value1.X * value2.X) + (value1.Y * value2.Y);
+};
+
+Vector2.Lerp = function(value1, value2, amount) {
+ return Vector2.MultiplySingle(Vector2.Add(value1, Vector2.Subtract(value2, value1)), amount);
+};
+
+Vector2.Max = function(value1, value2) {
+ return new Vector2((value1.X > value2.X) ? value1.X : value2.X, (value1.Y > value2.Y) ? value1.Y : value2.Y);
+};
+
+Vector2.Min = function(value1, value2) {
+ return new Vector2((value1.X < value2.X) ? value1.X : value2.X, (value1.Y < value2.Y) ? value1.Y : value2.Y);
+};
+
+Vector2.MultiplySingle = function(value, scaleFactor) {
+ return new Vector2(value.X * scaleFactor, value.Y * scaleFactor);
+};
+
+Vector2.MultiplyVector = function(value1, value2) {
+ return new Vector2(value1.X * value1.X, value1.Y * value2.Y);
+};
+
+Vector2.Negate = function(value) {
+ return new Vector2(value1.X * -1, value1.Y * -1);
+};
+
+Vector2.Normalize = function(value) {
+ return Vector2.DivideSingle(value, value.Length());
+};
+
+Vector2.Reflect = function(vector, normal) {
+ return Vector2.Subtract(vector, Vector2.MultiplySingle(Vector2.MultiplySingle(normal,Vector2.Dot(vector, normal)), 2));
+};
+
+Vector2.Subtract = function(value1, value2) {
+ return new Vector2(value1.X - value2.X, value1.Y - value2.Y);
+};

0 comments on commit f134b17

Please sign in to comment.