Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Keeping Track of Time (24 Hour System)  (Read 9354 times)
0 Members and 1 Guest are viewing this topic.
Offline Rocketalypse

Senior Newbie




Game Dev from Croatia


« Posted 2014-09-03 22:21:55 »

I'm creating a game that needs to keep track of time. I have a Time class which has a few methods -- constructor, update(), getDay(), getHour() and getMinute(). My Time class also has a speed variable which determines how fast minutes and hours pass. I would know how to implement day counting once I know how to actually keep track of time in hours and minutes. I want to create a time system like The Sims franchise did. How would I go about this? Could someone point me in the right direction?

Thank you in advance!


EDIT #1 - Added Time class
I don't know why formatting is messed up, but whatever.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
package com.bravesites.ivanvinski.game;

public class Time{
    public static final float SPEED_NORMAL = 1f;
    public static final float SPEED_FASTER = 5f;
    public static final float SPEED_FAST = 10f;
    public static final float SPEED_EXTREME = 20f;
   
    private float speed;
    private int hours, minutes;
   
    private int daysPassed;
    private int dayIndex;
   
    public Time(){
   this(SPEED_NORMAL, 12, 0);
    }
   
    public Time(float speed, int hours, int minutes){
   setSpeed(speed);
   this.hours = hours;
   this.minutes = minutes;
    }
   
    public void update(float delta){
   if(minutes >= 60){
       hours++;
       minutes = 0;
   }
   
   if(hours > 24){
       daysPassed++;
       dayIndex++;
       if(dayIndex > 7)
      dayIndex = 1;
   }
   
   // How would I go about updating the minutes variable?
    }
   
    public static String getNameOfDay(int dayIndex){
   if(dayIndex == 2)
       return "Tuesday";
   if(dayIndex == 3)
       return "Wednesday";
   if(dayIndex == 4)
       return "Thursday";
   if(dayIndex == 5)
       return "Friday";
   if(dayIndex == 6)
       return "Saturday";
   if(dayIndex == 7)
       return "Sunday";
   
   return "Monday";
    }
   
    public float getSpeed(){
   return speed;
    }
   
    public void setSpeed(float speed){
   this.speed = speed;
    }
   
    public int getHours(){
   return hours;
    }
   
    public int getMinutes(){
   return hours;
    }
}
Offline trollwarrior1
« Reply #1 - Posted 2014-09-04 04:59:59 »

This is up to you to decide..

I would make minutes variable a float instead of int and do this in the update method.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public void update(float delta){

   float deltaMinutes = delta / 60; // assuming delta is in seconds

   minutes += deltaMinutes * speed;

   while(minutes >= 60) {
       hours++;
       minutes -= 60;
   }
   
   if(hours > 24){
       daysPassed++;
       dayIndex++;
       if(dayIndex > 7)
      dayIndex = 1;
   }
   
   // How would I go about updating the minutes variable?
    }
Offline Rocketalypse

Senior Newbie




Game Dev from Croatia


« Reply #2 - Posted 2014-09-04 18:15:32 »

Thank you! That's exactly what I needed.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2014-09-04 19:01:19 »

You only need to have time stored in 1 variable, which has millisecond precision.
1  
2  
3  
4  
5  
sec = millis / 1000 % 60;
min = millis / 1000 / 60 % 60;
hrs = millis / 1000 / 60 / 60 % 24;
day = millis / 1000 / 60 / 60 / 24;
dow = day % 7 + 1;


You can now perform arbitrary operations on 'millis' without having to keep track of overflows and underflows of all your variables.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (581 views)
2019-09-04 15:33:17

hadezbladez (5510 views)
2018-11-16 13:46:03

hadezbladez (2402 views)
2018-11-16 13:41:33

hadezbladez (5772 views)
2018-11-16 13:35:35

hadezbladez (1223 views)
2018-11-16 13:32:03

EgonOlsen (4661 views)
2018-06-10 19:43:48

EgonOlsen (5682 views)
2018-06-10 19:43:44

EgonOlsen (3198 views)
2018-06-10 19:43:20

DesertCoockie (4095 views)
2018-05-13 18:23:11

nelsongames (5115 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!