Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add random timer range as a feature of timers #396

Closed
eonarheim opened this issue Jan 23, 2015 · 11 comments · Fixed by #2317
Closed

Add random timer range as a feature of timers #396

eonarheim opened this issue Jan 23, 2015 · 11 comments · Fixed by #2317
Assignees
Labels
community in-progress This issue is marked in-progress. This label will be removed if there is no activity for 30 days. enhancement Label applied to enhancements or improvements to existing features good first issue good for people new to open source and Excalibur

Comments

@eonarheim
Copy link
Member

eonarheim commented Jan 23, 2015

Context

There is a use case for creating a timer that has a variable duration. For example, if you wanted to randomly space events during a game.

Proposal

Add the ability to specify a range (min and max) of time and allow the timer to randomly choose a duration within that range.

@eonarheim eonarheim added this to the 0.5 Release milestone Jan 23, 2015
@jedeen jedeen added the enhancement Label applied to enhancements or improvements to existing features label Apr 30, 2015
@jedeen jedeen modified the milestones: 0.7 Release, vNext Apr 30, 2015
@eonarheim eonarheim added the good first issue good for people new to open source and Excalibur label Jan 24, 2019
@yammik
Copy link

yammik commented Apr 27, 2019

Hi! I'd like to work on this if it's still up for grabs 🙏

@eonarheim
Copy link
Member Author

eonarheim commented Apr 28, 2019 via email

@Moonstrous
Copy link

Is this still up for grabs ?

@eonarheim eonarheim added the community in-progress This issue is marked in-progress. This label will be removed if there is no activity for 30 days. label Aug 6, 2019
@eonarheim
Copy link
Member Author

@DProBoy All yours, I've added the in-progress label

@Moonstrous
Copy link

@eonarheim Can i (or Should I?) add new sandbox tests for the Random Timer ? I've committed the changes to my fork of Excalibur and i'm about to submit a pull request for #875 and #396 . (hehe ... still haven't gotten used to Git, as I was a Perforce user before ...)

@kamranayub
Copy link
Member

kamranayub commented Aug 7, 2019 via email

@c3ho
Copy link

c3ho commented Oct 3, 2019

Hi, is this issue still open?

@eonarheim
Copy link
Member Author

@c3ho I haven't seen any work on it, all yours!

Excalibur has a built in seeded ex.Random implementation that we should use to do the random calculations.

It may be worth while to rethink the constructor and the Timer interface as well.

Perhaps something along these lines? If possible we should allow the previous constructor to be used as well avoid a breaking change

export interface TimerOptions {
    // milliseconds before the timer fires
   interval: number;
   // if number of repeats null, undefined means it fires once
   // if number of repeats <= 0 or Infinity means repeat forever
   repeats?: number;
   // maximum number of milliseconds to randomly add
   randomRange?: number; 
   // timer callback
   fcn?: () => void;
}

type Callback = () => void;

export class Timer {

    // Constructor overload
    constructor(options: TimerOptions);
    constructor(fcn: Callback, interval: number, repeats?: boolean, numberOfRepeats?: number);
    constructor(optionsOrFcn: TimerOptions | Callback, interval?: number, repeats?: boolean, numberOfRepeats?: number) {
       //... todo implement
    }
}

// Example new usage

// repeat once in 200 ms
const timerOnce = new ex.Timer({
   fcn: () => { console.log('Called Once'); },
   interval: 200
});

// repeat 3 times every 200 ms
const timerThreeTimes = new ex.Timer({
   fcn: () => { console.log('Called Three Times'); },
   interval: 200,
   repeat: 3
});

// repeat forever every 200 ms
const timerForever = new ex.Timer({
   fcn: () => { console.log('Called Forever'); },
   interval: 200,
   repeat: Infinity
});

// repeat with a random range 200 ms + (0ms to 200 ms)
const timerRandomRange = new ex.Timer({
   fcn: () => { console.log('Called every 200ms + random range'); },
   interval: 200,
   repeat: Infinity,
   randomRange: 200
});

@puranjayyadav
Copy link

@eonarheim is this issue still up ?

@malitherl
Copy link
Contributor

@eonarheim Hello, is this issue still up?

@eonarheim
Copy link
Member Author

@malitherl I haven't seen any traction on the issue yet, it's yours now!

malitherl added a commit to malitherl/Excalibur that referenced this issue May 16, 2022
Used /Util/Math to calculate randomness.
This would set a random interval after receiving a range  of numbers

Resolves: excaliburjs#396
@eonarheim eonarheim removed this from the vNext milestone May 27, 2022
eonarheim pushed a commit that referenced this issue Jun 7, 2022
Closes #396

## Changes:

* This would set a random interval after receiving a range of numbers
* New random interval for each invocation of the timer
 * Added the ability to pass a Random instance into TimerOptions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community in-progress This issue is marked in-progress. This label will be removed if there is no activity for 30 days. enhancement Label applied to enhancements or improvements to existing features good first issue good for people new to open source and Excalibur
Projects
None yet
8 participants