Skip to content

Instantly share code, notes, and snippets.

@ehzhang
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehzhang/9754635 to your computer and use it in GitHub Desktop.
Save ehzhang/9754635 to your computer and use it in GitHub Desktop.
Pokemon Pebble
<html style="width:100%; height:100%; overflow:hidden">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
padding: 100px;
}
.poke {
font-size: 8em;
}
</style>
</head>
<body style="width:100%; height:100%; overflow:hidden; margin:0">
<div class ="your poke">
</div>
<div class = "your health">
</div>
<br>
<br>
<div class="opp poke">
</div>
<div class = "opp health">
</div>
<script>
var turnData = {
name: "Edwin",
pokemon: [
{
name: "Pikachu",
level: 23,
hp: 80,
maxhp: 100,
moves: [
{
name: "Thundershock",
type: "Electric",
power: 40
},
{
name: "Thunder",
type: "Electric",
power: 110
},
{
name: "Tail Whip",
type: "Normal",
power: 0
},
{
name: "Tackle",
type: "Normal",
power: 50
}
]
},
{
name: "Squirtle",
level: 25,
hp: 80,
maxhp: 90,
moves: [
{
name: "Water Gun",
type: "Water",
power: 40
},
{
name: "Withdraw",
type: "Normal",
power: 0
},
{
name: "Tackle",
type: "Normal",
power: 50
}
]
},
{
name: "Bulbasaur",
level: 28,
hp: 90,
maxhp: 90,
moves: [
{
name: "Vine Whip",
type: "Grass",
power: 40
},
{
name: "Razor Leaf",
type: "Grass",
power: 70
},
{
name: "Leech Seed",
type: "Grass",
power: 0
},
{
name: "Tackle",
type: "Normal",
power: 50
}
]
},
{
name: "Butterfree",
level: 27,
hp: 80,
maxhp: 80,
moves: [
{
name: "Confusion",
type: "Psychic",
power: 50
},
{
name: "Gust",
type: "Flying",
power: 40
},
{
name: "Psybeam",
type: "Psychic",
power: 65
},
{
name: "Tackle",
type: "Normal",
power: 50
}
]
},
{
name: "Pikachu",
level: 28,
hp: 100,
maxhp: 100,
moves: [
{
name: "Thundershock",
type: "Electric",
power: 40
},
{
name: "Thunder",
type: "Electric",
power: 110
},
{
name: "Tail Whip",
type: "Normal",
power: 0
},
{
name: "Tackle",
type: "Normal",
power: 50
}
]
},
{
name: "Zapdos",
level: 50,
hp: 200,
maxhp: 200,
moves: [
{
name: "Thundershock",
type: "Electric",
power: 40
},
{
name: "Thunder",
type: "Electric",
power: 110
},
{
name: "Drill Peck",
type: "Flying",
power: 100
},
{
name: "Tackle",
type: "Normal",
power: 50
}
]
}
]
};
function main() {
if (WS.scriptVersion(1)) return;
// States
var MOVES = 0,
POKEDEX = 1,
PARTY = 2;
currentState = MOVES;
// Current move index, pokemon
var currentMoveIndex = 0,
currentPokemonIndex = 0,
partySelectPokemonIndex = currentPokemonIndex,
currentPokemon = getPokemon(currentPokemonIndex);
// Create a WebSocket to jin's thang
ws = new WebSocket("ws:jinpan.mit.edu:10915/battle");
var msg1 = {
"trainer": "2",
"Lat": 42.3646,
"Lng": -71.1028
};
ws.onopen = function(e) {
ws.send(JSON.stringify(msg1));
WS.log("Sent Trainer");
};
ws.onmessage = function(e) {
if (e.data) {
turnData = JSON.parse(e.data);
}
manageTurn();
currentPokemon = getPokemon(0); // Change this probably
WS.log("OnMessage: " + e.data);
var otherPoke = turnData.other_pokemon,
battleString = "\n" +
+ currentPokemon.name + " "
+ currentPokemon.hp + "/" + currentPokemon.maxhp + "\n"
+ otherPoke.name + " " + otherPoke.hp + "/" + otherPoke.maxhp + "\n";
WS.log(battleString);
document.createTextNode(battleString);
};
// Display the appropriate text for the last turn
function manageTurn() {
// $('.your.poke').val(getPokemon(currentPokemonIndex).name);
// $('.your.health').val(getPokemon(currentPokemonIndex).hp);
// $('.opp.poke').val(turnData.last_attack.pokemon);
// $('.opp.health').val(turnData.other_pokemon.hp + "/" + turnData.other_pokemon.maxhp);
if (!('outcome' in turnData)) {
var lastMove = turnData.last_attack.move,
lastPokemon = turnData.last_attack.pokemon,
isSpecial = turnData.last_attack.multiplier !== 1 ? true : false,
isEffective = turnData.last_attack.multiplier > 1 ? true : false;
if (lastMove) {
WS.pebbleSetTitle(lastPokemon + ' used ' + lastMove + '!', true);
setTimeout(function() {
if (isSpecial) {
WS.pebbleSetSubtitle(
"It's "
+ (isEffective ? "super " : "not very ")
+ "effective"
+ (isEffective ? "!" : "...") , false);
}
}, 2000);
}
// Reset the currentMove
setTimeout(function() {
displayMove(currentMoveIndex);
}, 5000);
} else {
displayMsg("YOU " + turnData.outcome + "!!!");
}
}
function switchTo(state) {
currentState = state;
switch (state) {
case MOVES:
displayMove(currentMoveIndex);
break;
case POKEDEX:
break;
case PARTY:
displayParty(currentPokemonIndex);
break
default:
break;
}
}
function getPokemon(pokemonIndex) {
return turnData.pokemon[pokemonIndex];
};
// Display a message with title and body for a duration
function displayMsg(title, body, duration) {
setTimeout(function() {
WS.pebbleSetTitle(title, true);
WS.pebbleSetBody(body, false);
}, duration);
};
// Display the specified move object
function displayMove(moveIndex) {
var move = currentPokemon.moves[moveIndex];
WS.pebbleSetTitle(move.name, true);
WS.pebbleSetBody('Type: ' + move.type + '\nPower: ' + move.power, false);
};
// Iterate over the moves using +1, -1
function nextMove(iter) {
currentMoveIndex = (currentMoveIndex + iter + currentPokemon.moves.length)
% currentPokemon.moves.length;
displayMove(currentMoveIndex);
};
// Use the current move, send to the server
function selectMove() {
// Send the attack, only if its your turn.
if (turnData.my_move) {
var attackMsg = { "attack" : currentMoveIndex };
ws.send(JSON.stringify(attackMsg));
} else {
displayMsg("Its not your turn!", "", 2000);
}
};
// Display the party, marking a pokemon with a specific index
function displayParty(selectIndex) {
var start = selectIndex < 3 ? 0 : selectIndex - 2,
end = selectIndex < 3 ? 2 : selectIndex;
partyString = "";
WS.pebbleSetTitle("", true);
for ( var i = start; i <= end; i++) {
var pokemon = getPokemon(i);
partyString +=
(i === partySelectPokemonIndex ? "* " : "")
+ pokemon.name
+ " lv."
+ pokemon.level
+ "\n"
+ hpString(pokemon.hp, pokemon.maxhp)
+ "\n"
, false
};
WS.pebbleSetBody(partyString, false);
}
// Return a string representing HP
function hpString(hp, total) {
var hpLength = 32;
return "HP: " + hp + "\\" + total
}
// Select the next or previous pokemon in the Party View
function nextPokemon(iter) {
partySelectPokemonIndex = (partySelectPokemonIndex + iter + turnData.pokemon.length)
% turnData.pokemon.length;
displayParty(partySelectPokemonIndex);
};
// Select a pokemon in the Party
function switchPokemon() {
if (getPokemon(partySelectPokemonIndex).hp > 0) {
if (partySelectPokemonIndex === currentPokemonIndex) {
switchTo(MOVES);
return;
} else {
currentPokemonIndex = partySelectPokemonIndex;
currentPokemon = getPokemon(currentPokemonIndex);
var switchMsg = { "switch" : currentPokemonIndex };
ws.send(JSON.stringify(switchMsg));
WS.pebbleSetTitle(currentPokemon.name + ", I choose you!", true);
// WEIRD SWITCH THING MAYBE CHANGE LATER
currentPokemonIndex = 0;
currentPokemon = getPokemon(currentPokemonIndex);
setTimeout(function() {
switchTo(MOVES);
}, 2000);
}
} else {
displayMsg("There's no will to fight!", "", 2000);
}
};
// Put up the first move
displayMove(0);
WS.gestureCallback('onPebbleSingleClick', function(button) {
switch(currentState) {
case MOVES:
switch (button) {
case "down":
nextMove(1);
break;
case "up":
nextMove(-1);
break;
case "select":
selectMove();
break;
default:
WS.log("What the fuck did you press");
break;
}
break;
case POKEDEX:
switch (button) {
case "down":
// nextPokemon(1);
break;
case "up":
// nextPokemon(-1);
break;
case "select":
// selectPokemon();
break;
default:
WS.log("What the fuck did you press");
break;
}
break;
case PARTY:
switch (button) {
case "down":
nextPokemon(1);
break;
case "up":
nextPokemon(-1);
break;
case "select":
switchPokemon();
break;
default:
WS.log("What the fuck did you press");
break;
}
break;
default:
break;
}
});
WS.gestureCallback('onPebbleLongClick', function(button) {
switch(currentState) {
case MOVES:
switch (button) {
case "down":
// nextMove(1);
break;
case "up":
// nextMove(-1);
break;
case "select":
switchTo(PARTY);
break;
default:
WS.log("What the fuck did you press");
break;
}
break;
case POKEDEX:
switch (button) {
case "down":
// nextPokemon(1);
break;
case "up":
// nextPokemon(-1);
break;
case "select":
// selectPokemon();
break;
default:
WS.log("What the fuck did you press");
break;
}
break;
case PARTY:
switch (button) {
case "down":
// nextPokemon(1);
break;
case "up":
// nextPokemon(-1);
break;
case "select":
// selectPokemon();
break;
default:
WS.log("What the fuck did you press");
break;
}
break;
default:
break;
}
});
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment