Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 1 | package com.savoirfairelinux.sflphone.model; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | |
| 5 | public class BubbleModel |
| 6 | { |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame^] | 7 | private static final String TAG = BubbleModel.class.getSimpleName(); |
| 8 | |
| 9 | public long lastUpdate = 0; |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 10 | public int width, height; |
| 11 | public ArrayList<Bubble> listBubbles = new ArrayList<Bubble>(); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame^] | 12 | |
| 13 | private static final float BUBBLE_SPEED = 4.f; |
| 14 | |
| 15 | public void update() |
| 16 | { |
| 17 | long now = System.nanoTime(); |
| 18 | |
| 19 | // Do nothing if lastUpdate is in the future. |
| 20 | if (lastUpdate > now) |
| 21 | return; |
| 22 | |
| 23 | float dt = (float) Math.min((now - lastUpdate) / 1000000000.0, .2); |
| 24 | lastUpdate = now; |
| 25 | |
| 26 | //Log.w(TAG, "update dt="+dt); |
| 27 | |
| 28 | // Iterators should not be used in frequently called methods |
| 29 | // to avoid garbage collection glitches caused by iterator objects. |
| 30 | for(int i=0, n=listBubbles.size(); i<n; i++) { |
| 31 | Bubble b = listBubbles.get(i); |
| 32 | //Log.w(TAG, "update b"); |
| 33 | if(!b.dragged && b.attractor != null) { |
| 34 | float bx=b.getPosX(), by=b.getPosY(); |
| 35 | float dx = (b.attractor.x - bx) * dt * BUBBLE_SPEED; |
| 36 | float dy = (b.attractor.y - by) * dt * BUBBLE_SPEED; |
| 37 | //Log.w(TAG, "update dx="+dt+" dy="+dy); |
| 38 | b.setPos(bx+dx, by+dy); |
| 39 | } |
| 40 | } |
| 41 | } |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 42 | } |