JS Function Every 100 Points - unity3d

I tried writing this on my own, but I failed miserably. Right now I have a script checking the score in my game. I check for every 100 points and change a variable depending on the score, but I'm doing it the wrong and tedious way:
if(gameController.speed == 1) {
if(score >= 200) {
squishyController.gravity = -25;
}
if(score >= 300) {
squishyController.gravity = -50;
}
if(score >= 400) {
squishyController.gravity = -75;
}
if(score >= 500) {
squishyController.gravity = -100;
gamePause = true;
// Spawn First Boss
}
if(score >= 600) {
squishyController.gravity = -125;
}
if(score >= 700) {
squishyController.gravity = -150;
}
}
You get the point. What I want to do in word form is this:
For every 100 points
Change gravity by -25
if the points are 500, 1000, etc
spawn boss
That's it in a nutshell. Here's what I'm thinking it would look like but don't kill me.
for (var i = 0; i += pointValue) {
// Check if points are being added then add to score depending on point value
if(getPoints == true) {
i++;
}
// For every 100 points change the gravity value
if(i >= incrementsOf100) {
gravity = gravity -= 25;
}
// For every 500 points, spawn a boss more difficult than the last
if(i = incrementsOf500) {
bossDifficulty = bossDifficulty += 100; // Just adding this because I will add more hitpoints to boss.
spawnBoss = true;
}
}

How about using the modus operator to find the remainder of dividing by 100, then divide the score minus that remainder to get the number of times you need to change gravity
var score = 550;
var extra = score % 100;
var gravityTimes = (score - extra) / 100;
gravity = gravity -= (25 * gravityTimes);

Related

Can this stock graph be made using Highstock?

Could this graph be made using Highstock?
I'm currently using Highstock, but never saw this kind.
Before I waste weeks trying, the question needs to be asked...
If yes, how can this be made? by shifting the stock chart, and create another Xaxis at the right?
Stock projection
According to the comments - a similar result could be achieved by adding additional series with proper data.
Demo: https://jsfiddle.net/BlackLabel/mkpj71n4/
chart: {
events: {
load() {
let chart = this;
function addNewSeries(y, color) {
let data = [],
xValue = startDay,
yData = 420; // last candle
for (let i = 0; i < 20; i++) {
xValue = xValue + 30 * 60 * 1000;
yData = yData + i * y
data.push([xValue, yData])
}
chart.addSeries({
data: data,
color: color
})
}
for (let i = 0; i < 4; i++) {
if (i === 0) {
addNewSeries(0.1, 'orange')
} else if (i === 2) {
addNewSeries(-0.1, 'orange')
} else if (i === 3) {
addNewSeries(0.2, 'red')
} else {
addNewSeries(-0.2, 'red')
}
}
}
}
},
API: https://api.highcharts.com/highcharts/chart.events.load
API: https://api.highcharts.com/class-reference/Highcharts.Chart#addSeries

How to Convert JS Password Strength Meter to Swift

