-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
155 lines (128 loc) · 3.1 KB
/
sketch.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const Engine = Matter.Engine;
const Render = Matter.Render;
const World = Matter.World;
const Bodies = Matter.Bodies;
const Constraint = Matter.Constraint;
const Body = Matter.Body;
const Composites = Matter.Composites;
const Composite = Matter.Composite;
let engine;
let world;
var plank;
var ground;
var higherground;
var con;
var con2;
var rope;
var bubble,bubble_img;
function preload()
{
bubble_img = loadImage("bubble.png")
bg_img = loadImage('background.png');
food = loadImage('melon.png');
rabbit = loadImage('Rabbit-01.png');
blink = loadAnimation("blink_1.png","blink_2.png","blink_3.png");
eat = loadAnimation("eat_0.png" , "eat_1.png","eat_2.png","eat_3.png","eat_4.png");
sad = loadAnimation("sad_1.png","sad_2.png","sad_3.png");
star_img = loadImage('star.png');
blink.playing = true;
eat.playing = true;
sad.playing = true;
sad.looping= false;
eat.looping = false;
}
function setup() {
createCanvas(500,800);
frameRate(80);
engine = Engine.create();
world = engine.world;
var fruit_options = {
restitution: 0.8
}
ground =new Ground(250,height-10,width,20);
fruit = Bodies.circle(100,400,15,fruit_options);
World.add(world,fruit);
bubble = createSprite(290,460,20,20);
bubble.addImage(bubble_img);
bubble.scale = 0.1;
//sprite do coelhinho
blink.frameDelay = 20;
eat.frameDelay = 20;
bunny = createSprite(270,100,100,100);
bunny.addImage(rabbit);
bunny.scale = 0.2;
higherground =new Ground(300,170,100,10);
bunny.addAnimation('blinking',blink);
bunny.addAnimation('eating',eat);
bunny.addAnimation('crying',sad);
bunny.changeAnimation('blinking');
rope = new Rope(4,{x:230,y:330});
rope2 = new Rope(4,{x:50,y:450});
con = new Link(rope,fruit);
con2 = new Link(rope2,fruit);
button = createImg('cut_btn.png');
button.position(200,320);
button.size(50,50);
button2 = createImg('cut_btn.png');
button2.position(30,420);
button2.size(50,50);
button2.mouseClicked(drop);
ellipseMode(RADIUS);
}
function draw()
{
background(51);
image(bg_img,0,0,width,height);
Engine.update(engine);
push();
imageMode(CENTER);
if(fruit!=null){
image(food,fruit.position.x,fruit.position.y,70,70);
}
pop();
ground.show();
higherground.show();
rope.show();
rope2.show();
if(collide(fruit,bunny,80)==true)
{
remove_rope();
bubble.visible = false;
World.remove(engine.world,fruit);
fruit = null;
bunny.changeAnimation('eating');
}
if(collide(fruit,bubble,40) == true)
{
engine.world.gravity.y = -1;
bubble.position.x = fruit.position.x;
bubble.position.y = fruit.position.y;
}
drawSprites();
}
function drop()
{
rope2.break();
con2.dettach();
con2 = null;
}
function remove_rope()
{
rope.break();
con.dettach();
con = null;
}
function collide(body,sprite,x)
{
if(body!=null)
{
var d = dist(body.position.x,body.position.y,sprite.position.x,sprite.position.y);
if(d<=x)
{
return true;
}
else{
return false;
}
}
}