FlowField

Developer
Size
5,931 Kb
Views
30,360

How do I make an flowfield?

Generates a flow field for a grid. This would be a useful starting point for games with a lot of navigating agents. What is a flowfield? How do you make a flowfield? This script and codes were developed by Not Important on 13 July 2022, Wednesday.

FlowField Previews

FlowField - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>flowField</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class='container' id='container'></div>
<button id='northFlow'> North
</button>
<button id='eastFlow'> East
</button>
<button id='southFlow'> South
</button>
<button id='westFlow'> West
</button>
<ul> <li> Click a cell to generate the flow field </li> <li> Pick any point on the map and follow the arrows back to the start position </li> <li> The direction buttons will generate a flow off the grid, useful for chaining grids together. </li>
</ul> <script src='http://code.jquery.com/jquery-2.1.3.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/chance/0.5.6/chance.js'></script>
<script src='js/xwolmm.js'></script> <script src="js/index.js"></script>
</body>
</html>

FlowField - Script Codes CSS Codes

.grid { height: 400px; position: relative; width: 400px; margin: 0 auto;
}
.grid-row { overflow: auto;
}
.grid-cell { background-color: #eee; color: #333; float: left; font-size: 14px; height: 18px; width: 18px; text-align: center; line-height: 16px; border-width: 1px; border-style: solid; border-color: #fff #ccc #ccc #fff; text-shadow: 1px 1px 0 #fff; cursor: pointer;
}
.grid-cell:hover { background-color: #ccc;
}
.grid-cell.grid-wall { background-color: #800;
}

FlowField - Script Codes JS Codes

(function() { var GridView, grid, gridView, mapData, rng, seed, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; seed = 1437089783193; rng = new Chance(seed); GridView = (function() { GridView.prototype.directionLookup = { '1_0': '&#8593;', '2_1': '&#8594;', '1_2': '&#8595;', '0_1': '&#8592;', '2_0': '&#8599;', '2_2': '&#8600;', '0_2': '&#8601;', '0_0': '&#8598;', '1_1': '&#9737;' }; function GridView(selector, gridData) { this.selector = selector; this.gridData = gridData; this.markCost = bind(this.markCost, this); this.markFlow = bind(this.markFlow, this); this.$el = $('<div>').addClass('grid'); this.flowField = new FlowField(this.gridData); this.addEventListeners(); } GridView.prototype.render = function() { var $cellEl, $rowEl, height, i, j, nodes, ref, ref1, ref2, width, x, y; this.$el.empty(); ref = this.gridData, nodes = ref.nodes, width = ref.width, height = ref.height; for (y = i = 0, ref1 = height; 0 <= ref1 ? i < ref1 : i > ref1; y = 0 <= ref1 ? ++i : --i) { $rowEl = this.buildRow(); for (x = j = 0, ref2 = width; 0 <= ref2 ? j < ref2 : j > ref2; x = 0 <= ref2 ? ++j : --j) { $cellEl = this.buildCell(x, y, nodes[y][x]); $rowEl.append($cellEl); } this.$el.append($rowEl); } return $(this.selector).append(this.$el); }; GridView.prototype.buildRow = function() { return $('<div>').addClass('grid-row'); }; GridView.prototype.buildCell = function(x, y, gridCell) { var $cellEl; $cellEl = $('<div>'); $cellEl.addClass('grid-cell'); $cellEl.data({ x: x, y: y }); if (!gridCell.data.open) { $cellEl.addClass('grid-wall'); } return $cellEl; }; GridView.prototype.addEventListeners = function() { this.$el.on('click', '.grid-cell', (function(_this) { return function(event) { var $el, x, y; $el = $(event.currentTarget); x = $el.data('x'); y = $el.data('y'); return _this.beginFill(x, y); }; })(this)); $('#northFlow').click((function(_this) { return function() { return _this.directionFlow(0, -1); }; })(this)); $('#eastFlow').click((function(_this) { return function() { return _this.directionFlow(1, 0); }; })(this)); $('#southFlow').click((function(_this) { return function() { return _this.directionFlow(0, 1); }; })(this)); return $('#westFlow').click((function(_this) { return function() { return _this.directionFlow(-1, 0); }; })(this)); }; GridView.prototype.directionFlow = function(vX, vY) { this.render(); return this.flowField.directionFlow(vX, vY, this.markCost, this.markFlow); }; GridView.prototype.beginFill = function(x, y) { this.render(); return this.flowField.process(x, y, this.markCost, this.markFlow); }; GridView.prototype.markFlow = function(x, y, vector) { var $cellEl, directionIndex; $cellEl = this.$el.find('.grid-row').eq(y).find('.grid-cell').eq(x); directionIndex = (vector.x + 1) + "_" + (vector.y + 1); return $cellEl.html(this.directionLookup[directionIndex]); }; GridView.prototype.markCost = function(x, y, iteration) { var $cellEl; return; $cellEl = this.$el.find('.grid-row').eq(y).find('.grid-cell').eq(x); $cellEl.addClass('grid-cell-marked'); return $cellEl.html(iteration); }; return GridView; })(); mapData = function(width, height) { var data, i, j, ref, ref1, x, y; data = []; for (y = i = 0, ref = height; 0 <= ref ? i < ref : i > ref; y = 0 <= ref ? ++i : --i) { data[y] = []; for (x = j = 0, ref1 = width; 0 <= ref1 ? j < ref1 : j > ref1; x = 0 <= ref1 ? ++j : --j) { data[y][x] = rng.bool({ likelihood: 90 }); } } return data; }; grid = new Grid(mapData(20, 20)); gridView = new GridView('#container', grid); gridView.render();
}).call(this);
FlowField - Script Codes
FlowField - Script Codes
Home Page Home
Developer Not Important
Username clindsey
Uploaded July 13, 2022
Rating 3
Size 5,931 Kb
Views 30,360
Do you need developer help for FlowField?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Not Important (clindsey) Script Codes
Create amazing blog posts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!