I'm trying to convert this javascript password score function to swift, so I can use the same logic on mobile as we do on web. Here is the JS fiddle with the code to test:
function scorePassword(pass) {
var score = 0;
if (!pass)
return score;
// award every unique letter until 5 repetitions
var letters = new Object();
for (var i=0; i<pass.length; i++) {
letters[pass[i]] = (letters[pass[i]] || 0) + 1;
score += 5.0 / letters[pass[i]];
}
// bonus points for mixing it up
var variations = {
digits: /\d/.test(pass),
lower: /[a-z]/.test(pass),
upper: /[A-Z]/.test(pass),
nonWords: /\W/.test(pass),
}
variationCount = 0;
for (var check in variations) {
variationCount += (variations[check] == true) ? 1 : 0;
}
score += (variationCount - 1) * 10;
return parseInt(score);
}
function checkPassStrength(pass) {
var score = scorePassword(pass);
if (score > 80)
return "Strong";
if (score > 60)
return "Good";
if (score >= 30)
return "Weak";
return "";
}
I'm having an issue in converting this part:
var letters = new Object();
for (var i=0; i<pass.length; i++) {
letters[pass[i]] = (letters[pass[i]] || 0) + 1;
score += 5.0 / letters[pass[i]];
}
I've tried to create a dictionary to store the character as key and then increment the value with each occurrences of that key, but it's not working as expected.
let string = "Testing22!"
var score = 0
var dictionary = [String.Element: Int]()
for x in string {
let value = dictionary[x] ?? 0
dictionary[x] = value + 1
score += 5 / value
}
print(dictionary) //["s": 1, "T": 1, "g": 1, "2": 2, "n": 1, "i": 1, "!": 1, "t": 1, "e": 1]
print(score)
I'm also not sure of the most efficient way to handle the regex checks for the bonus points section.
I'd port it over to swift like this, I'm sure there are some improvements to be made, but thats a quick conversion:
func scorePassword(_ inputString: String?) -> Double {
var score: Double = 0
guard let string = inputString, !string.isEmpty else { return score }
// award every unique letter until 5 repetitions
let countedSet = NSCountedSet()
for x in string {
countedSet.add(x)
score += 5.0 / Double(countedSet.count(for: x))
}
// bonus points for mixing it up
let variations = [
"^(?=.*[0-9]).*$",
"^(?=.*[a-z]).*$",
"^(?=.*[A-Z]).*$",
"^(?=.*\\W).*$"
]
var variationCount: Double = 0
for check in variations {
print(string.testRegex(check))
variationCount += string.testRegex(check) ? 1 : 0
}
score += (variationCount - 1) * 10
return floor(score)
}
func checkPassStrength(_ inputString: String?) -> String {
let score = scorePassword(inputString)
if score > 80 {
return "Strong"
} else if score > 60 {
return "Good"
} else if score > 30 {
return "Weak"
}
return ""
}
extension String {
func testRegex(_ regex: String) -> Bool {
let test = NSPredicate(format: "SELF MATCHES %#", regex)
return test.evaluate(with: self)
}
}
You can run js code inside Swift and get the result from it, so you can share code between platforms.
let jsSource = """
function scorePassword(pass) {
var score = 0;
if (!pass)
return score;
// award every unique letter until 5 repetitions
var letters = new Object();
for (var i=0; i<pass.length; i++) {
letters[pass[i]] = (letters[pass[i]] || 0) + 1;
score += 5.0 / letters[pass[i]];
}
// bonus points for mixing it up
var variations = {
digits: /d/.test(pass),
lower: /[a-z]/.test(pass),
upper: /[A-Z]/.test(pass),
nonWords: /W/.test(pass),
}
variationCount = 0;
for (var check in variations) {
variationCount += (variations[check] == true) ? 1 : 0;
}
score += (variationCount - 1) * 10;
return parseInt(score);
}
function checkPassStrength(pass) {
var score = scorePassword(pass);
if (score > 80)
return "Strong";
if (score > 60)
return "Good";
if (score >= 30)
return "Weak";
return "";
}
"""
let context = JSContext()
context?.evaluateScript(jsSource)
let testFunction = context?.objectForKeyedSubscript("scorePassword")
let result = testFunction?.call(withArguments: ["1234"])
print("js result : " , result )
Note; I edited the part "digits: /\d/.test(pass)" to "digits: /d/.test(pass)"

Kinect 2 with unity3d speed keep on when the player stopped but keep one knee up

