This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo2.js
129 lines (120 loc) · 4.11 KB
/
demo2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* CraftyMouseFace - 2D Shooter / Demo #2
*
* Url: https://github.com/petarov/CraftyMouseFace
* Sprites & Tiles: http://art.devsader.com/browse
*/
$(document).ready(function() {
// Init Crafty Engine
Crafty.init(832, 512).canvas.init();
Crafty.background('#000');
// Change this to "DOM" if rendering is slow
var render = "Canvas";
// Load assets
Crafty.scene("load", function() {
Crafty.load([
"assets/InsideTiles_Moosader.png",
"assets/RunningDood_Moosader.png",
],
function() {
// load gfx
Crafty.sprite(64, "assets/InsideTiles_Moosader.png", {
wall: [1,1],
floor1: [0,2],
floor2: [1,2],
});
Crafty.sprite("assets/RunningDood_Moosader.png", {
player: [0, 0, 80, 30],
});
$('#loading').hide();
Crafty.scene('game');
},
// On Progress
function(e) {
$('#loading').html('Loaded: ' + e.percent.toFixed(0) + '%');
},
// On Error
function(e) {
$('#loading').html('Could not load: ' + e.src);
});
});
// main scene
Crafty.scene("game", function() {
// show FPS
Crafty.e("2D, " + render + ", FPS").attr({maxValues:10})
.bind("MessureFPS", function(fps) {
$('#fps').text('FPS: ' + fps.value);
});
var zbase = 2;
// draw tile floor
for(var i = 0; i < 13; i++) {
for(var j = 0; j < 8; j++) {
Crafty.e("2D, " + render + ", floor2")
.attr({x: i * 64, y: j * 64, z: zbase});
}
}
// character animation
Crafty.c('CharAnims', {
CharAnims: function() {
var animSpeed = 200
// setup animations sequences
this.requires("SpriteAnimation, Grid")
.reel("walk", animSpeed, [ [0, 90], [0, 60], [0, 30], [0, 0] ]);
return this;
}
});
// create character
Crafty.e("2D, " + render + ", player, CharAnims, Multiway, MouseFace, BoxOverlays")
.attr({
move: {left: false, right: false, up: false, down: false},
x: 400, y: 256, z: zbase + 1,
moving: false,
curAngle: 0,
})
.origin('center') // rotate origin
.MouseFace({x: 40, y: 15})
.CharAnims()
.bind("Moved", function(from) {
this.moving = true;
})
.bind("EnterFrame", function() {
// If moving, adjust the proper animation and facing
if (this.moving) {
if (!this.isPlaying('walk'))
this.animate('walk', 8, -1);
this.moving = false;
} else {
this.pauseAnimation();
}
})
.multiway(2, {W: -90, S: 90, D: 0, A: 180})
.bind("MouseMoved", function(e) {
// adjust player sprite facing
// we add +90, since initially sprite is facing PI/2
this.curAngle = (e.grad) + 90;
this.rotation = this.curAngle;
})
.bind("MouseUp", function(data) {
if (data.mouseButton == Crafty.mouseButtons.LEFT) {
// shoot - create bullet
Crafty.e("2D, " + render + ", Color")
.attr({
x: this.x + 40, y: this.y + 15, z: zbase + 1,
w: 3, h: 3,
speed: 5,
angle: this._dirAngle + Math.PI
})
.color("#FA5656")
.bind("EnterFrame", function(frame) {
this.x += Math.cos(this.angle) * this.speed;
this.y += Math.sin(this.angle) * this.speed;
if (this.x > Crafty.viewport.width || this.x < 0) {
this.destroy();
}
});
}
});
});
// start
Crafty.scene('load');
});