-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jsbattle-docs): add walkthroughs
- Loading branch information
Showing
15 changed files
with
459 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Challenge Walkthroughs | ||
|
||
This section is contains solution of all JsBattle Challenges. If you got stuck, you can check the solution here. Try to not use it too much since it will take all the fun away from you. | ||
|
||
- [Challenge #1](./challenge01.md) | ||
- [Challenge #2](./challenge02.md) | ||
- [Challenge #3](./challenge03.md) | ||
- [Challenge #4](./challenge04.md) | ||
- [Challenge #5](./challenge05.md) | ||
- [Challenge #6](./challenge06.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Challenge #1 Walkthrough | ||
|
||
```javascript | ||
importScripts('lib/tank.js'); | ||
|
||
tank.init(function(settings, info) { | ||
// initialize tank here | ||
}); | ||
|
||
tank.loop(function(state, control) { | ||
// Change control object to start shooting. | ||
// The loop() function is called at each simulation | ||
// step resulting in continuous fire. | ||
control.SHOOT = 1; | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Challenge #2 Walkthrough | ||
|
||
```javascript | ||
importScripts('lib/tank.js'); | ||
|
||
tank.init(function(settings, info) { | ||
// initialize tank here | ||
}); | ||
|
||
tank.loop(function(state, control) { | ||
// calculate desired direction of the gun. | ||
// Take into account rotation of the tank | ||
const targetAngle = 45 | ||
const gunAngle = Math.deg.normalize(targetAngle - state.angle); | ||
|
||
// point the gun at the target. | ||
const turnSpeed = 0.1; | ||
const angleDiff = Math.deg.normalize(gunAngle - state.gun.angle); | ||
control.GUN_TURN = turnSpeed * angleDiff; | ||
|
||
// keep shooting | ||
control.SHOOT = 1; | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Challenge #3 Walkthrough | ||
|
||
```javascript | ||
importScripts('lib/tank.js'); | ||
|
||
tank.init(function(settings, info) { | ||
// initialize tank here | ||
}); | ||
|
||
tank.loop(function(state, control) { | ||
let enemy = state.radar.enemy; | ||
// Keep scannig until you find the enemy | ||
if(!enemy) { | ||
control.RADAR_TURN = 1; | ||
} else { | ||
control.RADAR_TURN = 0; | ||
} | ||
|
||
// Aim at the target when found | ||
if(enemy) { | ||
// Calculate desired direction of the gun. | ||
// If you need additional explanation on math operations | ||
// read the docs: Algorithms / Geometry Basics | ||
const targetAngle = Math.deg.atan2(enemy.y - state.y, enemy.x - state.x); | ||
const gunAngle = Math.deg.normalize(targetAngle - state.angle); | ||
|
||
// point the gun at the target | ||
const turnSpeed = 0.1; | ||
const angleDiff = Math.deg.normalize(gunAngle - state.gun.angle); | ||
control.GUN_TURN = turnSpeed * angleDiff; | ||
|
||
// keep shooting | ||
control.SHOOT = 1; | ||
} | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Challenge #4 Walkthrough | ||
|
||
```javascript | ||
importScripts('lib/tank.js'); | ||
|
||
// SHOOT ENEMY --------------------------------------------------------------------------------- | ||
function shootEnemy(state, control) { | ||
let enemy = state.radar.enemy; | ||
if(!enemy) { | ||
return; | ||
} | ||
|
||
// predict position of moving target | ||
let bulletSpeed = 4; | ||
let distance = Math.distance(state.x, state.y, enemy.x, enemy.y) | ||
let bulletTime = distance / bulletSpeed; | ||
let targetX = enemy.x + bulletTime * enemy.speed * Math.cos(Math.deg2rad(enemy.angle)); | ||
let targetY = enemy.y + bulletTime * enemy.speed * Math.sin(Math.deg2rad(enemy.angle)); | ||
|
||
// calculate desired direction of the gun | ||
let targetAngle = Math.deg.atan2(targetY - state.y, targetX - state.x); | ||
let gunAngle = Math.deg.normalize(targetAngle - state.angle); | ||
|
||
// point the gun at the target | ||
let angleDiff = Math.deg.normalize(gunAngle - state.gun.angle); | ||
control.GUN_TURN = 0.3 * angleDiff; | ||
|
||
// shoot when aiming at target | ||
if(Math.abs(angleDiff) < 1) { | ||
control.SHOOT = 0.5; | ||
} | ||
} | ||
|
||
// SCAN ENEMY --------------------------------------------------------------------------------- | ||
function scanEnemy(state, control) { | ||
if(!state.radar.enemy) { | ||
// scan around for the enemy | ||
control.RADAR_TURN = 1; | ||
} else { | ||
//keep the enemy in the middle of radar beam | ||
let targetAngle = Math.deg.atan2(state.radar.enemy.y - state.y, state.radar.enemy.x - state.x); | ||
let radarAngle = Math.deg.normalize(targetAngle - state.angle); | ||
let angleDiff = Math.deg.normalize(radarAngle - state.radar.angle); | ||
control.RADAR_TURN = angleDiff; | ||
} | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------- | ||
tank.init(function(settings, info) { | ||
// initialize tank here | ||
}); | ||
|
||
tank.loop(function(state, control) { | ||
scanEnemy(state, control); | ||
shootEnemy(state, control); | ||
}); | ||
``` |
Oops, something went wrong.