File size: 9,564 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gold Miner</title>
<style>
  body { font-family: system-ui, sans-serif; background: #3b2410; color: #ffe8b0; text-align: center; margin: 0; padding: 12px; }
  h1 { color: #ffcc44; }
  #hud { font-size: 16px; margin: 6px; }
  #game { background: #6b4a23; border: 3px solid #ffcc44; display: block; margin: 10px auto; max-width:100%;height:auto; }
  .menu, .controls { margin: 10px; }
  button { padding: 8px 16px; font-size: 15px; border: none; border-radius: 6px;
           background: #ffcc44; color: #3b2410; cursor: pointer; margin: 0 4px; }
  #tip { color: #d9b98a; }
</style>
</head>
<body>
  <h1>Gold Miner</h1>
  <div class="menu" data-testid="mode-menu">
    <label for="mode">Game mode</label>
    <select id="mode" data-testid="game-mode">
      <option value="single">Single player</option>
      <option value="double">Two players</option>
    </select>
    <button id="start" data-testid="start-game">Start</button>
  </div>
  <section id="level" hidden>
    <div id="hud">
      Player A Score: <span id="scoreA">0</span> ·
      Player B Score: <span id="scoreB">0</span> ·
      Target: <span id="targetScore">800</span>
    </div>
    <canvas id="game" width="640" height="380"></canvas>
    <div class="controls">
      <button id="dropA">Player A Drop Hook (S)</button>
      <button id="dropB">Player B Drop Hook (↓)</button>
      <button id="reset">Reset</button>
    </div>
    <p id="tip"></p>
  </section>

<script>
  const level = document.getElementById('level');
  const canvas = document.getElementById('game');
  const ctx = canvas.getContext('2d');
  const TARGET = 800; // reach 800 score to win

  let mode = 'single';   // single / double
  let scores = { A: 0, B: 0 };
  let items = [];
  let hooks = [];
  let testMode = false;
  let explosions = 0;
  let effects = [];
  let testTarget = TARGET;

  // item types with value, weight and how fast the hook reels them in (speed)
  const TYPES = {
    diamond: { value: 800, weight: 1, color: '#7ad7ff', r: 8 },   // diamond: lightest, most valuable
    pig:     { value: 3,   weight: 1, color: '#ff9bd0', r: 12, moves: true }, // pig runs sideways, slow
    bomb:    { value: 2,   weight: 2, color: '#222',    r: 12 },   // bomb: explodes
    goldS:   { value: 50,  weight: 3, color: '#ffd000', r: 10 },   // small gold
    goldL:   { value: 200, weight: 6, color: '#ffb000', r: 18 },   // large gold (heavier, slower)
    stone:   { value: 8,   weight: 9, color: '#888',    r: 16 },   // stone / rock: heaviest
  };

  function makeItems() {
    items = [
      // at least 3 diamonds
      { type: 'diamond', x: 80,  y: 250 },
      { type: 'diamond', x: 300, y: 320 },
      { type: 'diamond', x: 540, y: 270 },
      { type: 'diamond', x: 430, y: 340 },
      { type: 'pig',     x: 200, y: 300, dir: 1 },
      { type: 'bomb',    x: 360, y: 230 },
      { type: 'goldS',   x: 130, y: 330 },
      { type: 'goldL',   x: 480, y: 320 },
      { type: 'stone',   x: 250, y: 360 },
      { type: 'stone',   x: 580, y: 350, def: {r:22, weight:12, value:15} },
    ].map(it => ({ ...it, def: {...TYPES[it.type], ...it.def}, grabbed: false }));
  }

  function makeHooks() {
    hooks = [
      { player: 'A', baseX: mode === 'double' ? 200 : 320, baseY: 40,
        angle: 0, dir: 1, state: 'swing', len: 0, holding: null },
    ];
    if (mode === 'double') {
      hooks.push({ player: 'B', baseX: 440, baseY: 40,
        angle: 0, dir: 1, state: 'swing', len: 0, holding: null });
    }
  }

  function startGame(m) {
    mode = m;
    scores = { A: 0, B: 0 };
    effects = [];
    explosions = 0;
    testTarget = TARGET;
    document.getElementById('tip').textContent = 'Player A uses S and Player B uses ArrowDown to control when the hook drops.';
    makeItems();
    makeHooks();
    level.hidden = false;
    document.getElementById('dropB').hidden = mode !== 'double';
    updateHud();
    draw();
  }

  function drop(player) {
    const hook = hooks.find(h => h.player === player);
    if (hook && hook.state === 'swing') hook.state = 'extend';
  }

  function explode(x, y) {
    effects.push({x, y, frames: 16});
  }

  function tipEaster(player) {
    // victory / win easter egg shown when a player reaches the target
    const tip = document.getElementById('tip');
    tip.textContent = '🎉🏆 Player ' + player + ' wins! Victory Easter egg unlocked! 🎉';
  }

  function updateHud() {
    document.getElementById('scoreA').textContent = scores.A;
    document.getElementById('scoreB').textContent = scores.B;
    document.getElementById('targetScore').textContent = testTarget;
  }

  function update() {
    effects = effects.map(effect => ({...effect, frames: effect.frames-1})).filter(effect => effect.frames > 0);
    for (const item of items) {
      if (item.def.moves && !item.grabbed) {
        item.x += item.dir * 0.6; // pig moves slowly sideways
        if (item.x < 40 || item.x > canvas.width - 40) item.dir *= -1;
      }
    }
    for (const hook of hooks) {
      const tipX = hook.baseX + Math.sin(hook.angle) * (40 + hook.len);
      const tipY = hook.baseY + Math.cos(hook.angle) * (40 + hook.len);
      if (hook.state === 'swing') {
        hook.angle += hook.dir * 0.03;
        if (hook.angle > 1.1) hook.dir = -1;
        if (hook.angle < -1.1) hook.dir = 1;
      } else if (hook.state === 'extend') {
        hook.len += 5;
        if (!hook.holding) {
          for (const item of items) {
            if (item.grabbed) continue;
            if (Math.hypot(item.x - tipX, item.y - tipY) < item.def.r + 6) {
              item.grabbed = true;
              hook.holding = item;
              if (item.type === 'bomb') { explode(tipX, tipY); explosions++; hook.fastReturn = true; }
              hook.state = 'reel';
              break;
            }
          }
        }
        // reached screen edge without grabbing -> return back
        if (tipX < 0 || tipX > canvas.width || tipY > canvas.height) hook.state = 'reel';
      } else if (hook.state === 'reel') {
        const speed = hook.fastReturn ? 30 : hook.holding ? Math.max(2, 12 - hook.holding.def.weight) : 12; // heavier = slower; bombs return fast
        hook.len -= speed;
        if (hook.holding) { hook.holding.x = tipX; hook.holding.y = tipY; }
        if (hook.len <= 0) {
          hook.len = 0;
          if (hook.holding) {
            scores[hook.player] += hook.holding.def.value;
            items = items.filter(i => i !== hook.holding);
            hook.holding = null;
            hook.fastReturn = false;
            updateHud();
            if (scores[hook.player] >= (typeof testTarget === 'number' ? testTarget : TARGET)) tipEaster(hook.player);
          }
          hook.state = 'swing';
        }
      }
    }
  }

  function draw() {
    ctx.fillStyle = '#6b4a23';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.fillStyle = '#c79a5b';
    ctx.fillRect(0, 0, canvas.width, 60); // surface where hooks hang
    for (const item of items) {
      ctx.fillStyle = item.def.color;
      ctx.beginPath();
      ctx.arc(item.x, item.y, item.def.r, 0, Math.PI * 2);
      ctx.fill();
    }
    for (const hook of hooks) {
      const tipX = hook.baseX + Math.sin(hook.angle) * (40 + hook.len);
      const tipY = hook.baseY + Math.cos(hook.angle) * (40 + hook.len);
      ctx.strokeStyle = '#eee';
      ctx.lineWidth = 2;
      ctx.beginPath();
      ctx.moveTo(hook.baseX, hook.baseY);
      ctx.lineTo(tipX, tipY);
      ctx.stroke();
      ctx.fillStyle = '#ddd';
      ctx.fillRect(tipX - 5, tipY - 5, 10, 10);
    }
    for (const effect of effects) {
      ctx.fillStyle = 'rgba(255,80,0,0.8)';
      ctx.beginPath();
      ctx.arc(effect.x, effect.y, 36 * effect.frames / 16, 0, Math.PI * 2);
      ctx.fill();
    }
  }

  function loop() {
    if (!level.hidden) {
      if (!testMode) update();
      draw();
    }
    requestAnimationFrame(loop);
  }

  document.addEventListener('keydown', (e) => {
    if (level.hidden) return;
    if (e.key === 's' || e.key === 'S') drop('A');
    if (e.key === 'ArrowDown') { e.preventDefault(); drop('B'); }
  });
  document.getElementById('start').addEventListener('click', () => {
    testMode = false;
    startGame(document.getElementById('mode').value);
  });
  document.getElementById('dropA').addEventListener('click', () => drop('A'));
  document.getElementById('dropB').addEventListener('click', () => drop('B'));
  document.getElementById('reset').addEventListener('click', () => startGame(mode));

  requestAnimationFrame(loop);
  window.gameTest = {
    reset(config={}) {
      testMode=true; explosions=0; startGame(config.mode||'single'); testTarget=config.targetScore||TARGET;
      if(config.items) items=config.items.map(it=>({dir:1,...it,def:{...(TYPES[it.type]||{}),...(it.def||{})},grabbed:false}));
      updateHud(); draw();
      return this.state();
    },
    step(ms) { for(let i=0;i<Math.max(1,Math.round(ms/16));i++) update(); draw(); return this.state(); },
    dropClaw(player='A') { drop(player); return this.state(); },
    state() { return { scores:{...scores}, target:testTarget, won:Object.values(scores).some(x=>x>=testTarget), explosions, items:items.map(i=>({type:i.type,x:i.x,y:i.y,value:i.def.value,weight:i.def.weight,grabbed:i.grabbed})), hooks:hooks.map(h=>({player:h.player,state:h.state,len:h.len,holding:h.holding?.type||null,fastReturn:!!h.fastReturn})) }; }
  };
</script>
</body>
</html>