Advertisement
Guest User

Untitled

a guest
Jul 14th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.73 KB | None | 0 0
  1. import 'dart:html';
  2. import 'package:vector_math/vector_math.dart';
  3.  
  4. class Demo {
  5.   final CanvasElement _canvas;
  6.   final Camera _camera;
  7.  
  8.   CanvasRenderingContext2D _context;
  9.  
  10.   Demo() : _canvas = document.createElement('canvas') as CanvasElement,
  11.     _camera = new Camera(new Vector2(0.0, 0.0)) {
  12.     _context = _canvas.getContext('2d') as CanvasRenderingContext2D;
  13.  
  14.     _canvas.width = 640;
  15.     _canvas.height = 640;
  16.  
  17.     document.body.append(_canvas);
  18.   }
  19.  
  20.   void start() {
  21.     window.requestAnimationFrame(processTick);
  22.   }
  23.  
  24.   void processTick(num delta) {
  25.     update();
  26.     render();
  27.   }
  28.  
  29.   void update() {}
  30.  
  31.   void render() {
  32.     var tileWidth = 32;
  33.     var tileHeight = 32;
  34.  
  35.     for (var y = 0; y < 20; y++) {
  36.       for (var x = 0; x < 20; x++) {
  37.         var pos = new Vector2(x.toDouble(), y.toDouble());
  38.         var relativePosition = _camera.getRelativePosition(pos);
  39.        
  40.         var colour = '#00ff00';
  41.         if (((x + y) % 8) == 0)
  42.           colour = '#ff00ff';
  43.         else if (((x + y) % 4) == 0)
  44.           colour = '#0000ff';
  45.         else if (((x + y) % 2) == 0)
  46.           colour = '#ff0000';
  47.  
  48.         _context.fillStyle = colour;
  49.         _context.fillRect(x * tileWidth + relativePosition.x, y * tileHeight + relativePosition.y, tileWidth, tileHeight);
  50.       }
  51.     }
  52.   }
  53. }
  54.  
  55. class Camera {
  56.   final Vector2 _position;
  57.  
  58.   Camera(this._position);
  59.  
  60.   Vector2 getRelativePosition(Vector2 position) {
  61.     return new Vector2(position.x - _position.x, position.y - _position.y);
  62.   }
  63.  
  64.   void moveTo(Vector2 position) {
  65.     _position.x = position.x;
  66.     _position.y = position.y;
  67.   }
  68.  
  69.   void move(Vector2 position) {
  70.     _position.x += position.x;
  71.     _position.y += position.y;
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement