Merge pull request #2788 from FalseIncarnate/arcade

Claw Games! (HTML5 Arcade)
This commit is contained in:
Fox McCloud
2015-12-10 01:07:59 -05:00
15 changed files with 635 additions and 3 deletions
+132
View File
@@ -0,0 +1,132 @@
/obj/machinery/arcade
name = "Arcade Game"
desc = "One of the most generic arcade games ever."
icon = 'icons/obj/arcade.dmi'
icon_state = "clawmachine_on"
density = 1
anchored = 1
use_power = 1
idle_power_usage = 40
var/tokens = 0
var/freeplay = 0 //for debugging and admin kindness
var/token_price = 0
var/playing = 0 //only has one set of controls, so only one person can play at once
var/last_winner = null //for letting people who to hunt down and steal prizes from
/obj/machinery/arcade/New()
..()
var/choice = pick(subtypesof(/obj/machinery/arcade))
new choice(loc)
qdel(src)
/obj/machinery/arcade/examine(mob/user)
..(user)
if(freeplay)
user << "Someone enabled freeplay on this machine!"
else
if(token_price)
user << "\The [src.name] costs [token_price] credits per play."
if(!tokens)
user << "\The [src.name] has no available play credits. Better feed the machine!"
else if(tokens == 1)
user << "\The [src.name] has only 1 play credit left!"
else
user << "\The [src.name] has [tokens] play credits!"
/obj/machinery/arcade/interact(mob/user as mob)
if(stat & BROKEN || panel_open)
return 0
if(!tokens && !freeplay)
user << "\The [src.name] doesn't have enough credits to play! Pay first!"
return 0
if(!playing && (tokens || freeplay))
user.set_machine(src)
playing = 1
if(!freeplay)
tokens -= 1
return 1
if(playing && (src != user.machine))
user << "Someone else is already playing this machine, please wait your turn!"
return 0
return 1
/obj/machinery/arcade/attackby(var/obj/item/O as obj, var/mob/user as mob, params)
if(istype(O, /obj/item/weapon/screwdriver) && anchored)
playsound(src.loc, 'sound/items/Screwdriver.ogg', 50, 1)
panel_open = !panel_open
user << "You [panel_open ? "open" : "close"] the maintenance panel."
update_icon()
return
if(!freeplay)
if(istype(O, /obj/item/weapon/card/id))
var/obj/item/weapon/card/id/C = O
if(pay_with_card(C))
tokens += 1
return
else if(istype(O, /obj/item/weapon/spacecash))
var/obj/item/weapon/spacecash/C = O
if(pay_with_cash(C, user))
tokens += 1
return
if(panel_open&& component_parts && istype(O, /obj/item/weapon/crowbar))
default_deconstruction_crowbar(O)
/obj/machinery/arcade/update_icon()
return
/obj/machinery/arcade/proc/pay_with_cash(var/obj/item/weapon/spacecash/cashmoney, var/mob/user)
if(cashmoney.get_total() < token_price)
user << "\icon[cashmoney] <span class='warning'>That is not enough money.</span>"
return 0
visible_message("<span class='info'>[usr] inserts a credit chip into [src].</span>")
var/left = cashmoney.get_total() - token_price
user.unEquip(cashmoney)
qdel(cashmoney)
if(left)
dispense_cash(left, src.loc, user)
return 1
/obj/machinery/arcade/proc/pay_with_card(var/obj/item/weapon/card/id/I, var/mob/user)
visible_message("<span class='info'>[usr] swipes a card through [src].</span>")
var/datum/money_account/customer_account = attempt_account_access_nosec(I.associated_account_number)
if (!customer_account)
user <<"Error: Unable to access account. Please contact technical support if problem persists."
return 0
if(customer_account.suspended)
user << "Unable to access account: account suspended."
return 0
// Have the customer punch in the PIN before checking if there's enough money. Prevents people from figuring out acct is
// empty at high security levels
if(customer_account.security_level != 0) //If card requires pin authentication (ie seclevel 1 or 2)
var/attempt_pin = input("Enter pin code", "Vendor transaction") as num
customer_account = attempt_account_access(I.associated_account_number, attempt_pin, 2)
if(!customer_account)
user << "Unable to access account: incorrect credentials."
return 0
if(token_price > customer_account.money)
user << "Insufficient funds in account."
return 0
else
// Okay to move the money at this point
// debit money from the purchaser's account
customer_account.money -= token_price
// create entry in the purchaser's account log
var/datum/transaction/T = new()
T.target_name = "[src.name]"
T.purpose = "Purchase of [src.name] credit"
if(token_price > 0)
T.amount = "([token_price])"
else
T.amount = "[token_price]"
T.source_terminal = src.name
T.date = current_date_string
T.time = worldtime2text()
customer_account.transaction_log.Add(T)
return 1
+24
View File
@@ -0,0 +1,24 @@
/obj/item/toy/prizeball
name = "prize ball"
desc = "A toy is a toy, but a prize ball could be anything! It could even be a toy!"
icon = 'icons/obj/arcade.dmi'
icon_state = "prizeball_1"
var/opening = 0
/obj/item/toy/prizeball/New()
..()
icon_state = pick("prizeball_1","prizeball_2","prizeball_3")
/obj/item/toy/prizeball/attack_self(mob/user as mob)
if(opening)
return
opening = 1
playsound(src.loc, 'sound/items/bubblewrap.ogg', 30, 1, extrarange = -4, falloff = 10)
icon_state = "prizeconfetti"
src.color = pick(random_color_list)
var/prize_inside = pick(/obj/random/carp_plushie, /obj/random/plushie, /obj/random/figure) //will add ticket bundles later
spawn(10)
user.unEquip(src)
new prize_inside(user.loc)
qdel(src)
+84
View File
@@ -0,0 +1,84 @@
/var/claw_game_html = null
/obj/machinery/arcade/claw
name = "Claw Game"
desc = "One of the most infuriating ways to win a toy."
icon = 'icons/obj/arcade.dmi'
icon_state = "clawmachine_1_on"
token_price = 15
var/machine_image = "_1"
var/bonus_prize_chance = 5 //chance to dispense a SECOND prize if you win, increased by matter bin rating
//This is to make sure the images are available
var/list/img_resources = list('icons/obj/arcade_images/backgroundsprite.png',
'icons/obj/arcade_images/clawpieces.png',
'icons/obj/arcade_images/crane_bot.png',
'icons/obj/arcade_images/crane_top.png',
'icons/obj/arcade_images/prize_inside.png',
'icons/obj/arcade_images/prizeorbs.png')
/obj/machinery/arcade/claw/New()
machine_image = pick("_1", "_2")
update_icon()
component_parts = list()
component_parts += new /obj/item/weapon/circuitboard/clawgame(null)
component_parts += new /obj/item/weapon/stock_parts/matter_bin(null)
component_parts += new /obj/item/weapon/stock_parts/manipulator(null)
component_parts += new /obj/item/stack/cable_coil(null, 5)
component_parts += new /obj/item/stack/sheet/glass(null, 1)
RefreshParts()
if(!claw_game_html)
claw_game_html = file2text('code/modules/arcade/crane.html')
claw_game_html = replacetext(claw_game_html, "/* ref src */", "\ref[src]")
/obj/machinery/arcade/claw/RefreshParts()
var/bin_upgrades = 0
for(var/obj/item/weapon/stock_parts/matter_bin/B in component_parts)
bin_upgrades = B.rating
bonus_prize_chance = bin_upgrades * 5 //equals +5% chance per matter bin rating level (+20% with rating 4)
/obj/machinery/arcade/claw/update_icon()
if(stat & BROKEN)
icon_state = "clawmachine[machine_image]_broken"
else if(panel_open)
icon_state = "clawmachine[machine_image]_open"
else if(stat & NOPOWER)
icon_state = "clawmachine[machine_image]_off"
else
icon_state = "clawmachine[machine_image]_on"
return
/obj/machinery/arcade/claw/proc/win()
icon_state = "clawmachine[machine_image]_win"
visible_message("<span class='game say'><span class='name'>[src.name]</span> beeps, \"WINNER!\"</span>")
if(prob(bonus_prize_chance))
//double prize mania!
new /obj/item/toy/prizeball(get_turf(src))
new /obj/item/toy/prizeball(get_turf(src))
playsound(src.loc, 'sound/arcade/Win.ogg', 50, 1, extrarange = -3, falloff = 10)
spawn(10)
update_icon()
/obj/machinery/arcade/claw/attack_hand(mob/user as mob)
interact(user)
/obj/machinery/arcade/claw/interact(mob/user as mob)
if(!..())
return
user << browse_rsc('page.css')
for(var/i=1, i<=img_resources.len, i++)
user << browse_rsc(img_resources[i])
user << browse(claw_game_html, "window=arcade;size=700x600;can_close=0")
return
/obj/machinery/arcade/claw/Topic(href, list/href_list)
if(..())
return
var/prize_won = null
prize_won = href_list["prizeWon"]
if(!isnull(prize_won))
usr.unset_machine()
playing = 0
usr << browse(null, "window=arcade") //just in case
if(prize_won == "1")
win()
+284
View File
@@ -0,0 +1,284 @@
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Crane game</title>
<link rel="stylesheet" type="text/css" href="page.css" />
<script src="libraries.min.js"></script>
<!-- Prize.js --><script>
function Prize(id,css, crane) {
var top = css['top'];
var left = css['left'];
var original_left = css['left'];
hspd = 15;
vspd = 5;
var state = 'falling';
var error;
var rand = Math.floor(Math.random()*10);
css['background'] = 'url(prize_inside.png) no-repeat center';
var pic = $('<div></div>').attr({'id':'prize'+id,class:'prize-ball'}).css(css);
var ball = $('<div></div>').css({height: 60,'background':'url(prizeorbs.png) no-repeat -4px -'+62*rand+'px'});
$(pic).append(ball);
$('#game').append(pic);
//console.log(crane);
this.GetState = function() {return state};
var CheckBoundaries = function () {
if(left < 52)
left = 52;
else if(left > 812)
left = 812;
if(top < 30)
top = 30;
else if(top > 347 && state !='won' && state !='hidden') {
top = 347;
state = 'resting';
} else if(top > 500 && state =='won') {
top=500;
}
};
var CheckGrabbed = function() {
var tmp = Math.floor(Math.random()*100);
if(tmp>error) {
setTimeout(function(){
state='falling';
$('#debug-streak').html(0); //reset streak
setTimeout(gameShutDown(),500); //end game because you dropped it
},4*error+300);
}
state = 'is grabbed';
};
var IsGrabbed = function() {
top = crane.GetTop()+65;
if(top > 347)
top = 347;
left = crane.GetLeft()+23;
if(left <= 60) {
left = 60;
state = 'won';
}
};
this.GetError = function(offset) {
return Math.floor(160/(.1*offset+1)-60);
};
this.Update = function () {
if(crane.GetState() == 'down' && state=='resting') {
var offset = Math.abs(left - crane.GetLeft()-23);
error = this.GetError(offset);
if(error > 0) {
state = 'being grabbed';
$('#debug-errorpx').html(Math.abs(offset)+'px');
$('#debug-errordrop').html(error+'%');//debugging
}
}
if(crane.GetState() == 'up' && state=='being grabbed')
CheckGrabbed();
if((crane.GetState() == 'drop' || crane.GetState() == 'up') && state=='is grabbed')
IsGrabbed();
if(state=='falling'||state=='won')
top += vspd;
if(state=='won' && top > 460) {
$('#prize'+id).remove();//css({visibility:'hidden'});
state='hidden';
prizeWon = true;
gameShutDown();
}
CheckBoundaries();
};
this.Repaint = function () {
$('#prize'+id).css({'top':top, 'left':left});
//$('#'+id+' #crane-handle-top').css({height: handleHeight});
};
}</script>
<!-- Crane.js --><script>
function Crane(id) {
//console.log("crane created");
var top = 131; //private
var left = 100;
var hspd = 6;
var vspd = 4;
var state = false;
var handleHeight = 83;
var frames = {
normal: {'background-position': '-36px -34px',left:11},
half: {'background-position': '-36px -148px',left:6},
open: {'background-position': '-28px -270px',left:-2}
};
this.GetState = function() {return state};
this.GetLeft = function() {return left};
this.GetTop = function() {return top};
var Grab = function () {
top += (state == 'down')?vspd:-vspd;
handleHeight += (state == 'down')?vspd:-vspd;
if(top > 300 && state == 'down') { //when going down
top = 300;
//handleHeight = ;
state = 'up';
$('#'+id+' #crane-claw').css(frames['half']);
} else if(top < 131 && state == 'up') { //when going up
top = 131;
handleHeight = 83;
state = 'drop';
} else if(state == 'drop') { //when up and dropping
left -= hspd;
top = 131;
handleHeight = 83;
if(left < 30) {
left = 30;
$('#'+id+' #crane-claw').css(frames['open']);
setTimeout(function(){$('#'+id+' #crane-claw').css(frames['normal']);state = false;},500);
}
}
};
var CheckBoundaries = function () {
if(left < 30)
left = 30;
else if(left > 788)
left = 788;
};
this.Update = function () {
//console.log(left);
if(keys["left"] && !state) //left
left -= hspd;
if(keys["right"] && !state) //right
left += hspd;
if(keys["down"] && !state) { //drop
state = 'down';
$('#'+id+' #crane-claw').css(frames['open']);
}
if(state) {
Grab();
}
CheckBoundaries();
};
this.Repaint = function () {
$('#'+id).css({'top':top, 'left':left});
$('#'+id+' #crane-handle-top').css({height: handleHeight});
};
}</script>
<!-- main.js --><script>
var crane = new Crane('crane');
var keys = [];
var prizes = [];
var prizeWon = false;
window.requestAnimFrame = (function(callback){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
window.setTimeout(callback, 1000 / 30);
};
})();
function animate(){
//console.log("animate called");
crane.Update();
for(var i = 0; i< prizes.length; i++)
prizes[i].Update();
for(var i = 0; i< prizes.length; i++)
prizes[i].Repaint();
crane.Repaint();
requestAnimFrame(function(){
animate();
});
}
function joystickControlOn(direction){
//console.log(direction);
keys[direction] = true;
}
function joystickControlOff(direction){
//console.log(direction);
keys[direction] = false;
}
function gameStartUp(){ //main function
//console.log("game start!");
document.getElementById("play_btn").disabled = true; //to prevent button-mashing the start button.
for(var i = 0; i< 5; i++)
prizes[i] = new Prize(i,{top: Math.ceil(Math.random()*100),left: 400+i*100-Math.ceil(Math.random()*50)},crane);
//console.log("prize made");
//crane.Repaint();
animate();
}
function gameShutDown(){
document.getElementById("play_btn").disabled = true;
var close_game_link = '<h2>TRY AGAIN!</h2><br><a href="byond://?src=/* ref src */;prizeWon=0">\[Close Game\]</a>'
if(prizeWon)
close_game_link = '<h2>YOU WON!</h2><br><a href="byond://?src=/* ref src */;prizeWon=1">\[Close Game\]</a>'
document.getElementById('game').innerHTML = '<center><h1> GAME OVER!</h1><br>'+close_game_link+'</center>'
}</script>
</head>
<body>
<div id='game' style='position: relative;'>
<div id="background"></div>
<div id="crane">
<div id="crane-handle-top"></div>
<div id="crane-handle-bottom"></div>
<div id="crane-claw"></div>
<div id="crane-center"></div>
</div>
<div id="grayorbs-chute"></div>
<div id="foreground"></div>
</div>
<div id='controls' style='position: absolute; top: 550px'>
<br><button id="play_btn" onclick="gameStartUp()">Play Now!</button> <button id="close_btn" onclick="gameShutDown()">Close Game.</button>
<br>
<button onmouseover="joystickControlOn('left')" onmouseout="joystickControlOff('left')">/==<br>\==</button>
<button onmousedown="joystickControlOn('down')" onmouseup="joystickControlOff('down')">DROP<br>CLAW</button>
<button onmouseover="joystickControlOn('right')" onmouseout="joystickControlOff('right')">==\<br>==/</button>
</div>
</body>
</html>
+84
View File
@@ -0,0 +1,84 @@
#background {
width:900px;
height:406px;
position: absolute;
background: url(backgroundsprite.png) no-repeat;
background-position: 0 -166px;
top: 62px;
}
#grayorbs-chute {
width: 902px;
height: 131px;
position: absolute;
background: url(backgroundsprite.png) no-repeat center;
background-position: 3px -15px;
top: 337px;
left: -3px;
}
#foreground {
width:900px;
height:520px;
position: absolute;
background: url(backgroundsprite.png) no-repeat center;
background-position: 0 -575px;
z-index:10;
}
#crane {
width:100px;
height:100px;
position: absolute;
top: 131px;
left: 100px;
}
#crane-handle-top {
width: 10px;
height: 83px;
position: absolute;
background: url('crane_top.png') repeat-y center;
bottom: 86px;/*top: -69px;*/
left: 47px;
}
#crane-handle-bottom {
width:10px;
height:8px;
position: absolute;
background: url('clawpieces.png') no-repeat center;
top: 14px;
left: 47px;
background-position: -198px -83px;
}
#crane-center{
width: 26px;
height: 27px;
position: absolute;
background: url('clawpieces.png') no-repeat center;
top: 22px;
left: 39px;
background-position: -190px -101px;
}
#crane-claw {
width: 110px;
height: 78px;
position: absolute;
background: url('clawpieces.png') no-repeat center;
top: 33px;
left: 11px;
background-position: -36px -34px;
}
.prize-ball {
width: 60px;
height: 60px;
//background: url('prizeorbs.png') no-repeat;
//-moz-border-radius: 30px;
//-webkit-border-radius: 30px;
//border-radius: 30px;
position: absolute;
}
@@ -463,11 +463,21 @@
category = list ("Misc. Machinery")
/datum/design/mining_equipment_vendor
name = "Machine Design (Mining Rewards Vender Board)"
desc = "The circuit board for a Mining Rewards Vender."
name = "Machine Design (Mining Rewards Vendor Board)"
desc = "The circuit board for a Mining Rewards Vendor."
id = "mining_equipment_vendor"
req_tech = list("programming" = 1, "engineering" = 2)
build_type = IMPRINTER
materials = list(MAT_GLASS=1000, "sacid"=20)
build_path = /obj/item/weapon/circuitboard/mining_equipment_vendor
category = list ("Misc. Machinery")
/datum/design/clawgame
name = "Machine Design (Claw Game Board)"
desc = "The circuit board for a Claw Game."
id = "clawgame"
req_tech = list("programming" = 2)
build_type = IMPRINTER
materials = list(MAT_GLASS=1000, "sacid"=20)
build_path = /obj/item/weapon/circuitboard/clawgame
category = list ("Misc. Machinery")