daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest May 16th, 2018 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.onload = function()
  2. {
  3.     Crafty.init(400, 320);
  4.     Crafty.canvas.init();
  5.  
  6.     Crafty.sprite(16, "sprite.png", {
  7.     grass1: [0,0],
  8.     grass2: [1,0],
  9.     grass3: [2,0],
  10.     grass4: [3,0],
  11.     flower: [0,1],
  12.     bush1: [0,2],
  13.     bush2: [1,2],
  14.     player: [0,3]
  15.     });
  16.  
  17.     function generateWorld() {
  18.     //generate the grass along the x-axis
  19.     for(var i = 0; i < 25; i++)
  20.     {
  21.         //generate the grass along the y-axis
  22.         for(var j = 0; j < 20; j++)
  23.         {
  24.         grassType = Crafty.randRange(1, 4);
  25.         Crafty.e("2D, Canvas, grass"+grassType)
  26.                 .attr({x: i * 16, y: j * 16});
  27.  
  28.         //1/50 chance of drawing a flower and only within the bushes
  29.         if(i > 0 && i < 24 && j > 0 && j < 19 && Crafty.randRange(0, 50) > 49)
  30.         {
  31.         Crafty.e("2D, DOM, flower, solid, SpriteAnimation")
  32.                 .attr({x: i * 16, y: j * 16})
  33.                 .animate("wind", 0, 1, 3)
  34.                 .bind("EnterFrame", function() {
  35.                 if(!this.isPlaying())
  36.                 this.animate("wind", 80);
  37.                 });
  38.         }
  39.         }
  40.     }
  41.  
  42. //create the bushes along the x-axis which will form the boundaries
  43. for(var i = 0; i < 25; i++) {
  44. Crafty.e("2D, Canvas, wall_top, solid, bush"+Crafty.randRange(1,2))
  45. .attr({x: i * 16, y: 0, z: 2});
  46. Crafty.e("2D, DOM, wall_bottom, solid, bush"+Crafty.randRange(1,2))
  47. .attr({x: i * 16, y: 304, z: 2});
  48. }
  49.  
  50. //create the bushes along the y-axis
  51. //we need to start one more and one less to not overlap the previous bushes
  52. for(var i = 1; i < 19; i++) {
  53. Crafty.e("2D, DOM, wall_left, solid, bush"+Crafty.randRange(1,2))
  54. .attr({x: 0, y: i * 16, z: 2});
  55. Crafty.e("2D, Canvas, wall_right, solid, bush"+Crafty.randRange(1,2))
  56. .attr({x: 384, y: i * 16, z: 2});
  57. }
  58. }
  59.  
  60. //the loading screen that will display while our assets load
  61. Crafty.scene("loading", function() {
  62. //load takes an array of assets and a callback when complete
  63. Crafty.load(["sprite.png"], function() {
  64. Crafty.scene("main"); //when everything is loaded, run the main scene
  65. });
  66.  
  67. //black background with some loading text
  68. Crafty.background("#000");
  69. Crafty.e("2D, DOM, Text").attr({w: 100, h: 20, x: 150, y: 120})
  70. .text("Loading")
  71. .css({"text-align": "center"});
  72. });
  73.  
  74. //automatically play the loading scene
  75. Crafty.scene("loading");
  76.  
  77. Crafty.scene("main", function() {
  78. generateWorld();
  79.  
  80. Crafty.c('Hero', {
  81. init: function() {
  82. //setup animations
  83. this.requires("SpriteAnimation, Collision")
  84. .animate("walk_left", 6, 3, 8)
  85. .animate("walk_right", 9, 3, 11)
  86. .animate("walk_up", 3, 3, 5)
  87. .animate("walk_down", 0, 3, 2)
  88. //change direction when a direction change event is received
  89. .bind("NewDirection",
  90. function (direction) {
  91. if (direction.x < 0) {
  92. if (!this.isPlaying("walk_left"))
  93. this.stop().animate("walk_left", 10, -1);
  94. }
  95. if (direction.x > 0) {
  96. if (!this.isPlaying("walk_right"))
  97. this.stop().animate("walk_right", 10, -1);
  98. }
  99. if (direction.y < 0) {
  100. if (!this.isPlaying("walk_up"))
  101. this.stop().animate("walk_up", 10, -1);
  102. }
  103. if (direction.y > 0) {
  104. if (!this.isPlaying("walk_down"))
  105. this.stop().animate("walk_down", 10, -1);
  106. }
  107. if(!direction.x && !direction.y) {
  108. this.stop();
  109. }
  110. })
  111. // A rudimentary way to prevent the user from passing solid areas
  112. .bind('Moved', function(from) {
  113. if(this.hit('solid')){
  114. this.attr({x: from.x, y:from.y});
  115. }
  116. });
  117. return this;
  118. }
  119. });
  120.  
  121. Crafty.c("RightControls", {
  122. init: function() {
  123. this.requires('Multiway');
  124. },
  125.  
  126. rightControls: function(speed) {
  127. this.multiway(speed, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180})
  128. return this;
  129. }
  130.  
  131. });
  132.  
  133. //create our player entity with some premade components
  134. player = Crafty.e("2D, Canvas, player, RightControls, Hero, Animate, Collision, WiredHitBox")
  135.         .attr({x: 160, y: 144, z: 1})
  136.         .rightControls(1);
  137. });
  138. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top