<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< script type ="text/javascript" src ="js/jquery.js" ></ script >
<script type= "text/javascript" src= "js/crafty.js" ></script>
<!-- objects -->
<script type= "text/javascript" src= "js/objects/flower.js" ></script>
<script type= "text/javascript" src= "js/objects/bush.js" ></script>
<script type= "text/javascript" src= "js/objects/grass.js" ></script>
<script type= "text/javascript" src= "js/objects/unit.js" ></script>
<script type= "text/javascript" src= "js/objects/player.js" ></script>
<script type= "text/javascript" src= "js/objects/fourway_ai.js" ></script>
<script type= "text/javascript" src= "js/objects/monster.js" ></script>
<!-- scenes -->
<script type= "text/javascript" src= "js/scenes/loading.js" ></script>
<script type= "text/javascript" src= "js/scenes/main.js" ></script>
<script type= "text/javascript" src= "js/scenes/win.js" ></script>
<script type= "text/javascript" src= "js/scenes/lose.js" ></script>
<script type= "text/javascript" src= "js/game.js" > </ script >
< link rel ="stylesheet" href ="css/game.css" type ="text/css" media ="screen" charset ="utf-8" >
< title > Simpe RPG </ title >
</ head >
< body ></ body >
</ html >
* This source code was highlighted with Source Code Highlighter .
body, html { margin:0; padding: 0; overflow:hidden; font-family:Arial; font-size:20px }
#cr-stage { border:2px solid black; margin:5px auto; color:white }
* This source code was highlighted with Source Code Highlighter .
var Settings = {
width: 400, //
height: 320, //
poligon: 16, // 16x16
level: 1, //
flower_count: 0 //
};
window.onload = function() {
Crafty.init(Settings.width, Settings.height); //
// sprite
Crafty.sprite(Settings.poligon, "images/sprite.png", {
grass1: [0,0],
grass2: [1,0],
grass3: [2,0],
grass4: [3,0],
flower: [0,1],
bush1: [0,2],
bush2: [1,2],
player: [0,3],
monster: [0,4]
});
//
Crafty.scene("loading");
};
* This source code was highlighted with Source Code Highlighter .
Crafty.scene("loading", function() {
Crafty.load(["images/sprite.png"], function() {
// , images/sprite.png
setTimeout(function() {
Crafty.scene("main");
}, 100);
});
//
Crafty.background("#000");
//
Crafty.e("2D, DOM, Text").attr({w: 100, h: 20, x: 150, y: 120})
.text("Loading... < br /> Level: " + Settings.level)
.css({"text-align": "center"});
});
* This source code was highlighted with Source Code Highlighter .
Crafty.scene("win", function() {
Settings.level += 1;
Crafty.background("#000");
Crafty.e("2D, DOM, Text").attr({w: 100, h: 20, x: 150, y: 120})
.text("You win! < br /> Level: " + Settings.level)
.css({"text-align": "center"});
setTimeout(function() {
Crafty.scene("main");
}, 1000);
});
* This source code was highlighted with Source Code Highlighter .
Crafty.scene("lose", function() {
Settings.level = 1;
Crafty.background("#000");
Crafty.e("2D, DOM, Text").attr({w: 100, h: 20, x: 150, y: 120})
.text("You lose! < br /> Level: " + Settings.level)
.css({"text-align": "center"});
setTimeout(function() {
Crafty.scene("main");
}, 1000);
});
* This source code was highlighted with Source Code Highlighter .
Crafty.c('Grass', {
init: function() {
this.requires("2D");
this.requires("Canvas");
this.requires("grass"+Crafty.randRange(1,2));
this.attr({x: 0, y: 0});
}
});
* This source code was highlighted with Source Code Highlighter .
Crafty.c('Bush', {
init: function() {
this.requires("2D");
this.requires("Canvas");
this.requires("bush"+Crafty.randRange(1,2));
this.requires("hard_bush");
this.attr({x: 0, y: 0, z: 2});
}
});
* This source code was highlighted with Source Code Highlighter .
Crafty.c('Flower', {
init: function() {
this.requires("2D");
this.requires("Canvas");
this.requires("flower");
this.requires("SpriteAnimation");
this.attr({x: 0, y: 0});
this.animate("wind", 0, 1, 3);
this.bind("EnterFrame", function() {
if(!this.isPlaying())
this.animate("wind", 80);
});
},
clear: function() {
this.removeComponent('flower');
this._visible = false;
}
});
* This source code was highlighted with Source Code Highlighter .
Crafty.c('Unit', {
init: function() {
this.requires("2D");
this.requires("Canvas");
this.requires("SpriteAnimation");
this.requires("Collision"); //
this.attr({x: 0, y: 0, z: 1});
this.collision(); //
//
this.onHit("hard_bush", function(e) {
var object = e[0].obj;
// left
if (object.x > this.x && (this.x + Settings.poligon) > object.x) {
this.x -= this._speed;
this.stop();
}
// right
if (object.x < this.x && this.x < (object.x + Settings.poligon)) {
this.x += this._speed;
this.stop();
}
// top
if (object.y < this.y && (this.y + Settings.poligon) > object.y) {
this.y += this._speed;
this.stop();
}
// bottom
if (object.y > this.y && this.y < (object.y + Settings.poligon)) {
this.y -= this._speed;
this.stop();
}
});
// , sprite
//
this.bind("Moved", function(e) {
if(this.x < ex) {
if(!this.isPlaying("walk_left"))
this.stop().animate("walk_left", 10);
}
if(this.x > ex) {
if(!this.isPlaying("walk_right"))
this.stop().animate("walk_right", 10);
}
if(this.y < ey) {
if(!this.isPlaying("walk_up"))
this.stop().animate("walk_up", 10);
}
if(this.y > ey) {
if(!this.isPlaying("walk_down"))
this.stop().animate("walk_down", 10);
}
});
}
});
* This source code was highlighted with Source Code Highlighter .
Crafty.c('Player', {
init: function() {
this.requires("Unit"); // unit
this.requires("player"); // sprite
this.requires("Fourway"); //
this.attr({x: 0, y: 0, z: 1});
this.animate("walk_left", 6, 3, 8);
this.animate("walk_right", 9, 3, 11);
this.animate("walk_up", 3, 3, 5);
this.animate("walk_down", 0, 3, 2);
this.fourway(1);
this.onHit("flower", function(e) {
var object = e[0].obj;
object.clear();
if ((Settings.flower_count -= 1) == 0) Crafty.scene("win");
});
this.onHit("monster", function(e) {
var object = e[0].obj;
object.clear();
Crafty.scene("lose");
});
}
});
* This source code was highlighted with Source Code Highlighter .
Crafty.c('Monster', {
init: function() {
this.requires("Unit");
this.requires("monster");
this.requires("FourwayAI");
this.attr({x: 0, y: 0, z: 1});
this.animate("walk_left", 6, 4, 8);
this.animate("walk_right", 9, 4, 11);
this.animate("walk_up", 3, 4, 5);
this.animate("walk_down", 0, 4, 2);
this.fourway_ai(1);
},
clear: function() {
clearInterval(this.removeComponent('monster')._interval);
}
});
* This source code was highlighted with Source Code Highlighter .
Crafty.c('FourwayAI', {
_speed: 3,
_interval: null,
init: function() {
this._movement= { x: 0, y: 0};
this.bind("EnterFrame",function() {
if (this.disableControls) return;
if(this._movement.x !== 0) {
this.x += this._movement.x;
this.trigger('Moved', {x: this.x - this._movement.x, y: this.y});
}
if(this._movement.y !== 0) {
this.y += this._movement.y;
this.trigger('Moved', {x: this.x, y: this.y - this._movement.y});
}
});
},
fourway_ai: function(speed) {
this._speed = speed;
this.make_step();
var kclass = this;
this._interval = setInterval(function() {
kclass.make_step();
}, 1000 * this._speed);
},
make_step: function() {
step = Crafty.randRange(-1,1);
if (Crafty.randRange(1,2) == 1) {
this._movement.x = step;
this._movement.y = 0;
} else {
this._movement.x = 0;
this._movement.y = step;
}
this.trigger('NewDirection', this._movement);
}
});
* This source code was highlighted with Source Code Highlighter .
Crafty.scene("main", function() {
var flower_count = Settings.level + 1;
Settings.flower_count = 0;
//generate the grass along the x-axis
for(var i = 0; i < 25; i++) {
//generate the grass along the y-axis
for(var j = 0; j < 20; j++) {
Crafty.e("Grass").attr({x: i * Settings.poligon, y: j * Settings.poligon});
if (i * Settings.poligon == 160 && j * Settings.poligon == 144) continue;
if(i > 0 && i < 24 && j > 0 && j < 19 && Crafty.randRange(0, 50) > 40) {
if (Crafty.randRange(1,10) == 1 && (flower_count -= 1) > 0) {
Crafty.e("Flower").attr({x: i * Settings.poligon, y: j * Settings.poligon});
Settings.flower_count += 1
// one monster for one flower
Crafty.e("Monster").attr({x: i * Settings.poligon, y: j * Settings.poligon});
} else {
Crafty.e("Bush").attr({x: i * Settings.poligon, y: j * Settings.poligon});
}
}
}
}
//create the bushes along the x-axis which will form the boundaries
for(var i = 0; i < 25; i++) {
Crafty.e("Bush").attr({x: i * Settings.poligon, y: 0});
Crafty.e("Bush").attr({x: i * Settings.poligon, y: 304});
}
//create the bushes along the y-axis
//we need to start one more and one less to not overlap the previous bushes
for(var i = 1; i < 19; i++) {
Crafty.e("Bush").attr({x: 0, y: i * Settings.poligon});
Crafty.e("Bush").attr({x: 384, y: i * Settings.poligon});
}
Crafty.e("Player").attr({x: 160, y: 144, z: 1});
});
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/125857/
All Articles