I am making a running game that detect the player knee movement and add speed , its working fine but the problem is when I stop running and keep one knee up the game speed do not stop ,I need the speed to be decreased when I stop running even if one knee is still.
if (!player1)
{
player1 = GameObject.Find("player1");
player1State.text = "";
}
if (player1)
{
player1FootRight = player1.transform.Find("KneeRight").gameObject;
player1FootLeft = player1.transform.Find("KneeLeft").gameObject;
player1State.text = "OK";
player1State.color = Color.white;
newPosP1 = player1FootRight.transform.position.y;
oldPosP1 = player1FootLeft.transform.position.y;
if (startRunning) {
if (foot1 && newPosP1 > oldPosP1)
{
float k = newPosP1 - oldPosP1;
if (k > 1 && speed < 100)
{
speed = speed + 7;
if(leftPath.speed < 15)
{
leftPath.speed = leftPath.speed + 3;
}
footOld = foot1;
foot1 = !foot1;
}
else
{
if (speed > 0)
{
speed = speed - 1;
if (leftPath.speed > 0)
{
leftPath.speed = leftPath.speed - 1;
}
}
if (speed < 0)
{
speed = 0;
}
if (leftPath.speed < 0)
{
leftPath.speed = 0;
}
}
}
if (!foot1 && newPosP1 < oldPosP1)
{
float k = oldPosP1 - newPosP1;
if (k > 1 && speed < 100)
{
speed = speed + 7 ;
if (leftPath.speed < 15)
{
leftPath.speed = leftPath.speed + 3;
}
footOld = foot1;
foot1 = !foot1;
}
else
{
if (speed > 0)
{
speed = speed - 1;
if (leftPath.speed > 0)
{
leftPath.speed = leftPath.speed - 1;
}
}
if (speed < 0)
{
speed = 0;
}
if (leftPath.speed < 0)
{
leftPath.speed = 0;
}
}
}

p5.js Pong game

I am really sorry for this stupid question but i don't know what i should do.
I tried so many things nothing works.
I try to calculate the distance between playerx and the width of playerx.
Can someone just correct my code so i can understand it please try not to explain it.
var playerx = 0;
var z = 0;
var playery = 750;
var ball = {
x: 400,
y: 400,
speedx: 2,
speedy: 3,
};
function setup() {
createCanvas(800,800);}
function draw(){
background(0);
ball1();
player();
click();
wall();
bounce();
hit();
}
function hit(){
var AA = dist(playerx,playery,player.x + 200,playery)
var BB = dist(ball.x,ball.y,AA,).
if (BB <= 20){
ball.speedy = -7;
}
}
function ball1(){
fill(255);
rect(ball.x,ball.y,20,20);
}
function bounce(){
ball.x += ball.speedx;
ball.y += ball.speedy;
if (ball.x>800){
ball.speedx = -2;
}else if (ball.x<0){
ball.speedx = 3;
}else if (ball.y>800){
ball.speedy = -3;
} else if(ball.y<0){
ball.speedy = 2;
}
}
function player(){
fill(255);
rect(playerx,playery,200,20);
}
function click(){
if(keyCode === RIGHT_ARROW) {
playerx += z;
z = 3;
} else if (keyCode === LEFT_ARROW){
playerx += z;
z = -3;
}
}
function wall(){
if (playerx > 600){
playerx = 600;
} else if (playerx < 1){
playerx = 1;
}
}
check this library, it contains the code for collision detection:
https://github.com/bmoren/p5.collide2D
var playerx = 400;
var z = 0;
var playery = 750;
var ball = {
x: 400,
y: 400,
speedx: 2,
speedy: 3,
};
function setup() {
createCanvas(800, 800);
}
function draw() {
background(0);
ball1();
player();
click();
wall();
bounce();
hit();
}
function hit() {
if(checkCollision(playerx, playery, 200, 20, ball.x, ball.y, 20)){
ball.speedy = -7;
console.log("colliding");
}
}
function ball1() {
fill(255);
ellipse(ball.x, ball.y, 20, 20);
}
function bounce() {
ball.x += ball.speedx;
ball.y += ball.speedy;
if (ball.x > 800) {
ball.speedx = -2;
} else if (ball.x < 0) {
ball.speedx = 3;
} else if (ball.y > 800) {
ball.speedy = -3;
} else if (ball.y < 0) {
ball.speedy = 2;
}
}
function player() {
fill(255);
rect(playerx, playery, 200, 20);
}
function click() {
if (keyCode === RIGHT_ARROW) {
playerx += z;
z = 3;
} else if (keyCode === LEFT_ARROW) {
playerx += z;
z = -3;
}
}
function wall() {
if (playerx > 600) {
playerx = 600;
} else if (playerx < 1) {
playerx = 1;
}
}
function checkCollision(rx, ry, rw, rh, cx, cy, diameter) {
//2d
// temporary variables to set edges for testing
var testX = cx;
var testY = cy;
// which edge is closest?
if (cx < rx){ testX = rx // left edge
}else if (cx > rx+rw){ testX = rx+rw } // right edge
if (cy < ry){ testY = ry // top edge
}else if (cy > ry+rh){ testY = ry+rh } // bottom edge
// // get distance from closest edges
var distance = dist(cx,cy,testX,testY)
// if the distance is less than the radius, collision!
if (distance <= diameter/2) {
return true;
}
return false;
};

Score isn't showing in guiText

I have guiTexts called scoreLevel1, scoreLevel2 where i want to show the score from the right levels but it isn't showing any text or scores... There are no build errors. How do I make it show up as expected?
var Score : int;
function Update () {
Score -= 1 * Time.deltaTime;
guiText.text = "Score: "+Score;
TimerOfDeath();
}
HighScores.js:
//run at start if score doesn't exist yet to initialise playerPref
function Start(){
if(!PlayerPrefs.HasKey(Application.loadedLevelName+"HighScore"))
PlayerPrefs.SetFloat(Application.loadedLevelName+"HighScore", 0);
}
//run when level is completed
function OnTriggerEnter(other : Collider){
if(other.tag == "Player"){
Score = gameObject.Find("ScoreCount").GetComponent("ScoringPts").Update("Score");
if(Score > PlayerPrefs.GetFloat(Application.loadedLevelName+"HighScore"))
{
PlayerPrefs.SetFloat(Application.loadedLevelName+"HighScore", Score);
}
}
}
GetHighScores.js:
#pragma strict
function Start () {
var hscount = 1;
var iterations = 1;
var maxIterations = 5;
var findtext = gameObject.Find("scoreLevel"+(hscount));
while(hscount < 5 && iterations < maxIterations){
if(!PlayerPrefs.HasKey("Level"+(hscount)+"HighScore")){
findtext.guiText.text = "Level"+(hscount)+ ": " + PlayerPrefs.GetFloat("Level"+(hscount)+"HighScore");
hscount++;
}
iterations++;
}
}