-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree_cutting_Simulator_js.js
More file actions
186 lines (158 loc) · 6.26 KB
/
Tree_cutting_Simulator_js.js
File metadata and controls
186 lines (158 loc) · 6.26 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
function setup() {
}
function draw() {
}
/*
C0NTR0LS W=UP S= DOWN A= LEFT =D RIGHT TO CUT TREE DOWN HIT WITH AXE
IF U ARE NOT IN p5.js then set to p5.js please
*/
let camX=0, camY=0;
let trees=[], items=[];
let money=0;
let draggingItem=null;
const gravity=0.5;
const groundY=60;
let sander={x:300,y:groundY,w:120,h:40,processing:null};
let sellBtn;
function setup(){
createCanvas(windowWidth, windowHeight);
spawnTree(0,0,'oak');
items.push({kind:'axe',x:-50,y:0,w:16,h:32,vx:0,vy:0,dragging:false});
sellBtn=createButton('Sell Planks');
sellBtn.position(20, windowHeight - 60);
sellBtn.style('font-size','16px');
sellBtn.style('padding','6px 12px');
sellBtn.mousePressed(sellPlanks);
}
function draw(){
background(140,200,255);
push(); translate(width/2+camX,height/2+camY);
drawGround();
drawSander();
for(let t of trees) drawTree(t);
for(let it of items){ updateItem(it); drawItem(it); }
handleAxeHits();
handleSanderProcessing();
pop();
drawHUD();
handleInput();
drawPopulationGraph();
}
function drawGround(){ fill(100,200,100); rect(-4000,groundY,8000,4000); }
function drawSander(){
push(); translate(sander.x,sander.y);
rectMode(CENTER); fill(180); rect(0,0,sander.w,sander.h,8);
fill(100); rect(-sander.w/2+10,0,20,sander.h-10); rect(sander.w/2-10,0,20,sander.h-10);
fill(0); textAlign(CENTER,CENTER); textSize(14); text('SANDER',0,-sander.h/2-14);
pop();
}
function updateItem(it){
if(!it.dragging){
it.vy+=gravity; it.y+=it.vy; it.x+=it.vx;
if(it.y>groundY){ it.y=groundY; it.vy=0; }
} else {
it.vx=0; it.vy=0; it.x=mouseX-width/2-camX; it.y=mouseY-height/2-camY;
}
}
function drawItem(it){
push(); translate(it.x,it.y);
if(it.kind=='axe'){ fill(150,75,0); rect(-4,-16,8,32,2); fill(180); rect(-10,-20,20,8); }
else if(it.kind=='log'){ fill(110,70,40); rect(0,0,40,16,6); }
else if(it.kind=='sapling'){ fill(70,140,70); ellipse(0,-6,14,12); fill(90,60,40); rect(-2,0,4,12,2); }
else if(it.kind=='plank'){ fill(200,150,100); rect(0,0,26,12,3); }
if(it.dragging){ noFill(); stroke(0); strokeWeight(2); ellipse(0,0,40); }
pop();
}
function drawTree(t){
if(t.fallen) return;
push(); translate(t.x,t.y);
if(t.type=='gold') fill(212,175,55);
else if(t.type=='birch') fill(240,240,220);
else if(t.type=='rainbow') fill(lerpColor(color(255,0,0), color(0,0,255),(sin(frameCount*0.05)+1)/2));
else fill(120,70,40);
rect(-8,0,16,60,4);
fill(40,160,40); ellipse(0,-30,80,70);
let ratio=constrain(t.hitCount/t.hitsNeeded,0,1);
stroke(0); noFill(); rectMode(CENTER); rect(0,50,40,6);
noStroke(); fill(255,0,0); rectMode(CORNER); rect(-20,47,40*ratio,6);
pop();
}
function spawnTree(x,y,type='oak'){ trees.push({x:x,y:y,type:type,fallen:false,hitCount:0,hitsNeeded:hitsForType(type)}); }
function hitsForType(type){ if(type=='rainbow') return 22; if(type=='gold') return 18; if(type=='birch') return 10; return 8; }
function handleInput(){ if(keyIsDown(87)) camY+=6; if(keyIsDown(83)) camY-=6; if(keyIsDown(65)) camX+=6; if(keyIsDown(68)) camX-=6; }
function mousePressed(){
for(let it of items){
if(dist(mouseX-width/2-camX, mouseY-height/2-camY,it.x,it.y)<30){ it.dragging=true; draggingItem=it; break; }
}
}
function mouseDragged(){ if(draggingItem){ draggingItem.x=mouseX-width/2-camX; draggingItem.y=mouseY-height/2-camY; } }
function mouseReleased(){
if(draggingItem){
if(draggingItem.kind=='sapling' && draggingItem.y>groundY){ spawnTree(draggingItem.x,draggingItem.y,draggingItem.type||'oak'); items.splice(items.indexOf(draggingItem),1); }
draggingItem.dragging=false; draggingItem=null;
}
}
function handleAxeHits(){
let axe=items.find(it=>it.kind=='axe');
if(!axe) return;
for(let t of trees){
if(t.fallen) continue;
if(dist(axe.x,axe.y,t.x,t.y)<30){
t.hitCount++;
if(t.hitCount>=t.hitsNeeded){
t.fallen=true;
// Spawn logs and saplings immediately
for(let i=0;i<2;i++) items.push({kind:'sapling',type:mutateTypeMaybe(t.type),x:t.x+random(-10,10),y:t.y,vx:0,vy:0,dragging:false});
items.push({kind:'log',x:t.x,y:t.y+20,w:40,h:16,vx:0,vy:0,dragging:false,sourceType:t.type});
}
}
}
}
function handleSanderProcessing(){
for(let it of items){
if(it.kind=='log' && !it.dragging){
let dx=it.x-sander.x, dy=it.y-sander.y;
if(abs(dx)<sander.w/2 && abs(dy)<sander.h/2){
// Convert log to plank
items.push({kind:'plank',type:it.sourceType, x:it.x, y:it.y, vx:0, vy:0, dragging:false});
items.splice(items.indexOf(it),1);
}
}
}
}
function mutateTypeMaybe(type){ const r=random(); if(r<0.01) return 'rainbow'; if(r<0.04) return 'gold'; if(r<0.14) return 'birch'; return type; }
function drawHUD(){
fill(255); rect(8,8,180,40,8);
fill(0); textSize(16); textAlign(LEFT,TOP); text('Money: $'+nf(money,0,2),16,14);
}
function sellPlanks(){
for(let i=items.length-1;i>=0;i--){
if(items[i].kind=='plank'){
let price=0;
if(items[i].type=='oak') price=10;
else if(items[i].type=='birch') price=15;
else if(items[i].type=='rainbow') price=175;
else if(items[i].type=='gold') price=545;
money+=price;
items.splice(i,1);
}
}
}
function drawPopulationGraph(){
const x=width-260,y=100,w=240,h=170;
fill(255); rect(x,y,w,h,10);
fill(0); textSize(14); textAlign(LEFT,TOP); text('Alive Trees',x+12,y+12);
let counts={oak:0,birch:0,gold:0,rainbow:0};
for(let t of trees){ if(!t.fallen) counts[t.type]=counts[t.type]?counts[t.type]+1:1; }
let types=['oak','birch','gold','rainbow'];
let maxCount=max(Object.values(counts).concat(1));
for(let i=0;i<types.length;i++){
let ty=types[i];
let barH=map(counts[ty],0,maxCount,0,h-60);
fill(typeColor(ty));
rect(x+40+i*55,y+h-20-barH,35,barH);
fill(0); textSize(12); textAlign(CENTER); text(ty+': '+counts[ty],x+57+i*55,y+h+10);
}
}
function typeColor(type){ if(type=='gold') return color(212,175,55); if(type=='birch') return color(240,240,220); if(type=='rainbow') return color(255,0,255); return color(120,70,40); }
function windowResized(){ resizeCanvas(windowWidth,windowHeight); sellBtn.position(20, windowHeight - 60); }