I know that you can change the formatting of Eclipse in Window>Preferences>Java>Code Style>Formatter
But I don't know where to make it put a newline between variables and loops/if-else.
I need this:
isRunning = true;
final double frameTime = 1.0 / FRAME_CAP;
long lastTime = Time.getTime();
double unproccessedTime = 0;
while(isRunning)
{
long startTime = Time.getTime();
long passedTime = startTime - lastTime;
lastTime = startTime;
unproccessedTime += passedTime / (double)Time.SECOND;
while(unproccessedTime > frameTime)
{
unproccessedTime -= frameTime;
if(Window.isCloseRequested())
{
stop();
}
}
render();
}
To become more like this:
isRunning = true;
final double frameTime = 1.0 / FRAME_CAP;
long lastTime = Time.getTime();
double unproccessedTime = 0;
while(isRunning)
{
long startTime = Time.getTime();
long passedTime = startTime - lastTime;
lastTime = startTime;
unproccessedTime += passedTime / (double)Time.SECOND;
while(unproccessedTime > frameTime)
{
unproccessedTime -= frameTime;
if(Window.isCloseRequested())
{
stop();
}
}
render();
}
Can anyone tell me which format setting could perform this?
I usually like to working with clean code too,and was wondering the same thing before, But I use Regex with Find and Replace:
Find: ^.+if
Replace with: \R$0
Find: ^\s*\n
Replace with: (empty)
This was answer I found before that does works as well
How to add a new line BEFORE a line that matches a given pattern?
Related
I am uploading image using Dio. I got the upload progress in percentage but I also want
to get the upload reaming time.
onSendProgress: (int sent, int total) {
uploadProgress.value = "${(sent / total * 100).toStringAsFixed (0)}%";
},
Is there any way to get the reaming time ?
Convert the remaining file size to megabytes.
Convert upload speed to megabytes.
file size / (uploads per second / 8 )
ex) 200M/(10M/8) = 160 seconds
I simulated an upload environment, I believe you can apply it to the Dio environment, good luck.
void test() {
var totalSize = 120000.0;
var uploadSpeed = 0.0;
var timeLeft = 0.0;
var startTime = DateTime.now();
var curTime = DateTime.now();
Timer? timer;
timer = Timer.periodic(Duration(seconds: 1), (t) {
if (t.tick > 9) timer?.cancel();
curTime = DateTime.now();
var currentSize = 1500。0;
var usedTime =
((curTime.difference(startTime).inMilliseconds) / 1000).toInt();
uploadSpeed = (totalSize / usedTime) / 1024;
totalSize = totalSize - currentSize;
timeLeft = totalSize / (currentSize / 8);
print('useTime : $usedTime');
print('uploadSpeed : $uploadSpeed');
print('File Size : $totalSize');
print('uploadSpeed :$uploadSpeed');
print('timeLeft $timeLeft');
});
}
I'm learning Dart & flutter for 2 days and I'm confused about how to convert seconds (for example 1500sec) to minutes.
For studying the new language, I'm making Pomodoro timer, and for test purposes, I want to convert seconds to MM: SS format. So far, I got this code below, but I'm stuck for a couple of hours now... I googled it but could not solve this problem, so I used Stackoverflow. How should I fix the code?
int timeLeftInSec = 1500;
void startOrStop() {
timer = Timer.periodic(Duration(seconds: 1), (timer) {
setState(() {
if (timeLeftInSec > 0) {
timeLeftInSec--;
timeLeft = Duration(minutes: ???, seconds: ???)
} else {
timer.cancel();
}
}
}
}
This code works for me
formatedTime({required int timeInSecond}) {
int sec = time % 60;
int min = (time / 60).floor();
String minute = min.toString().length <= 1 ? "0$min" : "$min";
String second = sec.toString().length <= 1 ? "0$sec" : "$sec";
return "$minute : $second";
}
now just call the function
formatedTime(timeInSecond: 152)
formated time example
Without formatting:
int mins = Duration(seconds: 120).inMinutes; // 2 mins
Formatting:
String formatTime(int seconds) {
return '${(Duration(seconds: seconds))}'.split('.')[0].padLeft(8, '0');
}
void main() {
String time = formatTime(3700); // 01:01:11
}
You can try this :
int minutes = (seconds / 60).truncate();
String minutesStr = (minutes % 60).toString().padLeft(2, '0');
This is the way I've achieved desired format of MM:SS
Duration(seconds: _secondsLeft--).toString().substring(2, 7);
Here is an example of what toString method returns:
d = Duration(days: 0, hours: 1, minutes: 10, microseconds: 500);
d.toString(); // "1:10:00.000500"
So with the substring method chained you can easily achive more formats e.g. HH:MM:SS etc.
Check out this piece of code for a count down timer with formatted output
import 'dart:async';
void main(){
late Timer timer;
int startSeconds = 120; //time limit
String timeToShow = "";
timer = Timer.periodic(Duration(seconds:1 ),(time){
startSeconds = startSeconds-1;
if(startSeconds ==0){
timer.cancel();
}
int minutes = (startSeconds/60).toInt();
int seconds = (startSeconds%60);
timeToShow = minutes.toString().padLeft(2,"0")+"."+seconds.toString().padLeft(2,"0");
print(timeToShow);
});
}
/*output
01.59
01.58
01.57
...
...
...
00.02
00.01
00.00*/
static int timePassedFromNow(String? dateTo) {
if (dateTo != null) {
DateTime targetDateTime = DateTime.parse(dateTo);
DateTime dateTimeNow = DateTime.now();
if (targetDateTime.isAfter(dateTimeNow)) {
Duration differenceInMinutes = dateTimeNow.difference(targetDateTime);
return differenceInMinutes.inSeconds;
}
}
return 0;
}
static String timeLeft(int seconds) {
int diff = seconds;
int days = diff ~/ (24 * 60 * 60);
diff -= days * (24 * 60 * 60);
int hours = diff ~/ (60 * 60);
diff -= hours * (60 * 60);
int minutes = diff ~/ (60);
diff -= minutes * (60);
String result = "${twoDigitNumber(days)}:${twoDigitNumber(hours)}:${twoDigitNumber(minutes)}";
return result;
}
static String twoDigitNumber(int? dateTimeNumber) {
if (dateTimeNumber == null) return "0";
return (dateTimeNumber < 9 ? "0$dateTimeNumber" : dateTimeNumber).toString();
}
I'm using the coroutine below to try to scale a transform from Vector3.zero to Vector3.one over one second (scalingTime). I've determined that the coroutine is definitely being run, but the object is not scaling. Am I using the "yield return null" in the while loop correctly?
IEnumerator ScaleLaser()
{
float elapsedTime = 0;
float scalingTime = 1;
Vector3 currentScale = laser.localScale;
while (elapsedTime < scalingTime)
{
transform.localScale = Vector3.Lerp(currentScale, Vector3.one, elapsedTime / scalingTime);
elapsedTime += Time.deltaTime;
yield return null;
}
}
This should work.
IEnumerator ScaleLaser()
{
float scalingTime = 1;
float time = 0;
while (time < 1)
{
time += Time.deltaTime / scalingTime;
laser.localScale = Vector3.Lerp(laser.localScale, Vector3.one, time);
yield return null;
}
}
I am creating a sort of game with Arduino and Processing. In my code, I use Daniel Shiffman's class Timer, but would like to create two different Timers using two different instances of the class.
My problem is that these two instances seem to be getting mixed up, with each one doing parts of what the other should be doing.
For example, timer should run for 10 seconds and correctTimer should run for 3 seconds, but they both run for 10 seconds. Additionally, when timer is finished, it should set the background to red and when correctTimer is finished, it should set the background to blue. However, both Timers set the background to blue when they are finished.
Does anyone have any ideas of how to fix this?
import processing.serial.*;
int end = 10;
String serial;
Serial port;
float[] array;
// --------------------------------------------------
PImage img;
PImage correct;
PImage incorrect;
float thumb;
float index;
float middle;
float ring;
float pinky;
// --------------------------------------------------
String alphabet;
int randomNum;
String letter;
// --------------------------------------------------
int savedTime;
int totalTime;
int passedTime;
boolean quit = false;
class Timer {
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void start() {
savedTime = millis();
//quit = false;
}
boolean isFinished() {
passedTime = millis() - savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
Timer timer;
Timer correctTimer;
// --------------------------------------------------
boolean checkLetter(String letterPicked, float flexR_THUMB, float flexR_INDEX, float flexR_MIDDLE, float flexR_RING, float flexR_PINKY) {
if (letterPicked == "A") {
if (flexR_THUMB > 12000 && flexR_THUMB < 22000 &&
flexR_INDEX > 27958 && flexR_INDEX < 38500 &&
flexR_MIDDLE > 26035 && flexR_MIDDLE < 41650 &&
flexR_RING > 16492 && flexR_RING < 26000 &&
flexR_PINKY > 37528 && flexR_PINKY < 53500) {
return true;
} else {
return false;
}
}
return false; }
// --------------------------------------------------
void setup() {
size(1280, 950);
background(255);
port = new Serial(this, "/dev/tty.usbmodem1421", 9600);
port.clear();
serial = port.readStringUntil(end);
serial = null;
correct = loadImage("img/RIGHT.png");
incorrect = loadImage("img/WRONG.png");
correctTimer = new Timer(3000);
startOver();
}
// --------------------------------------------------
void startOver() {
background(255);
letter = "A";
img = loadImage("img/" + letter +".png");
image(img, 0, 0, 1280, 950);
timer = new Timer(10000);
timer.start();
}
// --------------------------------------------------
void draw() {
while(port.available() > 0) {
serial = port.readStringUntil(end);
}
if (serial != null) {
float[] array = float(split(serial, ','));
thumb = array[0];
index = array[1];
middle = array[2];
ring = array[3];
pinky = array[4];
}
if (checkLetter(letter, thumb, index, middle, ring, pinky) == true && quit == false) {
image(correct, 0, 0, 1280, 950);
quit = true;
correctTimer.start();
} else if (timer.isFinished() && quit == false) {
background(255, 0, 0);
quit = true;
correctTimer.start();
}
if (correctTimer.isFinished()) {
background(0, 0, 255);
}
}
Please try to post a MCVE instead of your whole project. Just put together a small example that demonstrates the problem. That makes it much easier for us to help you.
But your problem is caused by your savedTime, totalTime, and passedTime variables being outside the Timer class. Basically that means they're shared between all instances of the Timer class. You can use println() statements to confirm this.
To fix your problem, just move those variables inside the Timer class, so each instance has its own copy of them.
If you're still having trouble, please post a MCVE in a new question post, and we'll go from there. Good luck.
I have a script that regenerates player's health after 5 seconds:
float counter;
void Update (){
counter += Time.deltaTime;
if(counter > 30minutes){
curHealth++;
counter = 0.0f; }
if(curHealth > 5)
curHealth = 5;
}
Which is attach to my player's health script. I want to start regenerating my player health back when timer is at 0. But I don't know how.
This is my player health script:
//Stats
public int curHealth;
public int maxHealth = 3;
public Collider2D col;
public PlayerHealth playerhealthRef;
public TimeManager countDownTimer;
float counter;
public Animator anima; // drag the panel in here again
private UI_ManagerScripts UIM;
DateTime currentDate;
DateTime oldDate;
private GenerateEnemy generateEnemy = null;
void Start ()
{
curHealth = maxHealth;
currentDate = System.DateTime.Now;
if (countDownTimer != null)
{
// countDownTimer.gameObject.SetActive(false);
countDownTimer.Enable(false);
}
if (GameObject.Find("Spawner"))
{
generateEnemy = GameObject.Find("Spawner").GetComponent<GenerateEnemy>();
}
}
void Update ()
{
counter += Time.deltaTime;
if (curHealth > maxHealth) {
curHealth = maxHealth;
}
if ((curHealth <= 0) && (!countDownTimer.gameObject.activeSelf)) {
Die ();
}
if(counter > 5)
{
curHealth++;
counter = 0.0f;
}
if(curHealth > 3)
curHealth = 3;
}
void Awake()
{
UIM = GameObject.Find ("UIManager").GetComponent<UI_ManagerScripts> ();
}
void Die() {
UIM.EnableBoolAnimator(anima);
PlayerPrefs.SetInt("RemainingLives", curHealth);
PlayerPrefs.Save();
if (generateEnemy != null)
{
generateEnemy.DestroyAllBlobs();
}
if (countDownTimer != null)
{
countDownTimer.Enable(true);
}
}
public void Damage(int dmg)
{
curHealth -= dmg;
}
}
And this is my Timer countdown script:
public Text timer;
int minutes = 1;
int seconds = 0;
float miliseconds = 0;
[Range(1, 59)]
public int defaultStartMinutes = 1;
public bool allowTimerRestart = false;
public bool useElapsedTime = true;
private int savedSeconds = -1;
private bool resetTimer = false;
private DateTime centuryBegin = new DateTime(2001, 1, 1);
private float tickPerSecond = 10000000.0f;
public void Enable (bool enable)
{
gameObject.SetActive(enable);
ResetTime(); // force the timer to restart
}
void Awake ()
{
minutes = defaultStartMinutes;
if (PlayerPrefs.HasKey("TimeOnExit"))
{
miliseconds = PlayerPrefs.GetFloat("TimeOnExit");
savedSeconds = (int)miliseconds;
if (useElapsedTime && PlayerPrefs.HasKey("CurrentTime"))
{
int elapsedTicks = (int)(DateTime.Now.Ticks / tickPerSecond);
int ct = PlayerPrefs.GetInt("CurrentTime", elapsedTicks);
PlayerPrefs.DeleteKey("CurrentTime");
elapsedTicks -= ct;
if (elapsedTicks < miliseconds)
{
miliseconds -= elapsedTicks;
}
else
{
miliseconds = 0;
}
}
minutes = (int)miliseconds / 60;
miliseconds -= (minutes * 60);
seconds = (int)miliseconds;
miliseconds -= seconds;
PlayerPrefs.DeleteKey("TimeOnExit");
}
savedSeconds = 0;
}
public void Update()
{
// count down in seconds
miliseconds += Time.deltaTime;
if (resetTimer)
{
ResetTime();
}
if (miliseconds >= 1.0f)
{
miliseconds -= 1.0f;
if ((seconds > 0) || (minutes > 0))
{
seconds--;
if (seconds < 0)
{
seconds = 59;
minutes--;
}
}
else
{
resetTimer = allowTimerRestart;
}
}
if (seconds != savedSeconds)
{
// Show current time
timer.text = string.Format("{0}:{1:D2}", minutes, seconds);
savedSeconds = seconds;
}
}
void ResetTime()
{
minutes = defaultStartMinutes;
seconds = 0;
savedSeconds = 0;
miliseconds = 1.0f - Time.deltaTime;
resetTimer = false;
}
private void OnApplicationQuit()
{
int numSeconds = ((minutes * 60) + seconds);
if (numSeconds > 0)
{
miliseconds += numSeconds;
PlayerPrefs.SetFloat("TimeOnExit", miliseconds);
if (useElapsedTime)
{
int elapsedTicks = (int)(DateTime.Now.Ticks / tickPerSecond);
PlayerPrefs.SetInt("CurrentTime", elapsedTicks);
}
}
}
}
Thank you :)
Second edit
I tried doing this in my update file:
if (countDownTimer = true)
{
if(counter > 5)
{
curHealth++;
counter = 0.0f;
}
if(curHealth > 3)
curHealth = 3;
}
But I got this error: error CS0029: Cannot implicitly convert type bool' toTimeManager'.
I just wasn't sure of how to go about doing this.?
According to your second edit line 1
if (countDownTimer = true)
should be
if(countDownTimer)
as = operator is only to give a value, and for comparison you use ==, or, if the variable is bool, simply if(var) or if(!var)
Most of your code is useless.
You can use WaitForSeconds(just google it) or you can add LeanTween to your project and use
LeanTweed.DeayedCall(5f, ()=>{ /*code that will be runned after 5 sec will pass*/});
I prefer to use LeanTween.